codeberg_cli/actions/
mod.rs

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