Skip to content

Commit

Permalink
fix: read all config files for mise set
Browse files Browse the repository at this point in the history
Fixes #3294
  • Loading branch information
jdx committed Dec 11, 2024
1 parent 1c59fcb commit f633d32
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 31 deletions.
100 changes: 70 additions & 30 deletions src/cli/set.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use super::args::EnvVarArg;
Expand Down Expand Up @@ -42,37 +43,17 @@ pub struct Set {

impl Set {
pub fn run(self) -> Result<()> {
let filename = if let Some(file) = &self.file {
file.clone()
} else if self.global {
config::global_config_path()
} else {
config::local_toml_config_path()
};

if self.remove.is_none() && self.env_vars.is_none() {
let rows = Config::get()
.env_with_sources()?
.iter()
.filter(|(_, (_, source))| {
if self.file.is_some() {
source == &filename
} else {
true
}
})
.map(|(key, (value, source))| Row {
key: key.clone(),
value: value.clone(),
source: display_path(source),
})
.collect::<Vec<_>>();
let mut table = tabled::Table::new(rows);
table::default_style(&mut table, false);
miseprintln!("{table}");
return Ok(());
match (&self.remove, &self.env_vars) {
(None, None) => {
return self.list_all();
}
(None, Some(env_vars)) if env_vars.iter().all(|ev| ev.value.is_none()) => {
return self.get();
}
_ => {}
}

let filename = self.filename();
let config = MiseToml::from_file(&filename).unwrap_or_default();

let mut mise_toml = get_mise_toml(&filename)?;
Expand Down Expand Up @@ -104,6 +85,65 @@ impl Set {
}
mise_toml.save()
}

fn list_all(self) -> Result<()> {
let env = self.cur_env()?;
let mut table = tabled::Table::new(env);
table::default_style(&mut table, false);
miseprintln!("{table}");
Ok(())
}

fn get(self) -> Result<()> {
let env = self.cur_env()?;
let filter = self.env_vars.unwrap();
let vars = filter
.iter()
.filter_map(|ev| {
env.iter()
.find(|r| r.key == ev.key)
.map(|r| (r.key.clone(), r.value.clone()))
})
.collect::<HashMap<String, String>>();
for eva in filter {
if let Some(value) = vars.get(&eva.key) {
miseprintln!("{value}");
} else {
bail!("Environment variable {} not found", eva.key);
}
}
Ok(())
}

fn cur_env(&self) -> Result<Vec<Row>> {
let rows = Config::get()
.env_with_sources()?
.iter()
.filter(|(_, (_, source))| {
if self.file.is_some() {
source == &self.filename()
} else {
true
}
})
.map(|(key, (value, source))| Row {
key: key.clone(),
value: value.clone(),
source: display_path(source),
})
.collect();
Ok(rows)
}

fn filename(&self) -> PathBuf {
if let Some(file) = &self.file {
file.clone()
} else if self.global {
config::global_config_path()
} else {
config::local_toml_config_path()
}
}
}

fn get_mise_toml(filename: &Path) -> Result<MiseToml> {
Expand All @@ -117,7 +157,7 @@ fn get_mise_toml(filename: &Path) -> Result<MiseToml> {
Ok(mise_toml)
}

#[derive(Tabled)]
#[derive(Tabled, Clone)]
struct Row {
key: String,
value: String,
Expand Down
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub use settings::SETTINGS;

type AliasMap = IndexMap<String, Alias>;
type ConfigMap = IndexMap<PathBuf, Box<dyn ConfigFile>>;
type EnvWithSources = IndexMap<String, (String, PathBuf)>;
pub type EnvWithSources = IndexMap<String, (String, PathBuf)>;

#[derive(Default)]
pub struct Config {
Expand Down

0 comments on commit f633d32

Please sign in to comment.