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