1use std::ffi::{OsStr, OsString};
4use std::io::{self, BufWriter, Write};
5use std::path::Path;
6use std::process::ExitCode;
7
8use jsonvet::Severity;
9
10mod args;
11mod glob;
12mod output;
13mod sarif;
14mod schema;
15mod settings;
16
17use args::{Command, ConfigSource};
18use output::Printer;
19use schema::Schema;
20use settings::Settings;
21
22const SCHEMA_MAX_DEPTH: usize = 512;
26
27const STACK: usize = 64 << 20;
32
33pub fn run() -> ExitCode {
35 on_big_stack(run_here)
36}
37
38fn on_big_stack<T: Send + 'static>(f: fn() -> T) -> T {
41 let thread = std::thread::Builder::new().name("jsonvet".into());
42 match thread.stack_size(STACK).spawn(f) {
43 Ok(thread) => thread
44 .join()
45 .unwrap_or_else(|panic| std::panic::resume_unwind(panic)),
46 Err(_) => f(),
47 }
48}
49
50fn run_here() -> ExitCode {
51 let annotate = std::env::var_os("GITHUB_ACTIONS").is_some_and(|v| v == "true");
52 let stdout = io::stdout().lock();
53 match run_with(
54 std::env::args_os().skip(1),
55 annotate,
56 BufWriter::new(stdout),
57 ) {
58 Ok(code) => ExitCode::from(code),
59 Err(message) => {
60 eprintln!("jsonvet: {message}");
61 ExitCode::from(2)
62 }
63 }
64}
65
66fn run_with(
69 args: impl IntoIterator<Item = OsString>,
70 annotate: bool,
71 out: impl Write,
72) -> Result<u8, String> {
73 let args = match args::parse(args).map_err(|e| {
75 format!(
76 "{}\n\nRun `jsonvet --help` for usage.",
77 output::printable(&e)
78 )
79 })? {
80 Command::Help => return print(out, args::HELP),
81 Command::Version => return print(out, concat!("jsonvet ", env!("CARGO_PKG_VERSION"))),
82 Command::Check(args) => args,
83 };
84 let from_file = match &args.config {
85 ConfigSource::None => Settings::default(),
86 ConfigSource::File(path) => settings::load(path)?,
87 ConfigSource::Search => {
88 let cwd = std::env::current_dir()
89 .map_err(|e| format!("cannot read the working directory: {e}"))?;
90 match settings::discover(&cwd) {
91 Some(path) => settings::load(&path)?,
92 None => Settings::default(),
93 }
94 }
95 };
96 let settings = from_file.overridden_by(args.settings);
97 let schema = args
98 .schema
99 .as_deref()
100 .map(|path| Schema::load(path, &settings))
101 .transpose()
102 .map_err(|e| output::printable(&e))?;
103
104 let mut printer = Printer::new(out, args.format, annotate);
105 let mut counts = Counts::default();
106 let written = args
107 .files
108 .iter()
109 .try_for_each(|file| {
110 check_file(file, &settings, schema.as_ref(), &mut printer, &mut counts)
111 })
112 .and_then(|()| printer.finish());
113 match written {
115 Err(e) if e.kind() != io::ErrorKind::BrokenPipe => {
116 return Err(format!("cannot write output: {e}"));
117 }
118 _ => {}
119 }
120 let too_many_warnings = args.max_warnings.is_some_and(|max| counts.warnings > max);
121 Ok(u8::from(counts.errors > 0 || too_many_warnings))
122}
123
124#[derive(Default)]
125struct Counts {
126 errors: usize,
127 warnings: usize,
128}
129
130fn check_file(
134 file: &OsStr,
135 settings: &Settings,
136 schema: Option<&Schema>,
137 printer: &mut Printer<impl Write>,
138 counts: &mut Counts,
139) -> io::Result<()> {
140 let text = read(Path::new(file));
141 let name = file.to_string_lossy();
142 let text = match text {
143 Ok(text) => text,
144 Err(message) => {
145 counts.errors += 1;
146 return printer.unreadable(file, &message);
147 }
148 };
149 let config = settings.config_for(&name);
150 let (report, events) = match schema {
151 Some(_) => jsonvet::parse(&text, &config.max_depth(SCHEMA_MAX_DEPTH)),
152 None => (jsonvet::check(&text, &config), None),
153 };
154 for problem in report.problems() {
155 match problem.severity() {
156 Severity::Error => counts.errors += 1,
157 Severity::Warning => counts.warnings += 1,
158 _ => {}
159 }
160 printer.problem(file, &report, problem)?;
161 }
162 if let (Some(schema), Some(events)) = (schema, events) {
164 for (offset, message, summary) in schema.errors(&events) {
165 counts.errors += usize::from(!summary);
166 printer.schema_error(file, &report, offset, &message, summary)?;
167 }
168 }
169 Ok(())
170}
171
172fn read(path: &Path) -> Result<String, String> {
176 let read = std::fs::metadata(path).and_then(|meta| {
177 if meta.is_file() {
178 std::fs::read(path).map(Some)
179 } else {
180 Ok(None)
181 }
182 });
183 match read {
184 Ok(Some(bytes)) => String::from_utf8(bytes)
185 .map_err(|e| format!("not valid UTF-8 (byte {})", e.utf8_error().valid_up_to())),
186 Ok(None) => Err("not a regular file".to_owned()),
187 Err(e) if e.kind() == io::ErrorKind::NotFound => Err("no such file".to_owned()),
188 Err(e) => Err(format!("cannot read: {e}")),
189 }
190}
191
192fn print(mut out: impl Write, text: &str) -> Result<u8, String> {
193 match writeln!(out, "{text}").and_then(|()| out.flush()) {
194 Err(e) if e.kind() != io::ErrorKind::BrokenPipe => Err(format!("cannot write output: {e}")),
195 _ => Ok(0),
196 }
197}
198
199#[cfg(test)]
200mod tests {
201 use std::sync::atomic::{AtomicUsize, Ordering};
202
203 #[test]
204 fn a_panic_on_the_big_stack_carries_on_in_the_caller() {
205 static RUNS: AtomicUsize = AtomicUsize::new(0);
206 let panic = std::panic::catch_unwind(|| {
207 super::on_big_stack(|| -> u8 {
208 RUNS.fetch_add(1, Ordering::Relaxed);
209 panic!("on the big stack")
210 })
211 })
212 .expect_err("the panic carries on");
213 assert_eq!(panic.downcast_ref::<&str>(), Some(&"on the big stack"));
214 assert_eq!(RUNS.load(Ordering::Relaxed), 1);
215 }
216}