From 120a86b8ddec8df047e1ac0594f8e1ee6e6f6009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augusto=20C=C3=A9sar?= Date: Fri, 17 Nov 2023 15:28:17 +0100 Subject: [PATCH 1/8] refactor: stop using start methods on other commands Achieve this by moving the state saving and loading responsibility to the LocalState itself --- linkup-cli/src/background_booting.rs | 7 ++--- linkup-cli/src/local_config.rs | 40 +++++++++++++++++++++++- linkup-cli/src/remote_local.rs | 9 +++--- linkup-cli/src/reset.rs | 6 ++-- linkup-cli/src/start.rs | 46 +++------------------------- linkup-cli/src/status.rs | 3 +- linkup-cli/src/stop.rs | 4 +-- 7 files changed, 57 insertions(+), 58 deletions(-) diff --git a/linkup-cli/src/background_booting.rs b/linkup-cli/src/background_booting.rs index 38d4a67..38e0d71 100644 --- a/linkup-cli/src/background_booting.rs +++ b/linkup-cli/src/background_booting.rs @@ -11,14 +11,13 @@ use crate::background_local_server::{ }; use crate::background_tunnel::start_tunnel; use crate::local_config::{LocalState, ServiceTarget}; -use crate::start::save_state; use crate::status::print_session_names; use crate::worker_client::WorkerClient; +use crate::CliError; use crate::LINKUP_LOCALSERVER_PORT; -use crate::{start::get_state, CliError}; pub fn boot_background_services() -> Result<(), CliError> { - let mut state = get_state()?; + let mut state = LocalState::load()?; let local_url = Url::parse(&format!("http://localhost:{}", LINKUP_LOCALSERVER_PORT)) .expect("linkup url invalid"); @@ -58,7 +57,7 @@ pub fn boot_background_services() -> Result<(), CliError> { state.linkup.session_name = server_session_name.clone(); let state_to_print = state.clone(); - save_state(state)?; + state.save()?; println!("Waiting for tunnel to be ready at {}...", tunnel_url); diff --git a/linkup-cli/src/local_config.rs b/linkup-cli/src/local_config.rs index 6da49a9..793bc7c 100644 --- a/linkup-cli/src/local_config.rs +++ b/linkup-cli/src/local_config.rs @@ -10,7 +10,7 @@ use url::Url; use linkup::{CreatePreviewRequest, StorableDomain, StorableRewrite, StorableService}; -use crate::{CliError, LINKUP_CONFIG_ENV}; +use crate::{linkup_file_path, CliError, LINKUP_CONFIG_ENV, LINKUP_STATE_FILE}; #[derive(Deserialize, Serialize, Clone)] pub struct LocalState { @@ -19,6 +19,44 @@ pub struct LocalState { pub services: Vec, } +impl LocalState { + pub fn load() -> Result { + if let Err(e) = fs::File::open(linkup_file_path(LINKUP_STATE_FILE)) { + return Err(CliError::NoState(e.to_string())); + } + + let content = match fs::read_to_string(linkup_file_path(LINKUP_STATE_FILE)) { + Ok(content) => content, + Err(e) => return Err(CliError::NoState(e.to_string())), + }; + + match serde_yaml::from_str(&content) { + Ok(config) => Ok(config), + Err(e) => Err(CliError::NoState(e.to_string())), + } + } + + pub fn save(&self) -> Result<(), CliError> { + let yaml_string = match serde_yaml::to_string(self) { + Ok(yaml) => yaml, + Err(_) => { + return Err(CliError::SaveState( + "Failed to serialize the state into YAML".to_string(), + )) + } + }; + + if fs::write(linkup_file_path(LINKUP_STATE_FILE), yaml_string).is_err() { + return Err(CliError::SaveState(format!( + "Failed to write the state file at {}", + linkup_file_path(LINKUP_STATE_FILE).display() + ))); + } + + Ok(()) + } +} + #[derive(Deserialize, Serialize, Clone)] pub struct LinkupState { pub session_name: String, diff --git a/linkup-cli/src/remote_local.rs b/linkup-cli/src/remote_local.rs index 90564a1..1afd86d 100644 --- a/linkup-cli/src/remote_local.rs +++ b/linkup-cli/src/remote_local.rs @@ -3,7 +3,6 @@ use url::Url; use crate::{ background_booting::{load_config, server_config_from_state}, local_config::{LocalState, ServiceTarget}, - start::{get_state, save_state}, CliError, LINKUP_LOCALSERVER_PORT, }; @@ -13,7 +12,7 @@ pub fn remote(service_names: &[String]) -> Result<(), CliError> { "No service names provided".to_string(), )); } - let mut state = get_state()?; + let mut state = LocalState::load()?; for service_name in service_names { let service = state @@ -24,7 +23,7 @@ pub fn remote(service_names: &[String]) -> Result<(), CliError> { service.current = ServiceTarget::Remote; } - save_state(state.clone())?; + state.save()?; load_server_states(state)?; println!( @@ -42,7 +41,7 @@ pub fn local(service_names: &[String]) -> Result<(), CliError> { )); } - let mut state = get_state()?; + let mut state = LocalState::load()?; for service_name in service_names { let service = state @@ -53,7 +52,7 @@ pub fn local(service_names: &[String]) -> Result<(), CliError> { service.current = ServiceTarget::Local; } - save_state(state.clone())?; + state.save()?; load_server_states(state)?; println!( diff --git a/linkup-cli/src/reset.rs b/linkup-cli/src/reset.rs index 7b63576..46fc39c 100644 --- a/linkup-cli/src/reset.rs +++ b/linkup-cli/src/reset.rs @@ -1,8 +1,8 @@ use crate::{ background_booting::boot_background_services, linkup_file_path, - local_config::{config_path, get_config}, - start::{boot_local_dns, get_state}, + local_config::{config_path, get_config, LocalState}, + start::boot_local_dns, stop::shutdown, CliError, LINKUP_LOCALDNS_INSTALL, }; @@ -10,7 +10,7 @@ use crate::{ // TODO(ostenbom)[2023-09-26]: Config arg shouldn't be needed here, we could use config state for this pub fn reset(config_arg: &Option) -> Result<(), CliError> { // Ensure there is some kind of state from before, otherwise reset doesn't make sense - get_state()?; + LocalState::load()?; shutdown()?; boot_background_services()?; diff --git a/linkup-cli/src/start.rs b/linkup-cli/src/start.rs index a799416..e55580a 100644 --- a/linkup-cli/src/start.rs +++ b/linkup-cli/src/start.rs @@ -1,5 +1,5 @@ use std::{ - fs::{self, File}, + fs, path::{Path, PathBuf}, }; @@ -10,12 +10,12 @@ use crate::{ linkup_file_path, local_config::{config_to_state, LocalState, YamlLocalConfig}, status::{server_status, ServerStatus}, - CliError, LINKUP_STATE_FILE, + CliError, }; use crate::{services, LINKUP_LOCALDNS_INSTALL}; pub fn start(config_arg: &Option) -> Result<(), CliError> { - let previous_state = get_state(); + let previous_state = LocalState::load(); let config_path = config_path(config_arg)?; let input_config = get_config(&config_path)?; @@ -30,7 +30,7 @@ pub fn start(config_arg: &Option) -> Result<(), CliError> { state.linkup.tunnel = ps.linkup.tunnel; } - save_state(state.clone())?; + state.save()?; // Set env vars to linkup for service in &state.services { @@ -51,42 +51,6 @@ pub fn start(config_arg: &Option) -> Result<(), CliError> { Ok(()) } -pub fn get_state() -> Result { - if let Err(e) = File::open(linkup_file_path(LINKUP_STATE_FILE)) { - return Err(CliError::NoState(e.to_string())); - } - - let content = match fs::read_to_string(linkup_file_path(LINKUP_STATE_FILE)) { - Ok(content) => content, - Err(e) => return Err(CliError::NoState(e.to_string())), - }; - - match serde_yaml::from_str(&content) { - Ok(config) => Ok(config), - Err(e) => Err(CliError::NoState(e.to_string())), - } -} - -pub fn save_state(state: LocalState) -> Result<(), CliError> { - let yaml_string = match serde_yaml::to_string(&state) { - Ok(yaml) => yaml, - Err(_) => { - return Err(CliError::SaveState( - "Failed to serialize the state into YAML".to_string(), - )) - } - }; - - if fs::write(linkup_file_path(LINKUP_STATE_FILE), yaml_string).is_err() { - return Err(CliError::SaveState(format!( - "Failed to write the state file at {}", - linkup_file_path(LINKUP_STATE_FILE).display() - ))); - } - - Ok(()) -} - fn set_service_env(directory: String, config_path: String) -> Result<(), CliError> { let config_dir = Path::new(&config_path).parent().ok_or_else(|| { CliError::SetServiceEnv( @@ -130,7 +94,7 @@ fn set_service_env(directory: String, config_path: String) -> Result<(), CliErro } fn check_local_not_started() -> Result<(), CliError> { - let state = get_state()?; + let state = LocalState::load()?; for service in state.services { if service.local == service.remote { continue; diff --git a/linkup-cli/src/status.rs b/linkup-cli/src/status.rs index ceb5b55..d50f424 100644 --- a/linkup-cli/src/status.rs +++ b/linkup-cli/src/status.rs @@ -5,7 +5,6 @@ use std::{thread, time::Duration}; use crate::{ local_config::{LocalState, ServiceTarget}, - start::get_state, CliError, LINKUP_LOCALSERVER_PORT, }; @@ -57,7 +56,7 @@ impl From> for ServerStatus } pub fn status(json: bool, all: bool) -> Result<(), CliError> { - let state = get_state()?; + let state = LocalState::load()?; let (tx, rx) = std::sync::mpsc::channel(); linkup_status(tx.clone(), &state); diff --git a/linkup-cli/src/stop.rs b/linkup-cli/src/stop.rs index bb67b1b..b7259f3 100644 --- a/linkup-cli/src/stop.rs +++ b/linkup-cli/src/stop.rs @@ -4,8 +4,8 @@ use std::path::{Path, PathBuf}; use nix::sys::signal::Signal; use crate::env_files::clear_env_file; +use crate::local_config::LocalState; use crate::signal::{get_pid, send_signal, PidError}; -use crate::start::get_state; use crate::{ linkup_file_path, services, CliError, LINKUP_CLOUDFLARED_PID, LINKUP_LOCALDNS_INSTALL, LINKUP_LOCALSERVER_PID_FILE, @@ -13,7 +13,7 @@ use crate::{ pub fn stop() -> Result<(), CliError> { // Reset env vars back to what they were before - let state = get_state()?; + let state = LocalState::load()?; for service in &state.services { let remove_res = match &service.directory { Some(d) => remove_service_env(d.clone(), state.linkup.config_path.clone()), From 913e3e9a27b90b8e7a3a11e33ea10fdac35bb39f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augusto=20C=C3=A9sar?= Date: Fri, 17 Nov 2023 15:51:17 +0100 Subject: [PATCH 2/8] refactor: create constants module and shuffle imports --- linkup-cli/src/background_booting.rs | 5 ++--- linkup-cli/src/background_local_server.rs | 4 ++-- linkup-cli/src/background_tunnel.rs | 2 +- linkup-cli/src/constants.rs | 8 ++++++++ linkup-cli/src/env_files.rs | 6 ++---- linkup-cli/src/local_config.rs | 10 ++++------ linkup-cli/src/local_dns.rs | 16 ++++++---------- linkup-cli/src/local_server.rs | 2 +- linkup-cli/src/main.rs | 12 ++---------- linkup-cli/src/preview.rs | 3 ++- linkup-cli/src/remote_local.rs | 9 ++++----- linkup-cli/src/reset.rs | 14 ++++++-------- linkup-cli/src/services/caddy.rs | 13 +++++-------- linkup-cli/src/services/dnsmasq.rs | 3 ++- linkup-cli/src/signal.rs | 5 +++-- linkup-cli/src/start.rs | 14 +++++--------- linkup-cli/src/status.rs | 10 +++++----- linkup-cli/src/stop.rs | 8 ++++---- 18 files changed, 64 insertions(+), 80 deletions(-) create mode 100644 linkup-cli/src/constants.rs diff --git a/linkup-cli/src/background_booting.rs b/linkup-cli/src/background_booting.rs index 38e0d71..a35da20 100644 --- a/linkup-cli/src/background_booting.rs +++ b/linkup-cli/src/background_booting.rs @@ -1,20 +1,19 @@ use std::thread; use std::time::{Duration, Instant}; -use reqwest::StatusCode; - use linkup::{StorableService, StorableSession, UpdateSessionRequest}; +use reqwest::StatusCode; use url::Url; use crate::background_local_server::{ is_local_server_started, is_tunnel_started, start_local_server, }; use crate::background_tunnel::start_tunnel; +use crate::constants::LINKUP_LOCALSERVER_PORT; use crate::local_config::{LocalState, ServiceTarget}; use crate::status::print_session_names; use crate::worker_client::WorkerClient; use crate::CliError; -use crate::LINKUP_LOCALSERVER_PORT; pub fn boot_background_services() -> Result<(), CliError> { let mut state = LocalState::load()?; diff --git a/linkup-cli/src/background_local_server.rs b/linkup-cli/src/background_local_server.rs index 3d09c13..922181e 100644 --- a/linkup-cli/src/background_local_server.rs +++ b/linkup-cli/src/background_local_server.rs @@ -5,9 +5,9 @@ use std::sync::Once; use daemonize::{Daemonize, Outcome}; use thiserror::Error; +use crate::constants::{LINKUP_CLOUDFLARED_PID, LINKUP_LOCALSERVER_PID_FILE}; use crate::local_server::local_linkup_main; -use crate::LINKUP_CLOUDFLARED_PID; -use crate::{linkup_file_path, CliError, LINKUP_LOCALSERVER_PID_FILE}; +use crate::{linkup_file_path, CliError}; const LINKUP_LOCALSERVER_STDOUT: &str = "localserver-stdout"; const LINKUP_LOCALSERVER_STDERR: &str = "localserver-stderr"; diff --git a/linkup-cli/src/background_tunnel.rs b/linkup-cli/src/background_tunnel.rs index 2ff25a9..1880ec6 100644 --- a/linkup-cli/src/background_tunnel.rs +++ b/linkup-cli/src/background_tunnel.rs @@ -12,9 +12,9 @@ use url::Url; use crate::signal::send_signal; +use crate::constants::{LINKUP_CLOUDFLARED_PID, LINKUP_LOCALSERVER_PORT}; use crate::stop::stop_pid_file; use crate::{linkup_file_path, CliError}; -use crate::{LINKUP_CLOUDFLARED_PID, LINKUP_LOCALSERVER_PORT}; const LINKUP_CLOUDFLARED_STDOUT: &str = "cloudflared-stdout"; const LINKUP_CLOUDFLARED_STDERR: &str = "cloudflared-stderr"; diff --git a/linkup-cli/src/constants.rs b/linkup-cli/src/constants.rs new file mode 100644 index 0000000..02b7078 --- /dev/null +++ b/linkup-cli/src/constants.rs @@ -0,0 +1,8 @@ +pub const LINKUP_CONFIG_ENV: &str = "LINKUP_CONFIG"; +pub const LINKUP_LOCALSERVER_PORT: u16 = 9066; +pub const LINKUP_DIR: &str = ".linkup"; +pub const LINKUP_STATE_FILE: &str = "state"; +pub const LINKUP_LOCALSERVER_PID_FILE: &str = "localserver-pid"; +pub const LINKUP_CLOUDFLARED_PID: &str = "cloudflared-pid"; +pub const LINKUP_LOCALDNS_INSTALL: &str = "localdns-install"; +pub const LINKUP_CF_TLS_API_ENV_VAR: &str = "LINKUP_CF_API_TOKEN"; diff --git a/linkup-cli/src/env_files.rs b/linkup-cli/src/env_files.rs index d1eaea7..a2cf3d2 100644 --- a/linkup-cli/src/env_files.rs +++ b/linkup-cli/src/env_files.rs @@ -1,8 +1,6 @@ +use std::fs::{self, OpenOptions}; use std::io::Write; -use std::{ - fs::{self, OpenOptions}, - path::PathBuf, -}; +use std::path::PathBuf; use crate::{CliError, Result}; diff --git a/linkup-cli/src/local_config.rs b/linkup-cli/src/local_config.rs index 793bc7c..9c88d40 100644 --- a/linkup-cli/src/local_config.rs +++ b/linkup-cli/src/local_config.rs @@ -1,8 +1,5 @@ -use std::{ - env, - fmt::{self, Display, Formatter}, - fs, -}; +use std::fmt::{self, Display, Formatter}; +use std::{env, fs}; use rand::{distributions::Alphanumeric, Rng}; use serde::{Deserialize, Serialize}; @@ -10,7 +7,8 @@ use url::Url; use linkup::{CreatePreviewRequest, StorableDomain, StorableRewrite, StorableService}; -use crate::{linkup_file_path, CliError, LINKUP_CONFIG_ENV, LINKUP_STATE_FILE}; +use crate::constants::{LINKUP_CONFIG_ENV, LINKUP_STATE_FILE}; +use crate::{linkup_file_path, CliError}; #[derive(Deserialize, Serialize, Clone)] pub struct LocalState { diff --git a/linkup-cli/src/local_dns.rs b/linkup-cli/src/local_dns.rs index 09b800e..3ce9af0 100644 --- a/linkup-cli/src/local_dns.rs +++ b/linkup-cli/src/local_dns.rs @@ -1,13 +1,9 @@ -use std::{ - fs, - process::{Command, Stdio}, -}; - -use crate::{ - linkup_file_path, - local_config::{config_path, get_config}, - services, CliError, Result, LINKUP_CF_TLS_API_ENV_VAR, LINKUP_LOCALDNS_INSTALL, -}; +use std::fs; +use std::process::{Command, Stdio}; + +use crate::constants::{LINKUP_CF_TLS_API_ENV_VAR, LINKUP_LOCALDNS_INSTALL}; +use crate::local_config::{config_path, get_config}; +use crate::{linkup_file_path, services, CliError, Result}; pub fn install(config_arg: &Option) -> Result<()> { if std::env::var(LINKUP_CF_TLS_API_ENV_VAR).is_err() { diff --git a/linkup-cli/src/local_server.rs b/linkup-cli/src/local_server.rs index 3152821..9d3310b 100644 --- a/linkup-cli/src/local_server.rs +++ b/linkup-cli/src/local_server.rs @@ -9,7 +9,7 @@ use thiserror::Error; use linkup::{HeaderMap as LinkupHeaderMap, HeaderName as LinkupHeaderName, *}; -use crate::LINKUP_LOCALSERVER_PORT; +use crate::constants::LINKUP_LOCALSERVER_PORT; #[derive(Error, Debug)] pub enum ProxyError { diff --git a/linkup-cli/src/main.rs b/linkup-cli/src/main.rs index ef4a06f..c6a8499 100644 --- a/linkup-cli/src/main.rs +++ b/linkup-cli/src/main.rs @@ -8,6 +8,7 @@ mod background_booting; mod background_local_server; mod background_tunnel; mod completion; +mod constants; mod env_files; mod local_config; mod local_dns; @@ -30,15 +31,6 @@ use start::start; use status::status; use stop::stop; -const LINKUP_CONFIG_ENV: &str = "LINKUP_CONFIG"; -const LINKUP_LOCALSERVER_PORT: u16 = 9066; -const LINKUP_DIR: &str = ".linkup"; -const LINKUP_STATE_FILE: &str = "state"; -const LINKUP_LOCALSERVER_PID_FILE: &str = "localserver-pid"; -const LINKUP_CLOUDFLARED_PID: &str = "cloudflared-pid"; -const LINKUP_LOCALDNS_INSTALL: &str = "localdns-install"; -const LINKUP_CF_TLS_API_ENV_VAR: &str = "LINKUP_CF_API_TOKEN"; - pub fn linkup_dir_path() -> PathBuf { let storage_dir = match env::var("HOME") { Ok(val) => val, @@ -47,7 +39,7 @@ pub fn linkup_dir_path() -> PathBuf { let mut path = PathBuf::new(); path.push(storage_dir); - path.push(LINKUP_DIR); + path.push(constants::LINKUP_DIR); path } diff --git a/linkup-cli/src/preview.rs b/linkup-cli/src/preview.rs index 408d040..a1490b8 100644 --- a/linkup-cli/src/preview.rs +++ b/linkup-cli/src/preview.rs @@ -1,8 +1,9 @@ +use linkup::CreatePreviewRequest; + use crate::local_config::{config_path, get_config}; use crate::status::{format_state_domains, print_session_status, SessionStatus}; use crate::worker_client::WorkerClient; use crate::CliError; -use linkup::CreatePreviewRequest; pub fn preview( config: &Option, diff --git a/linkup-cli/src/remote_local.rs b/linkup-cli/src/remote_local.rs index 1afd86d..77430fe 100644 --- a/linkup-cli/src/remote_local.rs +++ b/linkup-cli/src/remote_local.rs @@ -1,10 +1,9 @@ use url::Url; -use crate::{ - background_booting::{load_config, server_config_from_state}, - local_config::{LocalState, ServiceTarget}, - CliError, LINKUP_LOCALSERVER_PORT, -}; +use crate::background_booting::{load_config, server_config_from_state}; +use crate::constants::LINKUP_LOCALSERVER_PORT; +use crate::local_config::{LocalState, ServiceTarget}; +use crate::CliError; pub fn remote(service_names: &[String]) -> Result<(), CliError> { if service_names.is_empty() { diff --git a/linkup-cli/src/reset.rs b/linkup-cli/src/reset.rs index 46fc39c..3f80141 100644 --- a/linkup-cli/src/reset.rs +++ b/linkup-cli/src/reset.rs @@ -1,11 +1,9 @@ -use crate::{ - background_booting::boot_background_services, - linkup_file_path, - local_config::{config_path, get_config, LocalState}, - start::boot_local_dns, - stop::shutdown, - CliError, LINKUP_LOCALDNS_INSTALL, -}; +use crate::background_booting::boot_background_services; +use crate::constants::LINKUP_LOCALDNS_INSTALL; +use crate::local_config::{config_path, get_config, LocalState}; +use crate::start::boot_local_dns; +use crate::stop::shutdown; +use crate::{linkup_file_path, CliError}; // TODO(ostenbom)[2023-09-26]: Config arg shouldn't be needed here, we could use config state for this pub fn reset(config_arg: &Option) -> Result<(), CliError> { diff --git a/linkup-cli/src/services/caddy.rs b/linkup-cli/src/services/caddy.rs index dfe6ea7..b066457 100644 --- a/linkup-cli/src/services/caddy.rs +++ b/linkup-cli/src/services/caddy.rs @@ -1,12 +1,9 @@ -use std::{ - fs, - process::{Command, Stdio}, -}; +use std::fs; +use std::process::{Command, Stdio}; -use crate::{ - linkup_dir_path, linkup_file_path, local_config::YamlLocalConfig, CliError, Result, - LINKUP_CF_TLS_API_ENV_VAR, LINKUP_LOCALSERVER_PORT, -}; +use crate::constants::{LINKUP_CF_TLS_API_ENV_VAR, LINKUP_LOCALSERVER_PORT}; +use crate::local_config::YamlLocalConfig; +use crate::{linkup_dir_path, linkup_file_path, CliError, Result}; const CADDYFILE: &str = "Caddyfile"; const PID_FILE: &str = "caddy-pid"; diff --git a/linkup-cli/src/services/dnsmasq.rs b/linkup-cli/src/services/dnsmasq.rs index 4e8cf4e..f3c5790 100644 --- a/linkup-cli/src/services/dnsmasq.rs +++ b/linkup-cli/src/services/dnsmasq.rs @@ -6,7 +6,8 @@ use std::{ use nix::sys::signal::Signal; -use crate::{linkup_dir_path, linkup_file_path, stop::stop_pid_file, CliError, Result}; +use crate::stop::stop_pid_file; +use crate::{linkup_dir_path, linkup_file_path, CliError, Result}; const PORT: u16 = 8053; const CONF_FILE: &str = "dnsmasq-conf"; diff --git a/linkup-cli/src/signal.rs b/linkup-cli/src/signal.rs index 7051693..ebd8e1e 100644 --- a/linkup-cli/src/signal.rs +++ b/linkup-cli/src/signal.rs @@ -1,8 +1,9 @@ -use nix::sys::signal::{kill, Signal}; -use nix::unistd::Pid; use std::fs::{self, File}; use std::path::Path; use std::str::FromStr; + +use nix::sys::signal::{kill, Signal}; +use nix::unistd::Pid; use thiserror::Error; #[derive(Error, Debug)] diff --git a/linkup-cli/src/start.rs b/linkup-cli/src/start.rs index e55580a..3758e6d 100644 --- a/linkup-cli/src/start.rs +++ b/linkup-cli/src/start.rs @@ -3,16 +3,12 @@ use std::{ path::{Path, PathBuf}, }; +use crate::background_booting::boot_background_services; +use crate::constants::LINKUP_LOCALDNS_INSTALL; use crate::env_files::write_to_env_file; -use crate::local_config::{config_path, get_config}; -use crate::{ - background_booting::boot_background_services, - linkup_file_path, - local_config::{config_to_state, LocalState, YamlLocalConfig}, - status::{server_status, ServerStatus}, - CliError, -}; -use crate::{services, LINKUP_LOCALDNS_INSTALL}; +use crate::local_config::{config_path, config_to_state, get_config, LocalState, YamlLocalConfig}; +use crate::status::{server_status, ServerStatus}; +use crate::{linkup_file_path, services, CliError}; pub fn start(config_arg: &Option) -> Result<(), CliError> { let previous_state = LocalState::load(); diff --git a/linkup-cli/src/status.rs b/linkup-cli/src/status.rs index d50f424..31bc8a5 100644 --- a/linkup-cli/src/status.rs +++ b/linkup-cli/src/status.rs @@ -1,12 +1,12 @@ +use std::{thread, time::Duration}; + use colored::{ColoredString, Colorize}; use linkup::StorableDomain; use serde::{Deserialize, Serialize}; -use std::{thread, time::Duration}; -use crate::{ - local_config::{LocalState, ServiceTarget}, - CliError, LINKUP_LOCALSERVER_PORT, -}; +use crate::constants::LINKUP_LOCALSERVER_PORT; +use crate::local_config::{LocalState, ServiceTarget}; +use crate::CliError; #[derive(Deserialize, Serialize)] struct Status { diff --git a/linkup-cli/src/stop.rs b/linkup-cli/src/stop.rs index b7259f3..18c4b02 100644 --- a/linkup-cli/src/stop.rs +++ b/linkup-cli/src/stop.rs @@ -3,13 +3,13 @@ use std::path::{Path, PathBuf}; use nix::sys::signal::Signal; +use crate::constants::{ + LINKUP_CLOUDFLARED_PID, LINKUP_LOCALDNS_INSTALL, LINKUP_LOCALSERVER_PID_FILE, +}; use crate::env_files::clear_env_file; use crate::local_config::LocalState; use crate::signal::{get_pid, send_signal, PidError}; -use crate::{ - linkup_file_path, services, CliError, LINKUP_CLOUDFLARED_PID, LINKUP_LOCALDNS_INSTALL, - LINKUP_LOCALSERVER_PID_FILE, -}; +use crate::{linkup_file_path, services, CliError}; pub fn stop() -> Result<(), CliError> { // Reset env vars back to what they were before From 7a38173ab2fabdebb7f2c6358fb96565042b0a1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augusto=20C=C3=A9sar?= Date: Mon, 20 Nov 2023 07:56:55 +0100 Subject: [PATCH 3/8] refactor: breakdown server config into local and remote --- linkup-cli/src/background_booting.rs | 95 ++++++++++++++++------------ linkup-cli/src/remote_local.rs | 9 +-- linkup/src/session.rs | 2 +- 3 files changed, 62 insertions(+), 44 deletions(-) diff --git a/linkup-cli/src/background_booting.rs b/linkup-cli/src/background_booting.rs index a35da20..62e60ec 100644 --- a/linkup-cli/src/background_booting.rs +++ b/linkup-cli/src/background_booting.rs @@ -38,14 +38,14 @@ pub fn boot_background_services() -> Result<(), CliError> { println!("Cloudflare tunnel was already running.. Try stopping linkup first if you have problems."); } - let (local_server_conf, remote_server_conf) = server_config_from_state(&state); + let server_config = ServerConfig::from(&state); let server_session_name = load_config( &state.linkup.remote, &state.linkup.session_name, - remote_server_conf, + server_config.remote, )?; - let local_session_name = load_config(&local_url, &server_session_name, local_server_conf)?; + let local_session_name = load_config(&local_url, &server_session_name, server_config.local)?; if server_session_name != local_session_name { return Err(CliError::InconsistentState); @@ -78,7 +78,7 @@ pub fn load_config( ) -> Result { let session_update_req = UpdateSessionRequest { session_token: config.session_token, - desired_name: desired_name.into(), + desired_name: desired_name.to_string(), services: config.services, domains: config.domains, cache_routes: config.cache_routes, @@ -91,49 +91,66 @@ pub fn load_config( Ok(content) } -pub fn server_config_from_state(state: &LocalState) -> (StorableSession, StorableSession) { - let local_server_services = state - .services - .iter() - .map(|local_service| StorableService { - name: local_service.name.clone(), - location: if local_service.current == ServiceTarget::Remote { - local_service.remote.clone() - } else { - local_service.local.clone() - }, - rewrites: Some(local_service.rewrites.clone()), - }) - .collect::>(); - - let remote_server_services = state - .services - .iter() - .map(|local_service| StorableService { - name: local_service.name.clone(), - location: if local_service.current == ServiceTarget::Remote { - local_service.remote.clone() - } else { - state.linkup.tunnel.clone() - }, - rewrites: Some(local_service.rewrites.clone()), - }) - .collect::>(); - - ( - StorableSession { +pub struct ServerConfig { + pub local: StorableSession, + pub remote: StorableSession, +} + +impl From<&LocalState> for ServerConfig { + fn from(state: &LocalState) -> Self { + let local_server_services = state + .services + .iter() + .map(|local_service| StorableService { + name: local_service.name.clone(), + location: if local_service.current == ServiceTarget::Remote { + local_service.remote.clone() + } else { + local_service.local.clone() + }, + rewrites: Some(local_service.rewrites.clone()), + }) + .collect::>(); + + let remote_server_services = state + .services + .iter() + .map(|local_service| StorableService { + name: local_service.name.clone(), + location: if local_service.current == ServiceTarget::Remote { + local_service.remote.clone() + } else { + state.linkup.tunnel.clone() + }, + rewrites: Some(local_service.rewrites.clone()), + }) + .collect::>(); + + let local_storable_session = StorableSession { session_token: state.linkup.session_token.clone(), services: local_server_services, domains: state.domains.clone(), cache_routes: state.linkup.cache_routes.clone(), - }, - StorableSession { + }; + + let remote_storable_session = StorableSession { session_token: state.linkup.session_token.clone(), services: remote_server_services, domains: state.domains.clone(), cache_routes: state.linkup.cache_routes.clone(), - }, - ) + }; + + ServerConfig { + local: local_storable_session, + remote: remote_storable_session, + } + } +} + +impl<'a> From<&'a ServerConfig> for (&'a StorableSession, &'a StorableSession) { + fn from(config: &'a ServerConfig) -> Self { + (&config.local, &config.remote) + } } pub fn wait_till_ok(url: String) -> Result<(), CliError> { diff --git a/linkup-cli/src/remote_local.rs b/linkup-cli/src/remote_local.rs index 77430fe..ded139b 100644 --- a/linkup-cli/src/remote_local.rs +++ b/linkup-cli/src/remote_local.rs @@ -1,6 +1,6 @@ use url::Url; -use crate::background_booting::{load_config, server_config_from_state}; +use crate::background_booting::{load_config, ServerConfig}; use crate::constants::LINKUP_LOCALSERVER_PORT; use crate::local_config::{LocalState, ServiceTarget}; use crate::CliError; @@ -66,13 +66,14 @@ fn load_server_states(state: LocalState) -> Result<(), CliError> { let local_url = Url::parse(&format!("http://localhost:{}", LINKUP_LOCALSERVER_PORT)) .expect("linkup url invalid"); - let (local_server_conf, remote_server_conf) = server_config_from_state(&state); + let server_config = ServerConfig::from(&state); + let _ = load_config( &state.linkup.remote, &state.linkup.session_name.clone(), - remote_server_conf, + server_config.remote, )?; - let _ = load_config(&local_url, &state.linkup.session_name, local_server_conf)?; + let _ = load_config(&local_url, &state.linkup.session_name, server_config.local)?; Ok(()) } diff --git a/linkup/src/session.rs b/linkup/src/session.rs index f63a363..25a7ee2 100644 --- a/linkup/src/session.rs +++ b/linkup/src/session.rs @@ -64,7 +64,7 @@ pub struct StorableSession { pub cache_routes: Option>, } -#[derive(Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct StorableService { pub name: String, pub location: Url, From 9b272bc1644269ea6c84a8a02bc31fb8b672d9b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augusto=20C=C3=A9sar?= Date: Mon, 20 Nov 2023 08:07:37 +0100 Subject: [PATCH 4/8] Revert "refactor: create constants module and shuffle imports" This reverts commit 913e3e9a27b90b8e7a3a11e33ea10fdac35bb39f. # Conflicts: # linkup-cli/src/remote_local.rs --- linkup-cli/src/background_booting.rs | 5 +++-- linkup-cli/src/background_local_server.rs | 4 ++-- linkup-cli/src/background_tunnel.rs | 2 +- linkup-cli/src/constants.rs | 8 -------- linkup-cli/src/env_files.rs | 6 ++++-- linkup-cli/src/local_config.rs | 10 ++++++---- linkup-cli/src/local_dns.rs | 16 ++++++++++------ linkup-cli/src/local_server.rs | 2 +- linkup-cli/src/main.rs | 12 ++++++++++-- linkup-cli/src/preview.rs | 3 +-- linkup-cli/src/remote_local.rs | 9 +++++---- linkup-cli/src/reset.rs | 14 ++++++++------ linkup-cli/src/services/caddy.rs | 13 ++++++++----- linkup-cli/src/services/dnsmasq.rs | 3 +-- linkup-cli/src/signal.rs | 5 ++--- linkup-cli/src/start.rs | 14 +++++++++----- linkup-cli/src/status.rs | 10 +++++----- linkup-cli/src/stop.rs | 8 ++++---- 18 files changed, 80 insertions(+), 64 deletions(-) delete mode 100644 linkup-cli/src/constants.rs diff --git a/linkup-cli/src/background_booting.rs b/linkup-cli/src/background_booting.rs index 62e60ec..04dd61f 100644 --- a/linkup-cli/src/background_booting.rs +++ b/linkup-cli/src/background_booting.rs @@ -1,19 +1,20 @@ use std::thread; use std::time::{Duration, Instant}; -use linkup::{StorableService, StorableSession, UpdateSessionRequest}; use reqwest::StatusCode; + +use linkup::{StorableService, StorableSession, UpdateSessionRequest}; use url::Url; use crate::background_local_server::{ is_local_server_started, is_tunnel_started, start_local_server, }; use crate::background_tunnel::start_tunnel; -use crate::constants::LINKUP_LOCALSERVER_PORT; use crate::local_config::{LocalState, ServiceTarget}; use crate::status::print_session_names; use crate::worker_client::WorkerClient; use crate::CliError; +use crate::LINKUP_LOCALSERVER_PORT; pub fn boot_background_services() -> Result<(), CliError> { let mut state = LocalState::load()?; diff --git a/linkup-cli/src/background_local_server.rs b/linkup-cli/src/background_local_server.rs index 922181e..3d09c13 100644 --- a/linkup-cli/src/background_local_server.rs +++ b/linkup-cli/src/background_local_server.rs @@ -5,9 +5,9 @@ use std::sync::Once; use daemonize::{Daemonize, Outcome}; use thiserror::Error; -use crate::constants::{LINKUP_CLOUDFLARED_PID, LINKUP_LOCALSERVER_PID_FILE}; use crate::local_server::local_linkup_main; -use crate::{linkup_file_path, CliError}; +use crate::LINKUP_CLOUDFLARED_PID; +use crate::{linkup_file_path, CliError, LINKUP_LOCALSERVER_PID_FILE}; const LINKUP_LOCALSERVER_STDOUT: &str = "localserver-stdout"; const LINKUP_LOCALSERVER_STDERR: &str = "localserver-stderr"; diff --git a/linkup-cli/src/background_tunnel.rs b/linkup-cli/src/background_tunnel.rs index 1880ec6..2ff25a9 100644 --- a/linkup-cli/src/background_tunnel.rs +++ b/linkup-cli/src/background_tunnel.rs @@ -12,9 +12,9 @@ use url::Url; use crate::signal::send_signal; -use crate::constants::{LINKUP_CLOUDFLARED_PID, LINKUP_LOCALSERVER_PORT}; use crate::stop::stop_pid_file; use crate::{linkup_file_path, CliError}; +use crate::{LINKUP_CLOUDFLARED_PID, LINKUP_LOCALSERVER_PORT}; const LINKUP_CLOUDFLARED_STDOUT: &str = "cloudflared-stdout"; const LINKUP_CLOUDFLARED_STDERR: &str = "cloudflared-stderr"; diff --git a/linkup-cli/src/constants.rs b/linkup-cli/src/constants.rs deleted file mode 100644 index 02b7078..0000000 --- a/linkup-cli/src/constants.rs +++ /dev/null @@ -1,8 +0,0 @@ -pub const LINKUP_CONFIG_ENV: &str = "LINKUP_CONFIG"; -pub const LINKUP_LOCALSERVER_PORT: u16 = 9066; -pub const LINKUP_DIR: &str = ".linkup"; -pub const LINKUP_STATE_FILE: &str = "state"; -pub const LINKUP_LOCALSERVER_PID_FILE: &str = "localserver-pid"; -pub const LINKUP_CLOUDFLARED_PID: &str = "cloudflared-pid"; -pub const LINKUP_LOCALDNS_INSTALL: &str = "localdns-install"; -pub const LINKUP_CF_TLS_API_ENV_VAR: &str = "LINKUP_CF_API_TOKEN"; diff --git a/linkup-cli/src/env_files.rs b/linkup-cli/src/env_files.rs index a2cf3d2..d1eaea7 100644 --- a/linkup-cli/src/env_files.rs +++ b/linkup-cli/src/env_files.rs @@ -1,6 +1,8 @@ -use std::fs::{self, OpenOptions}; use std::io::Write; -use std::path::PathBuf; +use std::{ + fs::{self, OpenOptions}, + path::PathBuf, +}; use crate::{CliError, Result}; diff --git a/linkup-cli/src/local_config.rs b/linkup-cli/src/local_config.rs index 9c88d40..793bc7c 100644 --- a/linkup-cli/src/local_config.rs +++ b/linkup-cli/src/local_config.rs @@ -1,5 +1,8 @@ -use std::fmt::{self, Display, Formatter}; -use std::{env, fs}; +use std::{ + env, + fmt::{self, Display, Formatter}, + fs, +}; use rand::{distributions::Alphanumeric, Rng}; use serde::{Deserialize, Serialize}; @@ -7,8 +10,7 @@ use url::Url; use linkup::{CreatePreviewRequest, StorableDomain, StorableRewrite, StorableService}; -use crate::constants::{LINKUP_CONFIG_ENV, LINKUP_STATE_FILE}; -use crate::{linkup_file_path, CliError}; +use crate::{linkup_file_path, CliError, LINKUP_CONFIG_ENV, LINKUP_STATE_FILE}; #[derive(Deserialize, Serialize, Clone)] pub struct LocalState { diff --git a/linkup-cli/src/local_dns.rs b/linkup-cli/src/local_dns.rs index 3ce9af0..09b800e 100644 --- a/linkup-cli/src/local_dns.rs +++ b/linkup-cli/src/local_dns.rs @@ -1,9 +1,13 @@ -use std::fs; -use std::process::{Command, Stdio}; - -use crate::constants::{LINKUP_CF_TLS_API_ENV_VAR, LINKUP_LOCALDNS_INSTALL}; -use crate::local_config::{config_path, get_config}; -use crate::{linkup_file_path, services, CliError, Result}; +use std::{ + fs, + process::{Command, Stdio}, +}; + +use crate::{ + linkup_file_path, + local_config::{config_path, get_config}, + services, CliError, Result, LINKUP_CF_TLS_API_ENV_VAR, LINKUP_LOCALDNS_INSTALL, +}; pub fn install(config_arg: &Option) -> Result<()> { if std::env::var(LINKUP_CF_TLS_API_ENV_VAR).is_err() { diff --git a/linkup-cli/src/local_server.rs b/linkup-cli/src/local_server.rs index 9d3310b..3152821 100644 --- a/linkup-cli/src/local_server.rs +++ b/linkup-cli/src/local_server.rs @@ -9,7 +9,7 @@ use thiserror::Error; use linkup::{HeaderMap as LinkupHeaderMap, HeaderName as LinkupHeaderName, *}; -use crate::constants::LINKUP_LOCALSERVER_PORT; +use crate::LINKUP_LOCALSERVER_PORT; #[derive(Error, Debug)] pub enum ProxyError { diff --git a/linkup-cli/src/main.rs b/linkup-cli/src/main.rs index c6a8499..ef4a06f 100644 --- a/linkup-cli/src/main.rs +++ b/linkup-cli/src/main.rs @@ -8,7 +8,6 @@ mod background_booting; mod background_local_server; mod background_tunnel; mod completion; -mod constants; mod env_files; mod local_config; mod local_dns; @@ -31,6 +30,15 @@ use start::start; use status::status; use stop::stop; +const LINKUP_CONFIG_ENV: &str = "LINKUP_CONFIG"; +const LINKUP_LOCALSERVER_PORT: u16 = 9066; +const LINKUP_DIR: &str = ".linkup"; +const LINKUP_STATE_FILE: &str = "state"; +const LINKUP_LOCALSERVER_PID_FILE: &str = "localserver-pid"; +const LINKUP_CLOUDFLARED_PID: &str = "cloudflared-pid"; +const LINKUP_LOCALDNS_INSTALL: &str = "localdns-install"; +const LINKUP_CF_TLS_API_ENV_VAR: &str = "LINKUP_CF_API_TOKEN"; + pub fn linkup_dir_path() -> PathBuf { let storage_dir = match env::var("HOME") { Ok(val) => val, @@ -39,7 +47,7 @@ pub fn linkup_dir_path() -> PathBuf { let mut path = PathBuf::new(); path.push(storage_dir); - path.push(constants::LINKUP_DIR); + path.push(LINKUP_DIR); path } diff --git a/linkup-cli/src/preview.rs b/linkup-cli/src/preview.rs index a1490b8..408d040 100644 --- a/linkup-cli/src/preview.rs +++ b/linkup-cli/src/preview.rs @@ -1,9 +1,8 @@ -use linkup::CreatePreviewRequest; - use crate::local_config::{config_path, get_config}; use crate::status::{format_state_domains, print_session_status, SessionStatus}; use crate::worker_client::WorkerClient; use crate::CliError; +use linkup::CreatePreviewRequest; pub fn preview( config: &Option, diff --git a/linkup-cli/src/remote_local.rs b/linkup-cli/src/remote_local.rs index ded139b..fe4772b 100644 --- a/linkup-cli/src/remote_local.rs +++ b/linkup-cli/src/remote_local.rs @@ -1,9 +1,10 @@ use url::Url; -use crate::background_booting::{load_config, ServerConfig}; -use crate::constants::LINKUP_LOCALSERVER_PORT; -use crate::local_config::{LocalState, ServiceTarget}; -use crate::CliError; +use crate::{ + background_booting::{load_config, ServerConfig}, + local_config::{LocalState, ServiceTarget}, + CliError, LINKUP_LOCALSERVER_PORT, +}; pub fn remote(service_names: &[String]) -> Result<(), CliError> { if service_names.is_empty() { diff --git a/linkup-cli/src/reset.rs b/linkup-cli/src/reset.rs index 3f80141..46fc39c 100644 --- a/linkup-cli/src/reset.rs +++ b/linkup-cli/src/reset.rs @@ -1,9 +1,11 @@ -use crate::background_booting::boot_background_services; -use crate::constants::LINKUP_LOCALDNS_INSTALL; -use crate::local_config::{config_path, get_config, LocalState}; -use crate::start::boot_local_dns; -use crate::stop::shutdown; -use crate::{linkup_file_path, CliError}; +use crate::{ + background_booting::boot_background_services, + linkup_file_path, + local_config::{config_path, get_config, LocalState}, + start::boot_local_dns, + stop::shutdown, + CliError, LINKUP_LOCALDNS_INSTALL, +}; // TODO(ostenbom)[2023-09-26]: Config arg shouldn't be needed here, we could use config state for this pub fn reset(config_arg: &Option) -> Result<(), CliError> { diff --git a/linkup-cli/src/services/caddy.rs b/linkup-cli/src/services/caddy.rs index b066457..dfe6ea7 100644 --- a/linkup-cli/src/services/caddy.rs +++ b/linkup-cli/src/services/caddy.rs @@ -1,9 +1,12 @@ -use std::fs; -use std::process::{Command, Stdio}; +use std::{ + fs, + process::{Command, Stdio}, +}; -use crate::constants::{LINKUP_CF_TLS_API_ENV_VAR, LINKUP_LOCALSERVER_PORT}; -use crate::local_config::YamlLocalConfig; -use crate::{linkup_dir_path, linkup_file_path, CliError, Result}; +use crate::{ + linkup_dir_path, linkup_file_path, local_config::YamlLocalConfig, CliError, Result, + LINKUP_CF_TLS_API_ENV_VAR, LINKUP_LOCALSERVER_PORT, +}; const CADDYFILE: &str = "Caddyfile"; const PID_FILE: &str = "caddy-pid"; diff --git a/linkup-cli/src/services/dnsmasq.rs b/linkup-cli/src/services/dnsmasq.rs index f3c5790..4e8cf4e 100644 --- a/linkup-cli/src/services/dnsmasq.rs +++ b/linkup-cli/src/services/dnsmasq.rs @@ -6,8 +6,7 @@ use std::{ use nix::sys::signal::Signal; -use crate::stop::stop_pid_file; -use crate::{linkup_dir_path, linkup_file_path, CliError, Result}; +use crate::{linkup_dir_path, linkup_file_path, stop::stop_pid_file, CliError, Result}; const PORT: u16 = 8053; const CONF_FILE: &str = "dnsmasq-conf"; diff --git a/linkup-cli/src/signal.rs b/linkup-cli/src/signal.rs index ebd8e1e..7051693 100644 --- a/linkup-cli/src/signal.rs +++ b/linkup-cli/src/signal.rs @@ -1,9 +1,8 @@ +use nix::sys::signal::{kill, Signal}; +use nix::unistd::Pid; use std::fs::{self, File}; use std::path::Path; use std::str::FromStr; - -use nix::sys::signal::{kill, Signal}; -use nix::unistd::Pid; use thiserror::Error; #[derive(Error, Debug)] diff --git a/linkup-cli/src/start.rs b/linkup-cli/src/start.rs index 3758e6d..e55580a 100644 --- a/linkup-cli/src/start.rs +++ b/linkup-cli/src/start.rs @@ -3,12 +3,16 @@ use std::{ path::{Path, PathBuf}, }; -use crate::background_booting::boot_background_services; -use crate::constants::LINKUP_LOCALDNS_INSTALL; use crate::env_files::write_to_env_file; -use crate::local_config::{config_path, config_to_state, get_config, LocalState, YamlLocalConfig}; -use crate::status::{server_status, ServerStatus}; -use crate::{linkup_file_path, services, CliError}; +use crate::local_config::{config_path, get_config}; +use crate::{ + background_booting::boot_background_services, + linkup_file_path, + local_config::{config_to_state, LocalState, YamlLocalConfig}, + status::{server_status, ServerStatus}, + CliError, +}; +use crate::{services, LINKUP_LOCALDNS_INSTALL}; pub fn start(config_arg: &Option) -> Result<(), CliError> { let previous_state = LocalState::load(); diff --git a/linkup-cli/src/status.rs b/linkup-cli/src/status.rs index 31bc8a5..d50f424 100644 --- a/linkup-cli/src/status.rs +++ b/linkup-cli/src/status.rs @@ -1,12 +1,12 @@ -use std::{thread, time::Duration}; - use colored::{ColoredString, Colorize}; use linkup::StorableDomain; use serde::{Deserialize, Serialize}; +use std::{thread, time::Duration}; -use crate::constants::LINKUP_LOCALSERVER_PORT; -use crate::local_config::{LocalState, ServiceTarget}; -use crate::CliError; +use crate::{ + local_config::{LocalState, ServiceTarget}, + CliError, LINKUP_LOCALSERVER_PORT, +}; #[derive(Deserialize, Serialize)] struct Status { diff --git a/linkup-cli/src/stop.rs b/linkup-cli/src/stop.rs index 18c4b02..b7259f3 100644 --- a/linkup-cli/src/stop.rs +++ b/linkup-cli/src/stop.rs @@ -3,13 +3,13 @@ use std::path::{Path, PathBuf}; use nix::sys::signal::Signal; -use crate::constants::{ - LINKUP_CLOUDFLARED_PID, LINKUP_LOCALDNS_INSTALL, LINKUP_LOCALSERVER_PID_FILE, -}; use crate::env_files::clear_env_file; use crate::local_config::LocalState; use crate::signal::{get_pid, send_signal, PidError}; -use crate::{linkup_file_path, services, CliError}; +use crate::{ + linkup_file_path, services, CliError, LINKUP_CLOUDFLARED_PID, LINKUP_LOCALDNS_INSTALL, + LINKUP_LOCALSERVER_PID_FILE, +}; pub fn stop() -> Result<(), CliError> { // Reset env vars back to what they were before From 9de9c404b0531eca625f3c5b0f5d824b7494ca51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augusto=20C=C3=A9sar?= Date: Mon, 20 Nov 2023 08:18:20 +0100 Subject: [PATCH 5/8] lint: apply Clippy suggestions --- linkup-cli/src/start.rs | 2 +- linkup-cli/src/stop.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/linkup-cli/src/start.rs b/linkup-cli/src/start.rs index e55580a..cbb6cdb 100644 --- a/linkup-cli/src/start.rs +++ b/linkup-cli/src/start.rs @@ -61,7 +61,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) diff --git a/linkup-cli/src/stop.rs b/linkup-cli/src/stop.rs index b7259f3..77aca4b 100644 --- a/linkup-cli/src/stop.rs +++ b/linkup-cli/src/stop.rs @@ -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) From fef3dcfcfed4022d2d2a75606af675017141722c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augusto=20C=C3=A9sar?= Date: Mon, 20 Nov 2023 08:22:20 +0100 Subject: [PATCH 6/8] refactor: ensure state save has a mutable reference to it --- linkup-cli/src/local_config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linkup-cli/src/local_config.rs b/linkup-cli/src/local_config.rs index 793bc7c..736ab79 100644 --- a/linkup-cli/src/local_config.rs +++ b/linkup-cli/src/local_config.rs @@ -36,7 +36,7 @@ impl LocalState { } } - pub fn save(&self) -> Result<(), CliError> { + pub fn save(&mut self) -> Result<(), CliError> { let yaml_string = match serde_yaml::to_string(self) { Ok(yaml) => yaml, Err(_) => { From ea2f79257f77eaf373e9e440d0ac4381d436f85c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augusto=20C=C3=A9sar?= Date: Mon, 20 Nov 2023 08:22:54 +0100 Subject: [PATCH 7/8] refactor: remove unnecessary clones since now save use a reference --- linkup-cli/src/background_booting.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/linkup-cli/src/background_booting.rs b/linkup-cli/src/background_booting.rs index 04dd61f..122c92a 100644 --- a/linkup-cli/src/background_booting.rs +++ b/linkup-cli/src/background_booting.rs @@ -52,22 +52,21 @@ pub fn boot_background_services() -> Result<(), CliError> { return Err(CliError::InconsistentState); } - let tunnel_url = state.linkup.tunnel.clone(); - - state.linkup.session_name = server_session_name.clone(); - let state_to_print = state.clone(); - + state.linkup.session_name = server_session_name; state.save()?; - println!("Waiting for tunnel to be ready at {}...", tunnel_url); + println!( + "Waiting for tunnel to be ready at {}...", + &state.linkup.tunnel + ); // If the tunnel is checked too quickly, it dies ¯\_(ツ)_/¯ thread::sleep(Duration::from_millis(1000)); - wait_till_ok(format!("{}linkup-check", tunnel_url))?; + wait_till_ok(format!("{}linkup-check", &state.linkup.tunnel))?; println!(); - print_session_names(&state_to_print); + print_session_names(&state); Ok(()) } From 28055f6c01297c74abf2ad91937a0e44a45b1edf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augusto=20C=C3=A9sar?= Date: Mon, 20 Nov 2023 10:45:05 +0100 Subject: [PATCH 8/8] refactor: change name of variable on mapping --- linkup-cli/src/background_booting.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/linkup-cli/src/background_booting.rs b/linkup-cli/src/background_booting.rs index 122c92a..cf929bb 100644 --- a/linkup-cli/src/background_booting.rs +++ b/linkup-cli/src/background_booting.rs @@ -101,28 +101,28 @@ impl From<&LocalState> for ServerConfig { let local_server_services = state .services .iter() - .map(|local_service| StorableService { - name: local_service.name.clone(), - location: if local_service.current == ServiceTarget::Remote { - local_service.remote.clone() + .map(|service| StorableService { + name: service.name.clone(), + location: if service.current == ServiceTarget::Remote { + service.remote.clone() } else { - local_service.local.clone() + service.local.clone() }, - rewrites: Some(local_service.rewrites.clone()), + rewrites: Some(service.rewrites.clone()), }) .collect::>(); let remote_server_services = state .services .iter() - .map(|local_service| StorableService { - name: local_service.name.clone(), - location: if local_service.current == ServiceTarget::Remote { - local_service.remote.clone() + .map(|service| StorableService { + name: service.name.clone(), + location: if service.current == ServiceTarget::Remote { + service.remote.clone() } else { state.linkup.tunnel.clone() }, - rewrites: Some(local_service.rewrites.clone()), + rewrites: Some(service.rewrites.clone()), }) .collect::>();