| 1 | //! The commands. |
| 2 | |
| 3 | pub mod alias; |
| 4 | pub mod api; |
| 5 | pub mod auth; |
| 6 | pub mod browse; |
| 7 | pub mod deploy; |
| 8 | pub mod issue; |
| 9 | pub mod logs; |
| 10 | pub mod pr; |
| 11 | pub mod repo; |
| 12 | pub mod run; |
| 13 | pub mod secret; |
| 14 | pub mod status; |
| 15 | |
| 16 | use clap::CommandFactory; |
| 17 | |
| 18 | use crate::cli::{Cli, Command}; |
| 19 | use crate::context::Ctx; |
| 20 | use crate::error::{Error, Result}; |
| 21 | |
| 22 | pub fn dispatch(cli: Cli) -> Result<()> { |
| 23 | match cli.command { |
| 24 | Command::Completion(args) => { |
| 25 | let mut command = Cli::command(); |
| 26 | clap_complete::generate(args.shell, &mut command, "g1t", &mut std::io::stdout()); |
| 27 | Ok(()) |
| 28 | } |
| 29 | Command::Push(args) => { |
| 30 | let push = crate::push::Push::from_args(args).map_err(Error::usage)?; |
| 31 | push.run().map_err(Error::failed) |
| 32 | } |
| 33 | Command::Alias(command) => alias::run(command), |
| 34 | Command::Auth(command) => auth::run(Ctx::new(cli.global)?, command), |
| 35 | Command::Repo(command) => repo::run(&Ctx::new(cli.global)?, command), |
| 36 | Command::Issue(command) => issue::run(&Ctx::new(cli.global)?, command), |
| 37 | Command::Pr(command) => pr::run(&Ctx::new(cli.global)?, command), |
| 38 | Command::Run(command) => run::run(&Ctx::new(cli.global)?, command), |
| 39 | Command::Secret(command) => secret::run(&Ctx::new(cli.global)?, command), |
| 40 | Command::Deploy(command) => deploy::run(&Ctx::new(cli.global)?, command), |
| 41 | Command::Logs(args) => logs::run(&Ctx::new(cli.global)?, args), |
| 42 | Command::Status(_) => status::run(&Ctx::new(cli.global)?), |
| 43 | Command::Api(args) => api::run(&Ctx::new(cli.global)?, args), |
| 44 | Command::Browse(args) => browse::run(&Ctx::new(cli.global)?, args), |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// `@me` is the signed-in account. |
| 49 | pub fn who(ctx: &Ctx, client: &crate::client::Client, login: &str) -> Result<String> { |
| 50 | if login == "@me" { |
| 51 | ctx.me(client) |
| 52 | } else { |
| 53 | Ok(login.trim_start_matches('@').to_owned()) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// The items of a list answer, which comes bare or under a key. |
| 58 | pub fn items(value: serde_json::Value, key: &str) -> Vec<serde_json::Value> { |
| 59 | match value { |
| 60 | serde_json::Value::Array(list) => list, |
| 61 | mut other => match other.get_mut(key).map(serde_json::Value::take) { |
| 62 | Some(serde_json::Value::Array(list)) => list, |
| 63 | _ => match other.get_mut("items").map(serde_json::Value::take) { |
| 64 | Some(serde_json::Value::Array(list)) => list, |
| 65 | _ => Vec::new(), |
| 66 | }, |
| 67 | }, |
| 68 | } |
| 69 | } |