Skip to content

Commit

Permalink
feat: add arg to route all services to remote/local target (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
augustoccesar authored Oct 22, 2024
1 parent 7193cf3 commit a42e36c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 30 deletions.
26 changes: 22 additions & 4 deletions linkup-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,28 @@ enum Commands {
Reset,

#[clap(about = "Route session traffic to a local service")]
Local { service_names: Vec<String> },
Local {
service_names: Vec<String>,
#[arg(
short,
long,
help = "Route all the services to local. Cannot be used with SERVICE_NAMES.",
conflicts_with = "service_names"
)]
all: bool,
},

#[clap(about = "Route session traffic to a remote service")]
Remote { service_names: Vec<String> },
Remote {
service_names: Vec<String>,
#[arg(
short,
long,
help = "Route all the services to remote. Cannot be used with SERVICE_NAMES.",
conflicts_with = "service_names"
)]
all: bool,
},

#[clap(about = "View linkup component and service status")]
Status {
Expand Down Expand Up @@ -232,8 +250,8 @@ fn main() -> Result<()> {
Commands::Start { no_tunnel } => start(&cli.config, *no_tunnel),
Commands::Stop => stop(),
Commands::Reset => reset(),
Commands::Local { service_names } => local(service_names),
Commands::Remote { service_names } => remote(service_names),
Commands::Local { service_names, all } => local(service_names, *all),
Commands::Remote { service_names, all } => remote(service_names, *all),
Commands::Status { json, all } => status(*json, *all),
Commands::LocalDNS { subcommand } => match subcommand {
LocalDNSSubcommand::Install => local_dns::install(&cli.config),
Expand Down
69 changes: 43 additions & 26 deletions linkup-cli/src/remote_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,76 @@ use crate::{
CliError, LINKUP_LOCALSERVER_PORT,
};

pub fn remote(service_names: &[String]) -> Result<(), CliError> {
if service_names.is_empty() {
pub fn remote(service_names: &[String], all: bool) -> Result<(), CliError> {
if service_names.is_empty() && !all {
return Err(CliError::NoSuchService(
"No service names provided".to_string(),
));
}

let mut state = LocalState::load()?;

for service_name in service_names {
let service = state
.services
.iter_mut()
.find(|s| s.name.as_str() == service_name)
.ok_or_else(|| CliError::NoSuchService(service_name.to_string()))?;
service.current = ServiceTarget::Remote;
for service in state.services.iter_mut() {
if all {
service.current = ServiceTarget::Remote;
continue;
}

if service_names.contains(&service.name) {
service.current = ServiceTarget::Remote;
} else {
return Err(CliError::NoSuchService(service.name.clone()));
}
}

state.save()?;
load_server_states(state)?;

println!(
"Linkup is routing {} traffic to the remote server",
service_names.join(", ")
);
if all {
println!("Linkup is routing all traffic to the remote servers");
} else {
println!(
"Linkup is routing {} traffic to the remote server",
service_names.join(", ")
);
}

Ok(())
}

pub fn local(service_names: &[String]) -> Result<(), CliError> {
if service_names.is_empty() {
pub fn local(service_names: &[String], all: bool) -> Result<(), CliError> {
if service_names.is_empty() && !all {
return Err(CliError::NoSuchService(
"No service names provided".to_string(),
));
}

let mut state = LocalState::load()?;

for service_name in service_names {
let service = state
.services
.iter_mut()
.find(|s| s.name.as_str() == service_name)
.ok_or_else(|| CliError::NoSuchService(service_name.to_string()))?;
service.current = ServiceTarget::Local;
for service in state.services.iter_mut() {
if all {
service.current = ServiceTarget::Local;
continue;
}

if service_names.contains(&service.name) {
service.current = ServiceTarget::Local;
} else {
return Err(CliError::NoSuchService(service.name.clone()));
}
}

state.save()?;
load_server_states(state)?;

println!(
"Linkup is routing {} traffic to the local server",
service_names.join(", ")
);
if all {
println!("Linkup is routing all traffic to the local servers");
} else {
println!(
"Linkup is routing {} traffic to the local server",
service_names.join(", ")
);
}

Ok(())
}
Expand Down

0 comments on commit a42e36c

Please sign in to comment.