Skip to content

Commit

Permalink
refactor: move config to cli arguments instead of command (#48)
Browse files Browse the repository at this point in the history
Config is always optional for the commands that use it, so instead of
repeating it on all commands, we can move it to the cli arguments so
that any command that would like to use this option, could do so.
  • Loading branch information
augustoccesar authored Nov 20, 2023
1 parent 37e9e0c commit fe1cd00
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 45 deletions.
69 changes: 26 additions & 43 deletions linkup-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ pub enum CliError {
version = env!("CARGO_PKG_VERSION"),
)]
struct Cli {
#[arg(
short,
long,
value_name = "CONFIG",
help = "Path to config file, overriding environment variable."
)]
config: Option<String>,

#[command(subcommand)]
command: Commands,
}
Expand All @@ -139,31 +147,20 @@ enum LocalDNSSubcommand {
#[derive(Subcommand)]
enum Commands {
#[clap(about = "Start a new linkup session")]
Start {
#[arg(
short,
long,
value_name = "CONFIG",
help = "Path to config file, overriding environment variable."
)]
config: Option<String>,
},
Start,

#[clap(about = "Stop a running linkup session")]
Stop {},
Stop,

#[clap(about = "Reset a linkup session")]
Reset {
#[arg(
short,
long,
value_name = "CONFIG",
help = "Path to config file, overriding environment variable."
)]
config: Option<String>,
},
Reset,

#[clap(about = "Route session traffic to a local service")]
Local { service_names: Vec<String> },

#[clap(about = "Route session traffic to a remote service")]
Remote { service_names: Vec<String> },

#[clap(about = "View linkup component and service status")]
Status {
// Output status in JSON format
Expand All @@ -172,33 +169,20 @@ enum Commands {
#[arg(short, long)]
all: bool,
},
LocalDNS {
#[arg(
short,
long,
value_name = "CONFIG",
help = "Path to config file, overriding environment variable."
)]
config: Option<String>,

LocalDNS {
#[clap(subcommand)]
subcommand: LocalDNSSubcommand,
},

#[clap(about = "Generate completions for your shell")]
Completion {
#[arg(long, value_enum)]
shell: Option<Shell>,
},

#[clap(about = "Create a \"permanent\" Linkup preview")]
Preview {
#[arg(
short,
long,
value_name = "CONFIG",
help = "Path to config file, overriding environment variable."
)]
config: Option<String>,

#[arg(
help = "<service>=<url> pairs to preview.",
value_parser = ValueParser::new(preview::parse_services_tuple),
Expand All @@ -218,21 +202,20 @@ fn main() -> Result<()> {
ensure_linkup_dir()?;

match &cli.command {
Commands::Start { config } => start(config),
Commands::Stop {} => stop(),
Commands::Reset { config } => reset(config),
Commands::Start => start(&cli.config),
Commands::Stop => stop(),
Commands::Reset => reset(&cli.config),
Commands::Local { service_names } => local(service_names),
Commands::Remote { service_names } => remote(service_names),
Commands::Status { json, all } => status(*json, *all),
Commands::LocalDNS { config, subcommand } => match subcommand {
LocalDNSSubcommand::Install => local_dns::install(config),
LocalDNSSubcommand::Uninstall => local_dns::uninstall(config),
Commands::LocalDNS { subcommand } => match subcommand {
LocalDNSSubcommand::Install => local_dns::install(&cli.config),
LocalDNSSubcommand::Uninstall => local_dns::uninstall(&cli.config),
},
Commands::Completion { shell } => completion(shell),
Commands::Preview {
config,
services,
print_request,
} => preview(config, services, *print_request),
} => preview(&cli.config, services, *print_request),
}
}
2 changes: 1 addition & 1 deletion linkup-cli/src/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn set_service_env(directory: String, config_path: String) -> Result<(), CliErro

let service_path = PathBuf::from(config_dir).join(&directory);

let dev_env_files_result = fs::read_dir(&service_path);
let dev_env_files_result = fs::read_dir(service_path);
let dev_env_files: Vec<_> = match dev_env_files_result {
Ok(entries) => entries
.filter_map(Result::ok)
Expand Down
2 changes: 1 addition & 1 deletion linkup-cli/src/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn remove_service_env(directory: String, config_path: String) -> Result<(), CliE

let service_path = PathBuf::from(config_dir).join(&directory);

let env_files_result = fs::read_dir(&service_path);
let env_files_result = fs::read_dir(service_path);
let env_files: Vec<_> = match env_files_result {
Ok(entries) => entries
.filter_map(Result::ok)
Expand Down

0 comments on commit fe1cd00

Please sign in to comment.