jsona_cli/commands/
mod.rs

1#[cfg(feature = "lsp")]
2use self::lsp::LspCommand;
3use self::{format::FormatCommand, lint::LintCommand, queries::GetCommand};
4use crate::App;
5
6mod format;
7mod lint;
8#[cfg(feature = "lsp")]
9mod lsp;
10mod queries;
11
12use clap::{crate_version, ArgEnum, Args, Parser, Subcommand};
13use jsona_util::environment::Environment;
14
15impl<E: Environment> App<E> {
16    pub async fn execute(&mut self, args: AppArgs) -> Result<(), anyhow::Error> {
17        self.colors = match args.colors {
18            Colors::Auto => self.env.atty_stderr(),
19            Colors::Always => true,
20            Colors::Never => false,
21        };
22
23        match args.cmd {
24            JsonaCommand::Format(cmd) => self.execute_format(cmd).await,
25            #[cfg(feature = "lsp")]
26            JsonaCommand::Lsp { cmd } => {
27                #[cfg(feature = "lsp")]
28                {
29                    self.execute_lsp(cmd).await
30                }
31                #[cfg(not(feature = "lsp"))]
32                {
33                    let _ = cmd;
34                    return Err(anyhow::anyhow!("the LSP is not part of this build, please consult the documentation about enabling the functionality"));
35                }
36            }
37            JsonaCommand::Lint(cmd) => self.execute_lint(cmd).await,
38            JsonaCommand::Get(cmd) => self.execute_get(cmd).await,
39        }
40    }
41}
42
43#[derive(Clone, Copy, ArgEnum)]
44pub enum Colors {
45    /// Determine whether to colorize output automatically.
46    Auto,
47    /// Always colorize output.
48    Always,
49    /// Never colorize output.
50    Never,
51}
52
53#[derive(Clone, Parser)]
54#[clap(name = "jsona")]
55#[clap(bin_name = "jsona")]
56#[clap(version = crate_version!())]
57pub struct AppArgs {
58    #[clap(long, arg_enum, global = true, default_value = "auto")]
59    pub colors: Colors,
60    /// Enable a verbose logging format.
61    #[clap(long, global = true)]
62    pub verbose: bool,
63    /// Enable logging spans.
64    #[clap(long, global = true)]
65    pub log_spans: bool,
66    #[clap(subcommand)]
67    pub cmd: JsonaCommand,
68}
69
70#[derive(Debug, Clone, Args)]
71pub struct GeneralArgs {}
72
73#[derive(Clone, Subcommand)]
74pub enum JsonaCommand {
75    /// Lint JSONA documents.
76    #[clap(visible_aliases = &["check", "validate"])]
77    Lint(LintCommand),
78    /// Format JSONA documents.
79    ///
80    /// 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.
81    #[clap(visible_aliases = &["fmt"])]
82    Format(FormatCommand),
83    /// Language server operations.
84    #[cfg(feature = "lsp")]
85    Lsp {
86        #[clap(subcommand)]
87        cmd: LspCommand,
88    },
89    /// Extract a value from the given JSONA document.
90    Get(GetCommand),
91}