sqruff_cli_lib/
commands.rs

1use std::path::PathBuf;
2use strum_macros::Display;
3
4use clap::{Parser, Subcommand, ValueEnum};
5
6use crate::github_action::is_in_github_action;
7
8#[derive(Debug, Parser)]
9#[command(name = "sqruff")]
10#[command(about = "sqruff is a sql formatter and linter", long_about = None, version=env!("CARGO_PKG_VERSION")
11)]
12pub struct Cli {
13    #[command(subcommand)]
14    pub command: Commands,
15    /// Path to a configuration file.
16    #[arg(long, global = true)]
17    pub config: Option<String>,
18    /// Override the dialect (e.g., bigquery, clickhouse, ansi).
19    #[arg(long, global = true)]
20    pub dialect: Option<String>,
21    /// Show parse errors.
22    #[arg(long, global = true, default_value = "false")]
23    pub parsing_errors: bool,
24}
25
26#[derive(Debug, Subcommand)]
27pub enum Commands {
28    #[command(
29        name = "lint",
30        about = "Lint SQL files via passing a list of files or using stdin"
31    )]
32    Lint(LintArgs),
33    #[command(
34        name = "fix",
35        about = "Fix SQL files via passing a list of files or using stdin"
36    )]
37    Fix(FixArgs),
38    #[command(name = "lsp", about = "Run an LSP server")]
39    Lsp,
40    #[command(
41        name = "info",
42        about = "Print information about sqruff and the current environment"
43    )]
44    Info,
45    #[command(name = "rules", about = "Explain the available rules")]
46    Rules,
47    #[cfg(feature = "parser")]
48    #[command(
49        name = "parse",
50        about = "Parse SQL and output the parse tree for debugging"
51    )]
52    Parse(ParseArgs),
53}
54
55#[derive(Debug, Parser)]
56pub struct LintArgs {
57    /// Files or directories to fix. Use `-` to read from stdin.
58    pub paths: Vec<PathBuf>,
59    #[arg(default_value_t, short, long)]
60    pub format: Format,
61}
62
63#[derive(Debug, Parser)]
64pub struct FixArgs {
65    /// Files or directories to fix. Use `-` to read from stdin.
66    pub paths: Vec<PathBuf>,
67    /// The output format for the results.
68    #[arg(default_value_t, short, long)]
69    pub format: Format,
70}
71
72#[derive(Debug, Parser)]
73pub struct ParseArgs {
74    /// Files or directories to parse. Use `-` to read from stdin.
75    pub paths: Vec<PathBuf>,
76    /// The output format for the parse tree.
77    #[arg(default_value_t, short, long)]
78    pub format: ParseFormat,
79}
80
81#[derive(Debug, Clone, Copy, ValueEnum, Display)]
82#[strum(serialize_all = "kebab-case")]
83pub enum Format {
84    Human,
85    GithubAnnotationNative,
86    Json,
87}
88
89#[derive(Debug, Clone, Copy, ValueEnum, Display, Default)]
90#[strum(serialize_all = "kebab-case")]
91pub enum ParseFormat {
92    Json,
93    #[default]
94    Pretty,
95}
96
97impl Default for Format {
98    fn default() -> Self {
99        if is_in_github_action() {
100            Format::GithubAnnotationNative
101        } else {
102            Format::Human
103        }
104    }
105}