mod api;
mod auth;
mod browse;
mod client;
mod config;
mod context;
mod exit;
mod output;
mod pipeline;
mod pr;
mod repo;
mod workspace;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(name = "bb", version, about = "Work with Bitbucket Cloud from the command line")]
struct Cli {
#[arg(short = 'R', long = "repo", id = "repo_flag", global = true, value_name = "WORKSPACE/REPO")]
repo: Option<String>,
#[arg(long, global = true)]
json: bool,
#[command(subcommand)]
cmd: Cmd,
}
#[derive(Subcommand)]
enum Cmd {
Auth {
#[command(subcommand)]
cmd: auth::Cmd,
},
Repo {
#[command(subcommand)]
cmd: repo::Cmd,
},
#[command(alias = "ws", visible_alias = "org")]
Workspace {
#[command(subcommand)]
cmd: workspace::Cmd,
},
Pr {
#[command(subcommand)]
cmd: pr::Cmd,
},
#[command(alias = "run")]
Pipeline {
#[command(subcommand)]
cmd: pipeline::Cmd,
},
Workflow {
#[command(subcommand)]
cmd: pipeline::WorkflowCmd,
},
Api(api::Args),
Browse(browse::Args),
}
fn main() {
let cli = Cli::parse();
context::set_repo_flag(cli.repo);
output::set_json(cli.json);
let res = match cli.cmd {
Cmd::Auth { cmd } => auth::run(cmd),
Cmd::Repo { cmd } => repo::run(cmd),
Cmd::Workspace { cmd } => workspace::run(cmd),
Cmd::Pr { cmd } => pr::run(cmd),
Cmd::Pipeline { cmd } => pipeline::run(cmd),
Cmd::Workflow { cmd } => pipeline::workflow(cmd),
Cmd::Api(args) => api::run(args),
Cmd::Browse(args) => browse::run(args),
};
if let Err(e) = res {
eprintln!("bb: {e:#}");
std::process::exit(exit::code(&e));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn repo_flag_works_on_subcommands_with_a_repo_positional() {
for args in [["bb", "repo", "view", "-R", "ws/r"], ["bb", "repo", "delete", "-R", "ws/r"]] {
let cli = Cli::try_parse_from(args).unwrap();
assert_eq!(cli.repo.as_deref(), Some("ws/r"));
}
}
#[test]
fn pr_merge_strategy_flags_are_exclusive() {
assert!(Cli::try_parse_from(["bb", "pr", "merge", "1", "--rebase-merge"]).is_ok());
assert!(Cli::try_parse_from(["bb", "pr", "merge", "1", "--squash-fast-forward"]).is_ok());
for args in [["bb", "pr", "merge", "1", "--rebase-merge", "-s"], ["bb", "pr", "merge", "1", "--squash-fast-forward", "--rebase-merge"]] {
assert_eq!(Cli::try_parse_from(args).err().unwrap().kind(), clap::error::ErrorKind::ArgumentConflict);
}
}
}