-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.rs
243 lines (230 loc) Β· 7.71 KB
/
main.rs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#![allow(clippy::match_wildcard_for_single_variants)]
#[allow(clippy::non_ascii_literal)]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::enum_variant_names)]
#[allow(clippy::module_name_repetitions)]
mod config;
mod file_finding;
mod reporter;
mod subcommand;
use crate::file_finding::find_paths;
use crate::reporter::{
check_files, dump_ast_for_paths, explain_rule, list_rules, print_violations, DumpAstOption,
Reporter,
};
use crate::subcommand::{check_and_comment_on_pr, Command};
use atty::Stream;
use config::Config;
use log::info;
use simplelog::CombinedLogger;
use squawk_linter::versions::Version;
use squawk_linter::violations::RuleViolationKind;
use std::io;
use std::path::PathBuf;
use std::process;
use structopt::StructOpt;
fn exit<E: std::fmt::Display, T>(res: Result<T, E>, msg: &str) -> ! {
match res {
Ok(_) => process::exit(0),
Err(err) => {
eprintln!("{msg}: {err}");
process::exit(1)
}
}
}
/// Find problems in your SQL
#[allow(clippy::struct_excessive_bools)]
#[derive(StructOpt, Debug)]
struct Opt {
/// Paths or patterns to search
#[structopt(value_name = "path")]
path_patterns: Vec<String>,
/// Paths to exclude
///
/// For example:
///
/// `--exclude-path=005_user_ids.sql --exclude-path=009_account_emails.sql`
///
/// `--exclude-path='*user_ids.sql'`
#[structopt(long = "exclude-path", global = true)]
excluded_path: Option<Vec<String>>,
/// Exclude specific warnings
///
/// For example:
/// --exclude=require-concurrent-index-creation,ban-drop-database
#[structopt(
short = "e",
long = "exclude",
value_name = "rule",
use_delimiter = true,
global = true
)]
excluded_rules: Option<Vec<RuleViolationKind>>,
/// Specify postgres version
///
/// For example:
/// --pg-version=13.0
#[structopt(long, global = true)]
pg_version: Option<Version>,
/// List all available rules
#[structopt(long)]
list_rules: bool,
/// Provide documentation on the given rule
#[structopt(long, value_name = "rule")]
explain: Option<String>,
/// Output AST in JSON
#[structopt(long,value_name ="ast-format", possible_values = &DumpAstOption::variants(), case_insensitive = true)]
dump_ast: Option<DumpAstOption>,
/// Style of error reporting
#[structopt(long, possible_values = &Reporter::variants(), case_insensitive = true)]
reporter: Option<Reporter>,
#[structopt(long, value_name = "filepath")]
/// Path to use in reporting for stdin
stdin_filepath: Option<String>,
#[structopt(subcommand)]
cmd: Option<Command>,
/// Enable debug logging output
#[structopt(long, global = true)]
verbose: bool,
/// Path to the squawk config file (.squawk.toml)
#[structopt(short = "c", long = "config", global = true)]
config_path: Option<PathBuf>,
/// Assume that a transaction will wrap each SQL file when run by a migration tool
///
/// Use --no-assume-in-transaction to override any config file that sets this
#[structopt(long, global = true)]
assume_in_transaction: bool,
#[structopt(
long,
hidden = true,
conflicts_with = "assume-in-transaction",
global = true
)]
no_assume_in_transaction: bool,
}
#[allow(clippy::too_many_lines)]
fn main() {
let opts = Opt::from_args();
if opts.verbose {
CombinedLogger::init(vec![simplelog::TermLogger::new(
simplelog::LevelFilter::Info,
simplelog::Config::default(),
simplelog::TerminalMode::Mixed,
simplelog::ColorChoice::Auto,
)])
.expect("problem creating logger");
}
let conf = Config::parse(opts.config_path)
.unwrap_or_else(|e| {
eprintln!("Configuration error: {e}");
process::exit(1);
})
.unwrap_or_default();
// the --exclude flag completely overrides the configuration file.
let excluded_rules = if let Some(excluded_rules) = opts.excluded_rules {
excluded_rules
} else {
conf.excluded_rules.clone()
};
// the --exclude-path flag completely overrides the configuration file.
let excluded_paths = if let Some(excluded_paths) = opts.excluded_path {
excluded_paths
} else {
conf.excluded_paths.clone()
};
let pg_version = if let Some(pg_version) = opts.pg_version {
Some(pg_version)
} else {
conf.pg_version
};
let assume_in_transaction = if opts.assume_in_transaction {
opts.assume_in_transaction
} else if opts.no_assume_in_transaction {
!opts.no_assume_in_transaction
} else {
conf.assume_in_transaction.unwrap_or_default()
};
info!("pg version: {:?}", pg_version);
info!("excluded rules: {:?}", &excluded_rules);
info!("excluded paths: {:?}", &excluded_paths);
info!("assume in a transaction: {:?}", assume_in_transaction);
let mut clap_app = Opt::clap();
let stdout = io::stdout();
let mut handle = stdout.lock();
let is_stdin = !atty::is(Stream::Stdin);
let found_paths = find_paths(&opts.path_patterns, &excluded_paths).unwrap_or_else(|e| {
eprintln!("Failed to find files: {e}");
process::exit(1);
});
if found_paths.is_empty() && !opts.path_patterns.is_empty() {
eprintln!(
"Failed to find files for provided patterns: {:?}",
opts.path_patterns
);
process::exit(1);
}
if let Some(subcommand) = opts.cmd {
exit(
check_and_comment_on_pr(
subcommand,
&conf,
is_stdin,
opts.stdin_filepath,
&excluded_rules,
&excluded_paths,
pg_version,
assume_in_transaction,
),
"Upload to GitHub failed",
);
} else if !found_paths.is_empty() || is_stdin {
let read_stdin = found_paths.is_empty() && is_stdin;
if let Some(dump_ast_kind) = opts.dump_ast {
exit(
dump_ast_for_paths(&mut handle, &found_paths, read_stdin, &dump_ast_kind),
"Failed to dump AST",
);
} else {
match check_files(
&found_paths,
read_stdin,
opts.stdin_filepath,
&excluded_rules,
pg_version,
assume_in_transaction,
) {
Ok(file_reports) => {
let reporter = opts.reporter.unwrap_or(Reporter::Tty);
let total_violations = file_reports
.iter()
.map(|f| f.violations.len())
.sum::<usize>();
match print_violations(&mut handle, file_reports, &reporter) {
Ok(()) => {
let exit_code = i32::from(total_violations > 0);
process::exit(exit_code);
}
Err(e) => {
eprintln!("Problem reporting violations: {e}");
process::exit(1);
}
}
}
Err(e) => {
eprintln!("Problem linting SQL files: {e}");
process::exit(1)
}
}
}
} else if opts.list_rules {
exit(list_rules(&mut handle), "Could not list rules");
} else if let Some(rule_name) = opts.explain {
exit(
explain_rule(&mut handle, &rule_name),
"Could not explain rules",
);
} else {
clap_app.print_long_help().expect("problem printing help");
println!();
}
}