diff --git a/src/commands/environments.rs b/src/commands/environments.rs index 12952f7..b548b56 100644 --- a/src/commands/environments.rs +++ b/src/commands/environments.rs @@ -3,8 +3,7 @@ use clap::{Parser, Subcommand}; use super::CommandBase; use tabled::Table; -#[derive(Parser)] -#[derive(Debug)] +#[derive(Debug, Parser)] #[command( author, version, @@ -28,8 +27,7 @@ impl Environments { } } -#[derive(Subcommand)] -#[derive(Debug)] +#[derive(Debug, Subcommand)] pub enum Commands { /// Create an environment #[command(arg_required_else_help = true)] @@ -39,8 +37,7 @@ pub enum Commands { List(List), } -#[derive(Parser)] -#[derive(Debug)] +#[derive(Debug, Parser)] pub struct Create { #[arg(help = "Name of the environment to create")] name: String, @@ -50,11 +47,7 @@ pub struct Create { impl Create { pub fn execute(&self, base: &CommandBase) -> Result<()> { - let org_name = if self.org.is_some() { - self.org.clone().unwrap() - } else { - base.user_config().get_default_org().unwrap().to_string() - }; + let org_name = base.get_org()?; let token = base .user_config() .get_token() @@ -73,20 +66,12 @@ impl Create { } } -#[derive(Parser)] -#[derive(Debug)] -pub struct List { - #[arg(long, help = "Organization to list the environments of")] - org: Option, -} +#[derive(Debug, Parser)] +pub struct List {} impl List { pub fn execute(&self, base: &CommandBase) -> Result<()> { - let org_name = if self.org.is_some() { - self.org.clone().unwrap() - } else { - base.user_config().get_default_org().unwrap().to_string() - }; + let org_name = base.get_org()?; let token = base .user_config() .get_token() diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 3992f14..12db80e 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{anyhow, Result}; use once_cell::sync::OnceCell; use crate::{ @@ -19,13 +19,15 @@ pub mod services; pub struct CommandBase<'a> { user_config: &'a mut UserConfig, app_config: OnceCell, + org_arg: Option, } impl CommandBase<'_> { - pub fn new(user_config: &mut UserConfig) -> CommandBase { + pub fn new(user_config: &mut UserConfig, org_arg: Option) -> CommandBase { CommandBase { user_config, app_config: OnceCell::new(), + org_arg, } } @@ -54,4 +56,16 @@ impl CommandBase<'_> { self.app_config()?; Ok(self.app_config.get_mut().ok_or(Error::UserConfigNotInit)?) } + + pub fn get_org(&self) -> Result { + let org_name = if self.org_arg.is_some() { + self.org_arg.clone().unwrap() + } else { + match self.user_config.get_default_org() { + Some(cfg) => cfg.to_string(), + None => return Err(anyhow!("Either set a default org in the config or provide one via --org")) + } + }; + Ok(org_name) + } } diff --git a/src/commands/services.rs b/src/commands/services.rs index 11c2691..8bcd942 100644 --- a/src/commands/services.rs +++ b/src/commands/services.rs @@ -69,11 +69,7 @@ pub struct Manifest { impl Deploy { pub fn execute(&self, base: &CommandBase) -> Result<()> { - let org_name = if self.org.is_some() { - self.org.clone().unwrap() - } else { - base.user_config().get_default_org().unwrap().to_string() - }; + let org_name = base.get_org()?; let token = base .user_config() .get_token() @@ -419,11 +415,7 @@ pub struct List { impl List { pub fn execute(&self, base: &CommandBase) -> Result<()> { - let org_name = if self.org.is_some() { - self.org.clone().unwrap() - } else { - base.user_config().get_default_org().unwrap().to_string() - }; + let org_name = base.get_org()?; let token = base .user_config() .get_token() diff --git a/src/main.rs b/src/main.rs index 21adb76..9be6e2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,8 +8,7 @@ mod commands; mod config; -#[derive(Debug)] -#[derive(Parser)] +#[derive(Debug, Parser)] #[command( author, version, @@ -19,18 +18,20 @@ mod config; arg_required_else_help = true )] struct Cli { - #[arg(short, long, value_name = "FILE", env("MOLNETT_CONFIG"), help = "config file, default is $HOME/.config/molnett/config.json")] + #[arg(global = true, short, long, value_name = "FILE", env("MOLNETT_CONFIG"), help = "config file, default is $HOME/.config/molnett/config.json")] config: Option, - #[arg(long, env("MOLNETT_API_URL"), help = "Url of the Molnett API, default is https://api.molnett.org")] + #[arg(global = true, long, env("MOLNETT_API_URL"), help = "Url of the Molnett API, default is https://api.molnett.org")] url: Option, + #[arg(global = true, long, env("MOLNETT_ORG"), help = "Organization to use (overrides default in config)")] + org: Option, + #[command(subcommand)] command: Option, } -#[derive(Debug)] -#[derive(Subcommand)] +#[derive(Debug, Subcommand)] enum Commands { /// Manage organizations Orgs(commands::orgs::Orgs), @@ -50,7 +51,7 @@ fn main() -> Result<()> { } let mut config = UserConfig::new(&cli); - let mut base = CommandBase::new(&mut config); + let mut base = CommandBase::new(&mut config, cli.org); match cli.command { Some(Commands::Services(svcs)) => svcs.execute(&mut base),