Skip to content

Commit

Permalink
ADD support both hashmap and EnvVariable for vars in dir
Browse files Browse the repository at this point in the history
  • Loading branch information
synoet committed Nov 4, 2023
1 parent 5424365 commit 5ffea5b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
13 changes: 2 additions & 11 deletions src/cmd/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::config::{Config, EnvAlias, EnvVariable};
use super::super::config::{Config, EnvAlias, EnvVariable, EnvVariableStruct, EnvVariableVec};
use super::Shell;
use anyhow::{anyhow, Context, Result};
use std::path::Path;
Expand Down Expand Up @@ -52,16 +52,7 @@ pub fn get_vars_to_set(config: &Config, new_path: &str) -> Result<Vec<EnvVariabl
let path = Path::new(new_path);
path.starts_with(path_to_check) || path_to_check == path
})
.flat_map(|dir| {
dir.vars
.unwrap_or(vec![])
.iter()
.map(|var| EnvVariable {
name: var.name.clone(),
value: var.value.clone(),
})
.collect::<Vec<EnvVariable>>()
})
.flat_map(|dir| EnvVariableVec::from(dir.vars.unwrap_or(EnvVariableStruct::default())))
.collect::<Vec<EnvVariable>>(),
);

Expand Down
31 changes: 30 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
use crate::cmd::Shell;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

pub type EnvVariableVec = Vec<EnvVariable>;
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum EnvVariableStruct {
HashMap(HashMap<String, String>),
EnvVariableVec(EnvVariableVec),
}

impl Default for EnvVariableStruct {
fn default() -> Self {
EnvVariableStruct::EnvVariableVec(vec![])
}
}

impl From<EnvVariableStruct> for EnvVariableVec {
fn from(env_variable: EnvVariableStruct) -> Self {
match env_variable {
EnvVariableStruct::EnvVariableVec(dir_env_variable) => dir_env_variable,
EnvVariableStruct::HashMap(hash_map) => {
// Convert the HashMap into a Vec<DirEnvVariable>
hash_map
.into_iter()
.map(|(name, value)| EnvVariable { name, value })
.collect()
}
}
}
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Config {
Expand Down Expand Up @@ -76,7 +105,7 @@ impl Default for GlobalConfig {
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct EnvDirectory {
pub path: String,
pub vars: Option<Vec<EnvVariable>>,
pub vars: Option<EnvVariableStruct>,
pub load_from: Option<Vec<String>>,
pub run: Option<Vec<String>>,
pub aliases: Option<Vec<EnvAlias>>,
Expand Down

0 comments on commit 5ffea5b

Please sign in to comment.