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
93
94
95
96
97
98
99
100
101
102
103
104
#[cfg(feature = "lsp")]
use self::lsp::LspCommand;
use self::{format::FormatCommand, lint::LintCommand, queries::GetCommand};
use crate::App;

mod format;
mod lint;
#[cfg(feature = "lsp")]
mod lsp;
mod queries;

use clap::{crate_version, ArgEnum, Args, Parser, Subcommand};
use jsona_util::environment::Environment;
use std::path::PathBuf;

impl<E: Environment> App<E> {
    pub async fn execute(&mut self, args: AppArgs) -> Result<(), anyhow::Error> {
        self.colors = match args.colors {
            Colors::Auto => self.env.atty_stderr(),
            Colors::Always => true,
            Colors::Never => false,
        };

        match args.cmd {
            JsonaCommand::Format(cmd) => self.execute_format(cmd).await,
            #[cfg(feature = "lsp")]
            JsonaCommand::Lsp { cmd } => {
                #[cfg(feature = "lsp")]
                {
                    self.execute_lsp(cmd).await
                }
                #[cfg(not(feature = "lsp"))]
                {
                    let _ = cmd;
                    return Err(anyhow::anyhow!("the LSP is not part of this build, please consult the documentation about enabling the functionality"));
                }
            }
            JsonaCommand::Lint(cmd) => self.execute_lint(cmd).await,
            JsonaCommand::Get(cmd) => self.execute_get(cmd).await,
        }
    }
}

#[derive(Clone, Copy, ArgEnum)]
pub enum Colors {
    /// Determine whether to colorize output automatically.
    Auto,
    /// Always colorize output.
    Always,
    /// Never colorize output.
    Never,
}

#[derive(Clone, Parser)]
#[clap(name = "jsona")]
#[clap(bin_name = "jsona")]
#[clap(version = crate_version!())]
pub struct AppArgs {
    #[clap(long, arg_enum, global = true, default_value = "auto")]
    pub colors: Colors,
    /// Enable a verbose logging format.
    #[clap(long, global = true)]
    pub verbose: bool,
    /// Enable logging spans.
    #[clap(long, global = true)]
    pub log_spans: bool,
    #[clap(subcommand)]
    pub cmd: JsonaCommand,
}

#[derive(Clone, Args)]
pub struct GeneralArgs {
    /// Path to the Jsona configuration file.
    #[clap(long, short)]
    pub config: Option<PathBuf>,

    /// Set a cache path.
    #[clap(long)]
    pub cache_path: Option<PathBuf>,

    /// Do not search for a configuration file.
    #[clap(long)]
    pub no_auto_config: bool,
}

#[derive(Clone, Subcommand)]
pub enum JsonaCommand {
    /// Lint JSONA documents.
    #[clap(visible_aliases = &["check", "validate"])]
    Lint(LintCommand),
    /// Format JSONA documents.
    ///
    /// Files are modified in-place unless the input comes from the standard input, in which case the formatted result is printed to the standard output.
    #[clap(visible_aliases = &["fmt"])]
    Format(FormatCommand),
    /// Language server operations.
    #[cfg(feature = "lsp")]
    Lsp {
        #[clap(subcommand)]
        cmd: LspCommand,
    },
    /// Extract a value from the given JSONA document.
    Get(GetCommand),
}