Skip to content

Commit

Permalink
feat: refactor org name handling, make global args global
Browse files Browse the repository at this point in the history
  • Loading branch information
tmlye committed Feb 21, 2024
1 parent a04b465 commit f84d494
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 41 deletions.
29 changes: 7 additions & 22 deletions src/commands/environments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use clap::{Parser, Subcommand};
use super::CommandBase;
use tabled::Table;

#[derive(Parser)]
#[derive(Debug)]
#[derive(Debug, Parser)]
#[command(
author,
version,
Expand All @@ -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)]
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -73,20 +66,12 @@ impl Create {
}
}

#[derive(Parser)]
#[derive(Debug)]
pub struct List {
#[arg(long, help = "Organization to list the environments of")]
org: Option<String>,
}
#[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()
Expand Down
18 changes: 16 additions & 2 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use once_cell::sync::OnceCell;

use crate::{
Expand All @@ -19,13 +19,15 @@ pub mod services;
pub struct CommandBase<'a> {
user_config: &'a mut UserConfig,
app_config: OnceCell<ApplicationConfig>,
org_arg: Option<String>,
}

impl CommandBase<'_> {
pub fn new(user_config: &mut UserConfig) -> CommandBase {
pub fn new(user_config: &mut UserConfig, org_arg: Option<String>) -> CommandBase {
CommandBase {
user_config,
app_config: OnceCell::new(),
org_arg,
}
}

Expand Down Expand Up @@ -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<String> {
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)
}
}
12 changes: 2 additions & 10 deletions src/commands/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
15 changes: 8 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ mod commands;
mod config;


#[derive(Debug)]
#[derive(Parser)]
#[derive(Debug, Parser)]
#[command(
author,
version,
Expand All @@ -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<Utf8PathBuf>,

#[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<String>,

#[arg(global = true, long, env("MOLNETT_ORG"), help = "Organization to use (overrides default in config)")]
org: Option<String>,

#[command(subcommand)]
command: Option<Commands>,
}

#[derive(Debug)]
#[derive(Subcommand)]
#[derive(Debug, Subcommand)]
enum Commands {
/// Manage organizations
Orgs(commands::orgs::Orgs),
Expand All @@ -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),
Expand Down

0 comments on commit f84d494

Please sign in to comment.