1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! CLI command structures for formatter
#![allow(clippy::redundant_closure)]

mod format;
mod format_stdin;
mod init;

use self::format::format_cmd;
use self::format_stdin::format_stdin_cmd;
use self::init::init_cmd;
use crate::expand_path;
use std::path::PathBuf;
use structopt::StructOpt;

/// ✨  format all your language!
#[derive(Debug, StructOpt)]
pub struct Cli {
    /// Create a new treefmt.toml
    #[structopt(long = "init")]
    pub init: bool,

    /// Format the content passed in stdin
    #[structopt(long = "stdin", conflicts_with("init"))]
    pub stdin: bool,

    /// Clear the evaluation cache. Use in case the cache is not precise enough.
    #[structopt(long = "clear-cache", conflicts_with("stdin"), conflicts_with("init"))]
    pub clear_cache: bool,

    /// Exit with error if any changes were made. Useful for CI.
    #[structopt(
        long = "fail-on-change",
        conflicts_with("stdin"),
        conflicts_with("init")
    )]
    pub fail_on_change: bool,

    /// Log verbosity is based off the number of v used
    #[structopt(long = "verbose", short = "v", parse(from_occurrences))]
    pub verbosity: u8,

    #[structopt(long = "quiet", short = "q")]
    /// No output printed to stderr
    pub quiet: bool,

    #[structopt(short = "C", default_value = ".")]
    /// Run as if treefmt was started in <work-dir> instead of the current working directory.
    pub work_dir: PathBuf,

    #[structopt(long = "tree-root")]
    /// Set the path to the tree root directory. Defaults to the folder holding the treefmt.toml file.
    pub tree_root: Option<PathBuf>,

    #[structopt()]
    /// Paths to format. Defaults to formatting the whole tree.
    pub paths: Vec<PathBuf>,
}

/// Use this instead of Cli::from_args(). We do a little bit of post-processing here.
pub fn cli_from_args() -> anyhow::Result<Cli> {
    let mut cli = Cli::from_args();
    let cwd = std::env::current_dir()?;
    assert!(cwd.is_absolute());
    // Make sure the work_dir is an absolute path. Don't use the stdlib canonicalize() function
    // because symlinks should not be resolved.
    cli.work_dir = expand_path(&cli.work_dir, &cwd);

    // Make sure the tree_root is an absolute path.
    if let Some(tree_root) = cli.tree_root {
        cli.tree_root = Some(expand_path(&tree_root, &cwd));
    }
    Ok(cli)
}

/// Run a command with the given logger
pub fn run_cli(cli: &Cli) -> anyhow::Result<()> {
    if cli.init {
        init_cmd(&cli.work_dir)?
    } else if cli.stdin {
        format_stdin_cmd(&cli.tree_root, &cli.work_dir, &cli.paths)?
    } else {
        format_cmd(
            &cli.tree_root,
            &cli.work_dir,
            &cli.paths,
            cli.clear_cache,
            cli.fail_on_change,
        )?
    }

    Ok(())
}