Skip to content

Commit

Permalink
settings: read from config.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 11, 2024
1 parent 750141e commit f36f55c
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 40 deletions.
120 changes: 120 additions & 0 deletions schema/mise.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
"type": "string"
}
}
},
"settings": {
"description": "mise settings",
"type": "object",
"additionalProperties": false,
"properties": {"$ref": "#/$defs/settings"}
}
},
"$defs": {
Expand Down Expand Up @@ -213,6 +219,120 @@
"additionalProperties": false
}
]
},
"settings": {
"all_compile": {
"description": "do not use precompiled binaries for any tool",
"type": "boolean"
},
"always_keep_download": {
"description": "should mise keep downloaded files after installation",
"type": "boolean"
},
"always_keep_install": {
"description": "should mise keep install files after installation even if the installation fails",
"type": "boolean"
},
"asdf_compat": {
"description": "set to true to ensure .tool-versions will be compatible with asdf",
"type": "boolean"
},
"color": {
"description": "colorize output",
"type": "boolean",
"default": true
},
"disable_default_shorthands": {
"description": "disables built-in shorthands",
"type": "boolean"
},
"disable_tools": {
"description": "tools that should not be used",
"items": {
"description": "tool name",
"type": "string"
},
"type": "array"
},
"experimental": {
"description": "enable experimental features",
"type": "boolean"
},
"jobs": {
"description": "number of tools to install in parallel, default is 4",
"type": "integer"
},
"legacy_version_file": {
"description": "should mise parse legacy version files (e.g. .node-version)",
"type": "boolean"
},
"legacy_version_file_disable_tools": {
"description": "tools that should not have their legacy version files parsed",
"items": {
"description": "tool name",
"type": "string"
},
"type": "array"
},
"node_compile": {
"description": "do not use precompiled binaries for node",
"type": "boolean"
},
"not_found_auto_install": {
"description": "adds a shell hook to `mise activate` and shims to automatically install tools when they need to be installed",
"type": "boolean",
"default": true
},
"paranoid": {
"description": "extra-security mode, see https://mise.jdx.dev/paranoid.html for details",
"type": "boolean"
},
"plugin_autoupdate_last_check_duration": {
"description": "how often to check for plugin updates",
"type": "string"
},
"python_compile": {
"description": "do not use precompiled binaries for python",
"type": "boolean"
},
"python_venv_auto_create": {
"description": "automatically create a virtualenv for python tools",
"type": "boolean"
},
"raw": {
"description": "directly connect plugin scripts to stdin/stdout, implies --jobs=1",
"type": "boolean"
},
"shorthands_file": {
"description": "path to file containing shorthand mappings",
"type": "string"
},
"task_output": {
"default": "prefix",
"description": "how to display task output",
"enum": ["prefix", "interleave"],
"type": "string"
},
"trusted_config_paths": {
"description": "config files with these prefixes will be trusted by default",
"items": {
"description": "a path to add to PATH",
"type": "string"
},
"type": "array"
},
"quiet": {
"description": "suppress all non-error output",
"type": "boolean"
},
"verbose": {
"description": "display extra output",
"type": "boolean"
},
"yes": {
"description": "assume yes for all prompts",
"type": "boolean"
}
}
}
}
7 changes: 0 additions & 7 deletions src/cli/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ impl Doctor {
miseprintln!("{}", shell());
miseprintln!("{}", mise_data_dir());
miseprintln!("{}", mise_env_vars());
miseprintln!("{}", mise_settings_file());
match Settings::try_get() {
Ok(settings) => {
miseprintln!(
Expand Down Expand Up @@ -124,12 +123,6 @@ fn mise_env_vars() -> String {
s
}

fn mise_settings_file() -> String {
let mut s = style("mise settings file:\n").bold().to_string();
s.push_str(&format!(" {}\n", display_path(&env::MISE_SETTINGS_FILE)));
s
}

fn render_config_files(config: &Config) -> String {
let mut s = style("config files:\n").bold().to_string();
for f in config.config_files.keys().rev() {
Expand Down
25 changes: 9 additions & 16 deletions src/cli/settings/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,16 @@ impl SettingsSet {
_ => return Err(miette!("Unknown setting: {}", self.setting)),
};

let path = &*env::MISE_SETTINGS_FILE;
let path = &*env::MISE_GLOBAL_CONFIG_FILE;
file::create_dir_all(path.parent().unwrap())?;
let mut new_file = false;
if !path.exists() {
file::write(path, "")?;
new_file = true;
let raw = file::read_to_string(path).unwrap_or_default();
let mut config: Document = raw.parse().into_diagnostic()?;
if !config.contains_key("settings") {
config["settings"] = toml_edit::Item::Table(toml_edit::Table::new());
}
let raw = file::read_to_string(path)?;
let mut settings: Document = raw.parse().into_diagnostic()?;
let settings = config["settings"].as_table_mut().unwrap();
settings.insert(&self.setting, toml_edit::Item::Value(value));
if new_file {
settings
.key_decor_mut(&self.setting)
.unwrap()
.set_prefix("#:schema https://mise.jdx.dev/schema/settings.json\n");
}
file::write(path, settings.to_string())
file::write(path, config.to_string())
}
}

Expand Down Expand Up @@ -110,7 +103,7 @@ pub mod tests {
color = true
disable_default_shorthands = false
disable_tools = []
experimental = false
experimental = true
jobs = 4
legacy_version_file = true
legacy_version_file_disable_tools = []
Expand All @@ -131,7 +124,7 @@ pub mod tests {
shorthands_file = null
task_output = null
trusted_config_paths = []
verbose = false
verbose = true
yes = true
"###);
reset_config();
Expand Down
24 changes: 17 additions & 7 deletions src/config/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,19 @@ impl Settings {
if let Some(settings) = SETTINGS.read().unwrap().as_ref() {
return Ok(settings.clone());
}
let file_settings = Self::file_settings().unwrap_or_else(|e| {
let file_1 = Self::config_settings().unwrap_or_else(|e| {
eprintln!("Error loading settings file: {}", e);
Default::default()
});
let file_2 = Self::deprecated_settings_file().unwrap_or_else(|e| {
eprintln!("Error loading settings file: {}", e);
Default::default()
});
let mut settings = Self::builder()
.preloaded(CLI_SETTINGS.lock().unwrap().clone().unwrap_or_default())
.env()
.preloaded(file_settings)
.preloaded(file_1)
.preloaded(file_2)
.preloaded(DEFAULT_SETTINGS.clone())
.load()
.into_diagnostic()?;
Expand Down Expand Up @@ -221,11 +226,7 @@ impl Settings {
Self::reset(Some(s));
}

fn file_settings() -> Result<SettingsPartial> {
let settings_file = &*env::MISE_SETTINGS_FILE;
if settings_file.exists() {
return Self::from_file(settings_file);
}
fn config_settings() -> Result<SettingsPartial> {
let global_config = &*env::MISE_GLOBAL_CONFIG_FILE;
if !global_config.exists() {
return Ok(Default::default());
Expand All @@ -235,6 +236,15 @@ impl Settings {
Ok(settings_file.settings)
}

fn deprecated_settings_file() -> Result<SettingsPartial> {
// TODO: show warning and merge with config file in a few weeks
let settings_file = &*env::MISE_SETTINGS_FILE;
if !settings_file.exists() {
return Ok(Default::default());
}
Self::from_file(settings_file)
}

pub fn from_file(path: &PathBuf) -> Result<SettingsPartial> {
let raw = file::read_to_string(path)?;
let settings: SettingsPartial = toml::from_str(&raw).into_diagnostic()?;
Expand Down
11 changes: 6 additions & 5 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ pub fn reset_config() {
indoc! {r#"
experimental = true
verbose = true
always_keep_download= true
always_keep_install= true
legacy_version_file= true
plugin_autoupdate_last_check_duration = "20m"
jobs = 2
"#},
)
.unwrap();
Expand All @@ -77,6 +72,12 @@ pub fn reset_config() {
run = 'echo "linting!"'
[tasks.test]
run = 'echo "testing!"'
[settings]
always_keep_download= true
always_keep_install= true
legacy_version_file= true
plugin_autoupdate_last_check_duration = "20m"
jobs = 2
"#},
)
.unwrap();
Expand Down
6 changes: 6 additions & 0 deletions test/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ run = 'echo "configtask:"'
run = 'echo "linting!"'
[tasks.test]
run = 'echo "testing!"'
[settings]
always_keep_download= true
always_keep_install= true
legacy_version_file= true
plugin_autoupdate_last_check_duration = "20m"
jobs = 2
5 changes: 0 additions & 5 deletions test/config/settings.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
experimental = true
verbose = true
always_keep_download= true
always_keep_install= true
legacy_version_file= true
plugin_autoupdate_last_check_duration = "20m"
jobs = 2

0 comments on commit f36f55c

Please sign in to comment.