codeberg-cli 0.4.9

CLI Tool for codeberg similar to gh and glab
Documentation
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!",
            "",
            "Still WIP"
        ].join("\n")
    )]
    pub non_interactive: bool,

    #[arg(long, short = 'w', help = [
        "Maximum with of the stdout output,",
        "",
        "- negative numbers indicate using 'infinite' width per line",
        "- zero indicates using the terminals width",
        "- positive numbers are interpreted as max width. You may specify",
        "  widths that can lead to weird linebreaks. This is a feature for tools",
        "  which process stdout output line by line. You may also just negative",
        "  widths in this case. We discourage use of widths <= 25",
        "",
        "Falls back to `max_width` value in config or defaults to 80 otherwise."
        ].join("\n")
    )]
    pub max_width: Option<i32>,
}

pub struct GeneralArgs {
    pub non_interactive: bool,
    pub max_width: Option<i32>,
}

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,
            max_width: self.max_width,
        };
        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,
    },
}