Skip to content

Commit

Permalink
Set .env.dev files relative to config file dir
Browse files Browse the repository at this point in the history
  • Loading branch information
ostenbom committed May 11, 2023
1 parent 658adc8 commit e1f5f35
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
23 changes: 18 additions & 5 deletions linkup-cli/src/check.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 => {}
}
}
Expand Down Expand Up @@ -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));
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions linkup-cli/src/local_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -67,7 +68,7 @@ struct YamlLocalService {
rewrites: Option<Vec<StorableRewrite>>,
}

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)
Expand All @@ -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"),
};
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 16 additions & 14 deletions linkup-cli/src/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ pub fn start(config_arg: Option<String>) -> 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 {
Expand All @@ -31,19 +32,20 @@ pub fn start(config_arg: Option<String>) -> Result<(), CliError> {
Ok(())
}

fn get_config(config_arg: Option<String>) -> Result<YamlLocalConfig, CliError> {
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<String>) -> Result<String, CliError> {
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<YamlLocalConfig, CliError> {
let content = match fs::read_to_string(&config_path) {
Ok(content) => content,
Err(_) => {
Expand Down

0 comments on commit e1f5f35

Please sign in to comment.