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    #[command(name = "dialects", about = "List available dialects")]
48    Dialects,
49    #[command(name = "templaters", about = "List available templaters")]
50    Templaters,
51    #[cfg(feature = "parser")]
52    #[command(
53        name = "parse",
54        about = "Parse SQL and output the parse tree for debugging"
55    )]
56    Parse(ParseArgs),
57}
58
59#[derive(Debug, Parser)]
60pub struct LintArgs {
61    /// Files or directories to fix. Use `-` to read from stdin.
62    pub paths: Vec<PathBuf>,
63    #[arg(default_value_t, short, long)]
64    pub format: Format,
65}
66
67#[derive(Debug, Parser)]
68pub struct FixArgs {
69    /// Files or directories to fix. Use `-` to read from stdin.
70    pub paths: Vec<PathBuf>,
71    /// The output format for the results.
72    #[arg(default_value_t, short, long)]
73    pub format: Format,
74}
75
76#[derive(Debug, Parser)]
77pub struct ParseArgs {
78    /// Files or directories to parse. Use `-` to read from stdin.
79    pub paths: Vec<PathBuf>,
80    /// The output format for the parse tree.
81    #[arg(default_value_t, short, long)]
82    pub format: ParseFormat,
83}
84
85#[derive(Debug, Clone, Copy, ValueEnum, Display)]
86#[strum(serialize_all = "kebab-case")]
87pub enum Format {
88    Human,
89    GithubAnnotationNative,
90    Json,
91}
92
93#[derive(Debug, Clone, Copy, ValueEnum, Display, Default)]
94#[strum(serialize_all = "kebab-case")]
95pub enum ParseFormat {
96    Json,
97    #[default]
98    Pretty,
99}
100
101impl Default for Format {
102    fn default() -> Self {
103        if is_in_github_action() {
104            Format::GithubAnnotationNative
105        } else {
106            Format::Human
107        }
108    }
109}