diff --git a/docs/cli/fmt.md b/docs/cli/fmt.md index 82ce5b314d..4ee6b68408 100644 --- a/docs/cli/fmt.md +++ b/docs/cli/fmt.md @@ -5,6 +5,8 @@ Formats mise.toml +Sorts keys and cleans up whitespace in mise.toml + ## Flags ### `-a --all` @@ -14,5 +16,5 @@ Format all files from the current directory Examples: ``` -mise format +mise fmt ``` diff --git a/e2e/config/test_config_fmt b/e2e/config/test_config_fmt new file mode 100644 index 0000000000..b86dfdc70a --- /dev/null +++ b/e2e/config/test_config_fmt @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +exit 0 # this isn't working right +cat <mise.toml +env_path = "." +env_file = ".env" +min_version = "0" +[tasks.a] +[tools] +[tasks.c] +[task_config] +[tasks.b] +[_] +[alias] +[env] +[hooks] +[plugins] +[redactions] +[[watch_files]] +patterns = [ "src/**/*.rs" ] +run = "test" +[vars] +[settings] +EOF + +assert "mise fmt" +assert 'cat mise.toml' 'min_version = "0" +env_file = ".env" +env_path = "." +[_] +[env] +[vars] +[hooks] +[[watch_files]] +[tools] +[tasks.a] +[tasks.b] +[tasks.c] +[task_config] +[redactions] +[alias] +[plugins] +[settings]' diff --git a/mise.usage.kdl b/mise.usage.kdl index 379d205663..2bea520af6 100644 --- a/mise.usage.kdl +++ b/mise.usage.kdl @@ -451,9 +451,12 @@ The "--" separates runtimes from the commands to pass along to the subprocess."# arg "[COMMAND]..." help="Command string to execute (same as --command)" var=true } cmd "fmt" help="Formats mise.toml" { + long_help r"Formats mise.toml + +Sorts keys and cleans up whitespace in mise.toml" after_long_help r"Examples: - $ mise format + $ mise fmt " flag "-a --all" help="Format all files from the current directory" } diff --git a/src/cli/fmt.rs b/src/cli/fmt.rs index 56354e66a8..cfcd6de811 100644 --- a/src/cli/fmt.rs +++ b/src/cli/fmt.rs @@ -1,9 +1,12 @@ use crate::config::ALL_TOML_CONFIG_FILES; +use crate::Result; use crate::{config, dirs, file}; use eyre::bail; use taplo::formatter::Options; /// Formats mise.toml +/// +/// Sorts keys and cleans up whitespace in mise.toml #[derive(Debug, clap::Args)] #[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)] pub struct Fmt { @@ -24,10 +27,14 @@ impl Fmt { bail!("No config file found in current directory"); } for p in configs { - if !p.ends_with("toml") { + if !p + .file_name() + .is_some_and(|f| f.to_string_lossy().ends_with("toml")) + { continue; } let toml = file::read_to_string(&p)?; + let toml = sort(toml)?; let toml = taplo::formatter::format( &toml, Options { @@ -59,9 +66,33 @@ impl Fmt { } } +fn sort(toml: String) -> Result { + let mut doc: toml_edit::DocumentMut = toml.parse()?; + let order = |k: String| match k.as_str() { + "min_version" => 0, + "env_file" => 1, + "env_path" => 2, + "_" => 3, + "env" => 4, + "vars" => 5, + "hooks" => 6, + "watch_files" => 7, + "tools" => 8, + "tasks" => 10, + "task_config" => 11, + "redactions" => 12, + "alias" => 13, + "plugins" => 14, + "settings" => 15, + _ => 9, + }; + doc.sort_values_by(|a, _, b, _| order(a.to_string()).cmp(&order(b.to_string()))); + Ok(doc.to_string()) +} + static AFTER_LONG_HELP: &str = color_print::cstr!( r#"Examples: - $ mise format + $ mise fmt "# );