diff --git a/linkup-cli/src/check.rs b/linkup-cli/src/check.rs index 55f4c3a..13eb34d 100644 --- a/linkup-cli/src/check.rs +++ b/linkup-cli/src/check.rs @@ -1,6 +1,6 @@ use std::fs::{self, OpenOptions}; use std::io::Write; -use std::path::Path; +use std::path::{Path, PathBuf}; use reqwest::blocking::Client; use reqwest::StatusCode; @@ -30,7 +30,7 @@ pub fn check() -> Result<(), CliError> { for service in &state.services { match &service.directory { - Some(d) => set_service_env(d.clone())?, + Some(d) => set_service_env(d.clone(), state.linkup.config_path.clone())?, None => {} } } @@ -140,9 +140,16 @@ fn server_config_from_state(state: &LocalState) -> (StorableSession, StorableSes ) } -fn set_service_env(directory: String) -> Result<(), CliError> { - let dev_env_path = format!("{}/.env.dev", directory); - let env_path = format!("{}/.env", directory); +fn set_service_env(directory: String, config_path: String) -> Result<(), CliError> { + let config_dir = Path::new(&config_path).parent().ok_or_else(|| { + CliError::SetServiceEnv( + directory.clone(), + "config_path does not have a parent directory".to_string(), + ) + })?; + + let dev_env_path = PathBuf::from(config_dir).join(&directory).join(".env.dev"); + let env_path = PathBuf::from(config_dir).join(&directory).join(".env"); if !Path::new(&dev_env_path).exists() { return Err(CliError::NoDevEnv(directory)); @@ -155,6 +162,12 @@ fn set_service_env(directory: String) -> Result<(), CliError> { ) })?; + if let Ok(env_content) = fs::read_to_string(&env_path) { + if env_content.contains(LINKUP_ENV_SEPARATOR) { + return Ok(()); + } + } + let mut env_file = OpenOptions::new() .create(true) .append(true) diff --git a/linkup-cli/src/local_config.rs b/linkup-cli/src/local_config.rs index d970107..ad625a1 100644 --- a/linkup-cli/src/local_config.rs +++ b/linkup-cli/src/local_config.rs @@ -17,6 +17,7 @@ pub struct LocalState { pub struct LinkupState { pub session_name: String, pub session_token: String, + pub config_path: String, pub remote: Url, pub tunnel: Url, } @@ -67,7 +68,7 @@ struct YamlLocalService { rewrites: Option>, } -pub fn config_to_state(yaml_config: YamlLocalConfig) -> LocalState { +pub fn config_to_state(yaml_config: YamlLocalConfig, config_path: String) -> LocalState { let random_token: String = rand::thread_rng() .sample_iter(&Alphanumeric) .take(16) @@ -77,6 +78,7 @@ pub fn config_to_state(yaml_config: YamlLocalConfig) -> LocalState { let linkup = LinkupState { session_name: String::new(), session_token: random_token, + config_path, remote: yaml_config.linkup.remote, tunnel: Url::parse("http://localhost").expect("default url parses"), }; @@ -143,7 +145,9 @@ domains: fn test_config_to_state() { let input_str = String::from(CONF_STR); let yaml_config = serde_yaml::from_str(&input_str).unwrap(); - let local_state = config_to_state(yaml_config); + let local_state = config_to_state(yaml_config, "./path/to/config.yaml".to_string()); + + assert_eq!(local_state.linkup.config_path, "./path/to/config.yaml"); assert_eq!( local_state.linkup.remote, diff --git a/linkup-cli/src/start.rs b/linkup-cli/src/start.rs index 5e615ff..41b7c9e 100644 --- a/linkup-cli/src/start.rs +++ b/linkup-cli/src/start.rs @@ -14,9 +14,10 @@ pub fn start(config_arg: Option) -> Result<(), CliError> { // TODO: run `stop` to kill the previous local server? let previous_state = get_state(); - let input_config = get_config(config_arg)?; + let config_path = config_path(config_arg)?; + let input_config = get_config(config_path.clone())?; - let mut state = config_to_state(input_config); + let mut state = config_to_state(input_config, config_path); // Reuse previous session name if possible if let Ok(ps) = previous_state { @@ -31,19 +32,20 @@ pub fn start(config_arg: Option) -> Result<(), CliError> { Ok(()) } -fn get_config(config_arg: Option) -> Result { - let config_path = - match config_arg { - Some(path) => path, - None => match env::var(LINKUP_CONFIG_ENV) { - Ok(val) => val, - Err(_) => return Err(CliError::NoConfig( - "No config argument provided and LINKUP_CONFIG environment variable not set" - .to_string(), - )), - }, - }; +fn config_path(config_arg: Option) -> Result { + match config_arg { + Some(path) => Ok(path), + None => match env::var(LINKUP_CONFIG_ENV) { + Ok(val) => Ok(val), + Err(_) => Err(CliError::NoConfig( + "No config argument provided and LINKUP_CONFIG environment variable not set" + .to_string(), + )), + }, + } +} +fn get_config(config_path: String) -> Result { let content = match fs::read_to_string(&config_path) { Ok(content) => content, Err(_) => {