Skip to main content

codeberg_cli/actions/
mod.rs

1pub mod api;
2pub mod auth;
3pub mod config;
4pub mod issue;
5pub mod keys;
6pub mod label;
7pub mod milestone;
8pub mod notification;
9pub mod pull_request;
10pub mod release;
11pub mod repo;
12pub mod user;
13
14// utils for DRYing text functionalities
15mod text_manipulation;
16
17use clap::builder::Styles;
18use clap::builder::styling::{AnsiColor, Effects};
19use clap::{CommandFactory, Parser};
20use clap_complete::Shell;
21
22use crate::types::{git::OwnerRepo, output::OutputMode};
23
24const STYLES: Styles = Styles::styled()
25    .header(AnsiColor::Yellow.on_default().effects(Effects::BOLD))
26    .usage(AnsiColor::Green.on_default().effects(Effects::BOLD))
27    .literal(AnsiColor::Green.on_default().effects(Effects::BOLD))
28    .placeholder(AnsiColor::Green.on_default());
29
30#[derive(Debug, clap::Parser)]
31#[command(version, styles = STYLES)]
32pub struct BergCommand {
33    #[command(subcommand)]
34    pub sub: BergActions,
35
36    /// How to display the responses of the forgejo instance if there are any
37    #[arg(value_enum, long, default_value_t = OutputMode::Pretty, global = true,)]
38    pub output_mode: OutputMode,
39
40    /// Whether or not to disable all interactive features.
41    /// In this case arguments have to be provided in the console!
42    ///
43    /// Still WIP
44    #[arg(long, global = true)]
45    pub non_interactive: bool,
46
47    /// Maximum with of the stdout output,
48    ///
49    /// - negative numbers indicate using 'infinite' width per line
50    ///
51    /// - zero indicates using the terminals width
52    ///
53    /// - positive numbers are interpreted as max width. You may specify
54    ///   widths that can lead to weird linebreaks. This is a feature for tools
55    ///   which process stdout output line by line. You may also just negative
56    ///   widths in this case. We discourage use of widths <= 25
57    ///
58    /// Falls back to `max_width` value in config or defaults to 80 otherwise.
59    #[arg(long, short = 'w', global = true)]
60    pub max_width: Option<i32>,
61
62    /// The OWNER/REPO tuple if you want to target a repository other than the one in $PWD
63    #[arg(long, global = true, value_name = "OWNER/REPO")]
64    pub owner_repo: Option<OwnerRepo>,
65}
66
67#[derive(Debug, Clone)]
68pub struct GlobalArgs {
69    pub non_interactive: bool,
70    pub max_width: Option<i32>,
71    pub output_mode: OutputMode,
72    pub owner_repo: Option<OwnerRepo>,
73}
74
75impl BergCommand {
76    pub fn generate_completion(shell: Shell) -> miette::Result<()> {
77        clap_complete::generate(
78            shell,
79            &mut BergCommand::command(),
80            "berg",
81            &mut std::io::stdout(),
82        );
83        Ok(())
84    }
85
86    pub async fn run(self) -> miette::Result<()> {
87        let global_args = GlobalArgs {
88            non_interactive: self.non_interactive,
89            max_width: self.max_width,
90            output_mode: self.output_mode,
91            owner_repo: self.owner_repo,
92        };
93        match self.sub {
94            BergActions::API(args) => args.run(global_args).await,
95            BergActions::Auth(args) => args.run(global_args).await,
96            BergActions::Config(args) => args.run(global_args).await,
97            BergActions::User(args) => args.run(global_args).await,
98            BergActions::Issue(args) => args.run(global_args).await,
99            BergActions::Pull(args) => args.run(global_args).await,
100            BergActions::Label(args) => args.run(global_args).await,
101            BergActions::Release(args) => args.run(global_args).await,
102            BergActions::Repo(args) => args.run(global_args).await,
103            BergActions::Milestone(args) => args.run(global_args).await,
104            BergActions::Notification(args) => args.run(global_args).await,
105            BergActions::Keys(args) => args.run(global_args).await,
106            BergActions::Completion { shell } => Self::generate_completion(shell),
107        }
108    }
109}
110
111/// Codeberg/Forgejo CLI app
112#[derive(Parser, Debug)]
113pub enum BergActions {
114    #[command(subcommand)]
115    API(api::ApiArgs),
116
117    #[command(subcommand)]
118    Auth(auth::AuthArgs),
119
120    #[command(subcommand)]
121    Config(config::ConfigArgs),
122
123    #[command(subcommand)]
124    User(user::UserArgs),
125
126    #[command(subcommand)]
127    Issue(issue::IssueArgs),
128
129    #[command(subcommand)]
130    Pull(pull_request::PullRequestArgs),
131
132    #[command(subcommand)]
133    Label(label::LabelArgs),
134
135    #[command(subcommand)]
136    Release(release::ReleaseArgs),
137
138    #[command(subcommand)]
139    Repo(repo::RepoArgs),
140
141    #[command(subcommand)]
142    Milestone(milestone::MilestoneArgs),
143
144    #[command(subcommand)]
145    Notification(notification::NotificationArgs),
146
147    #[command(subcommand)]
148    Keys(keys::KeysArgs),
149
150    /// Print completion script
151    Completion {
152        /// Shell to generate completion for
153        shell: Shell,
154    },
155}