Skip to content

Commit

Permalink
CHG use Vec<EnvVariable> instead of HashMap, for env vars (supports m…
Browse files Browse the repository at this point in the history
…ultiline in toml)
  • Loading branch information
synoet committed Oct 20, 2023
1 parent 3461ea4 commit 5424365
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
71 changes: 32 additions & 39 deletions src/cmd/run.rs
@@ -1,15 +1,8 @@
use super::super::config::{Config, EnvAlias};
use super::super::config::{Config, EnvAlias, EnvVariable};
use super::Shell;
use anyhow::{anyhow, Context, Result};
use std::collections::HashMap;
use std::path::Path;

#[derive(PartialEq, Eq, Debug)]
pub struct EnvVar {
pub key: String,
pub value: String,
}

fn trim_quotes(s: &str) -> String {
if s.len() < 2 {
return s.to_string();
Expand All @@ -22,7 +15,7 @@ fn trim_quotes(s: &str) -> String {
}
}

fn parse_env_file(content: &str, file_name: &str) -> Result<Vec<EnvVar>> {
fn parse_env_file(content: &str, file_name: &str) -> Result<Vec<EnvVariable>> {
let lines = content
.lines()
.filter(|line| !line.contains('#') && !line.trim().is_empty());
Expand All @@ -37,18 +30,18 @@ fn parse_env_file(content: &str, file_name: &str) -> Result<Vec<EnvVar>> {
let key = trim_quotes(split.0);
let value = trim_quotes(split.1);

vars.push(EnvVar {
key: key.to_string(),
vars.push(EnvVariable {
name: key.to_string(),
value: value.to_string(),
});
}

Ok(vars)
}

pub fn get_vars_to_set(config: &Config, new_path: &str) -> Result<Vec<EnvVar>> {
pub fn get_vars_to_set(config: &Config, new_path: &str) -> Result<Vec<EnvVariable>> {
// Env variables defined in the config
let mut variables: Vec<EnvVar> = vec![];
let mut variables: Vec<EnvVariable> = vec![];
variables.extend(
config
.directories
Expand All @@ -61,18 +54,18 @@ pub fn get_vars_to_set(config: &Config, new_path: &str) -> Result<Vec<EnvVar>> {
})
.flat_map(|dir| {
dir.vars
.unwrap_or(HashMap::new())
.unwrap_or(vec![])
.iter()
.map(|var| EnvVar {
key: var.0.clone(),
value: var.1.clone(),
.map(|var| EnvVariable {
name: var.name.clone(),
value: var.value.clone(),
})
.collect::<Vec<EnvVar>>()
.collect::<Vec<EnvVariable>>()
})
.collect::<Vec<EnvVar>>(),
.collect::<Vec<EnvVariable>>(),
);

let mut file_vars: Vec<EnvVar> = vec![];
let mut file_vars: Vec<EnvVariable> = vec![];

// Env variabled defined in the file specified in the config
for dir in config.directories.clone().into_iter() {
Expand Down Expand Up @@ -104,14 +97,14 @@ pub fn get_vars_to_set(config: &Config, new_path: &str) -> Result<Vec<EnvVar>> {
path.starts_with(base_path) || base_path == path
})
})
.map(|var| EnvVar {
key: var.name,
.map(|var| EnvVariable {
name: var.name,
value: var.value,
})
.collect::<Vec<EnvVar>>(),
.collect::<Vec<EnvVariable>>(),
);

let mut ext_file_vars: Vec<EnvVar> = vec![];
let mut ext_file_vars: Vec<EnvVariable> = vec![];

let matched_files = config
.files
Expand Down Expand Up @@ -146,7 +139,7 @@ pub fn get_vars_to_unset(config: &Config, old_path: &str) -> Vec<String> {
get_vars_to_set(config, old_path)
.unwrap_or(vec![])
.iter()
.map(|var| var.key.clone())
.map(|var| var.name.clone())
.collect()
}

Expand Down Expand Up @@ -260,15 +253,15 @@ pub fn run(config: &Config, old_path: String, new_path: String) -> Result<()> {
gray_start,
to_set
.iter()
.map(|var| var.key.clone())
.map(|var| var.name.clone())
.collect::<Vec<String>>()
.join(", "),
gray_end
);
}

for var in to_set {
println!("export {}=\"{}\"", var.key, var.value);
println!("export {}=\"{}\"", var.name, var.value);
}

let commands = get_commands_to_run(&config, &new_path);
Expand Down Expand Up @@ -347,7 +340,7 @@ mod tests {
#[test]
fn test_parse_env_file() {
use super::parse_env_file;
use super::EnvVar;
use crate::config::EnvVariable;

let test_content = "\
# THIS IS A TEST COMMMENT\n\
Expand All @@ -359,25 +352,25 @@ mod tests {
ANOTHER_VAR=hello world this is a test\n\
";

let expected: Vec<EnvVar> = vec![
EnvVar {
key: "TEST_VAR".to_string(),
let expected: Vec<EnvVariable> = vec![
EnvVariable {
name: "TEST_VAR".to_string(),
value: "true".to_string(),
},
EnvVar {
key: "ANOTHER_VAR".to_string(),
EnvVariable {
name: "ANOTHER_VAR".to_string(),
value: "123".to_string(),
},
EnvVar {
key: "QUOTED_VAR".to_string(),
EnvVariable {
name: "QUOTED_VAR".to_string(),
value: "test".to_string(),
},
EnvVar {
key: "SINGLE_QUOTED_VAR".to_string(),
EnvVariable {
name: "SINGLE_QUOTED_VAR".to_string(),
value: "test".to_string(),
},
EnvVar {
key: "ANOTHER_VAR".to_string(),
EnvVariable {
name: "ANOTHER_VAR".to_string(),
value: "hello world this is a test".to_string(),
},
];
Expand Down
13 changes: 9 additions & 4 deletions src/config.rs
@@ -1,15 +1,14 @@
use crate::cmd::Shell;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Config {
pub config: Option<GlobalConfig>,
#[serde(rename = "directory")]
pub directories: Vec<EnvDirectory>,
#[serde(rename = "env_variable")]
pub variables: Option<Vec<EnvVariable>>,
pub variables: Option<Vec<DirEnvVariable>>,
#[serde(rename = "command")]
pub commands: Option<Vec<EnvCommand>>,
#[serde(rename = "env_file")]
Expand Down Expand Up @@ -77,7 +76,7 @@ impl Default for GlobalConfig {
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct EnvDirectory {
pub path: String,
pub vars: Option<HashMap<String, String>>,
pub vars: Option<Vec<EnvVariable>>,
pub load_from: Option<Vec<String>>,
pub run: Option<Vec<String>>,
pub aliases: Option<Vec<EnvAlias>>,
Expand All @@ -97,12 +96,18 @@ pub struct DirectoryEnvAlias {
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct EnvVariable {
pub struct DirEnvVariable {
pub name: String,
pub value: String,
pub dirs: Vec<String>,
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct EnvVariable {
pub name: String,
pub value: String,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct EnvCommand {
pub run: String,
Expand Down

0 comments on commit 5424365

Please sign in to comment.