Skip to content

Commit

Permalink
feat: mise format
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 11, 2024
1 parent 507ee27 commit 46a1947
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 10 deletions.
148 changes: 138 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ siphasher = "1"
strum = { version = "0.26", features = ["derive"] }
sys-info = "0.9"
tabled = { version = "0.17", features = ["ansi"] }
taplo = "0.13"
tar = "0.4"
tempfile = "3"
tera = "1"
Expand Down
67 changes: 67 additions & 0 deletions src/cli/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::config::ALL_TOML_CONFIG_FILES;
use crate::{config, dirs, file};
use eyre::bail;
use taplo::formatter::Options;

/// Formats mise.toml
#[derive(Debug, clap::Args)]
#[clap(visible_alias="fmt", verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct Format {
/// Format all files from the current directory
#[clap(short, long)]
pub all: bool,
}

impl Format {
pub fn run(self) -> eyre::Result<()> {
let cwd = dirs::CWD.clone().unwrap_or_default();
let configs = if self.all {
ALL_TOML_CONFIG_FILES.clone()
} else {
config::config_files_in_dir(&cwd)
};
if configs.is_empty() {
bail!("No config file found in current directory");
}
for p in configs {
if !p.ends_with("toml") {
continue;
}
let toml = file::read_to_string(&p)?;
let toml = taplo::formatter::format(
&toml,
Options {
align_entries: false,
align_comments: true,
align_single_comments: true,
array_trailing_comma: true,
array_auto_expand: true,
inline_table_expand: true,
array_auto_collapse: true,
compact_arrays: true,
compact_inline_tables: false,
compact_entries: false,
column_width: 80,
indent_tables: false,
indent_entries: false,
indent_string: " ".to_string(),
trailing_newline: true,
reorder_keys: false,
reorder_arrays: false,
allowed_blank_lines: 2,
crlf: false,
},
);
file::write(&p, &toml)?;
}

Ok(())
}
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>mise format</bold>
"#
);
Loading

0 comments on commit 46a1947

Please sign in to comment.