1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
pub mod api;
pub mod auth;
pub mod config;
pub mod issue;
pub mod label;
pub mod milestone;
pub mod notification;
pub mod pull_request;
pub mod repo;
pub mod user;

// utils for DRYing text functionalities
mod text_manipulation;

use clap::{CommandFactory, Parser};
use clap_complete::Shell;

#[derive(Debug, clap::Parser)]
#[command(version)]
pub struct BergCommand {
    #[command(subcommand)]
    pub sub: BergActions,

    #[arg(
        long,
        help = [
            "Whether or not to disable all interactive features.",
            "In this case arguments have to be provided in the console!",
            ""
        ].join("\n")
    )]
    pub non_interactive: bool,
}

pub struct GeneralArgs {
    pub non_interactive: bool,
}

impl BergCommand {
    pub fn generate_completion(shell: Shell) -> anyhow::Result<()> {
        clap_complete::generate(
            shell,
            &mut BergCommand::command(),
            "berg",
            &mut std::io::stdout(),
        );
        Ok(())
    }

    pub async fn run(self) -> anyhow::Result<()> {
        let general_args = GeneralArgs {
            non_interactive: self.non_interactive,
        };
        match self.sub {
            BergActions::API(args) => args.run(general_args).await,
            BergActions::Auth(args) => args.run(general_args).await,
            BergActions::Config(args) => args.run(general_args).await,
            BergActions::User(args) => args.run(general_args).await,
            BergActions::Issue(args) => args.run(general_args).await,
            BergActions::Pull(args) => args.run(general_args).await,
            BergActions::Label(args) => args.run(general_args).await,
            BergActions::Repo(args) => args.run(general_args).await,
            BergActions::Milestone(args) => args.run(general_args).await,
            BergActions::Notification(args) => args.run(general_args).await,
            BergActions::Completion { shell } => Self::generate_completion(shell),
        }
    }
}

/// Codeberg CLI app
#[derive(Parser, Debug)]
pub enum BergActions {
    #[command(subcommand)]
    API(api::ApiArgs),

    #[command(subcommand)]
    Auth(auth::AuthArgs),

    #[command(subcommand)]
    Config(config::ConfigArgs),

    #[command(subcommand)]
    User(user::UserArgs),

    #[command(subcommand)]
    Issue(issue::IssueArgs),

    #[command(subcommand)]
    Pull(pull_request::PullRequestArgs),

    #[command(subcommand)]
    Label(label::LabelArgs),

    #[command(subcommand)]
    Repo(repo::RepoArgs),

    #[command(subcommand)]
    Milestone(milestone::MilestoneArgs),

    #[command(subcommand)]
    Notification(notification::NotificationArgs),

    /// Print completion script
    Completion {
        /// Shell to generate completion for
        shell: Shell,
    },
}