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