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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use clap::CommandFactory;
use clap::Parser;
use clap_complete::Shell;
use flate2::read::GzDecoder;
use glob::glob;
use grep_cli::is_readable_stdin;
use humantime::format_duration;
use json::ndjson::parse_ndjson_bufreader_par;
use json::ndjson::parse_ndjson_receiver_par;
use json::ndjson::process_json_iterable_par;
use json::ndjson::Errors;
use json::ndjson::ErrorsPar;
use jsonpath_lib::Compiled;
use std::error;
use std::ffi::OsStr;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::PathBuf;
use std::time::Instant;

use crate::json::ndjson::FileStats;
use crate::json::ndjson::{
    parse_ndjson_bufreader, parse_ndjson_file_path, process_json_iterable, Stats,
};

mod io_helpers;
pub mod json;

type Result<T> = ::std::result::Result<T, Box<dyn error::Error>>;

#[derive(Parser, Default, PartialEq, Eq)]
#[clap(author, version, about, long_about = None)]
pub struct Cli {
    /// File to process, expected to contain a single JSON object or Newline Delimited (ND) JSON objects
    #[clap(value_parser)]
    file_path: Option<std::path::PathBuf>,

    /// Process all files identified by this glob pattern
    #[clap(short, long)]
    glob: Option<String>,

    /// Limit inspection to the first n lines
    #[clap(short = 'n', long)]
    lines: Option<usize>,

    /// JSONpath query to filter/limit the inspection to e.g. `'$.a_key.an_array[0]'`
    #[clap(long)]
    jsonpath: Option<String>,

    /// Walk the elements of arrays grouping elements paths together under `$.path.to.array[*]`?
    /// Takes precedence over `--explode-arrays`
    #[clap(long)]
    inspect_arrays: bool,

    /// Walk the elements of arrays treating arrays like a map of their enumerated elements?
    /// (E.g. $.path.to.array[0], $.path.to.array[1], ...)
    /// Ignored if using `--inspect-arrays`
    #[clap(long)]
    explode_arrays: bool,

    /// Include combined results for all files when using glob
    #[clap(long)]
    merge: bool,

    /// Use multi-threaded version of the processing
    #[clap(long)]
    parallel: bool,

    /// Silence error logging
    #[clap(short, long)]
    quiet: bool,

    /// Output shell completions for the chosen shell to stdout
    #[clap(value_enum, long, id = "SHELL")]
    generate_completions: Option<Shell>,
}

impl Cli {
    fn jsonpath_selector(&self) -> Result<Option<Compiled>> {
        let jsonpath_selector = if let Some(jsonpath) = &self.jsonpath {
            let selector = Compiled::compile(jsonpath)?;
            Some(selector)
        } else {
            None
        };
        Ok(jsonpath_selector)
    }
}

/// Wrapper around [`Cli`] to hold derived attributes
pub struct Settings {
    args: Cli,
    jsonpath_selector: Option<Compiled>,
}

impl Settings {
    fn init(args: Cli) -> Result<Self> {
        let jsonpath_selector = args.jsonpath_selector()?;
        Ok(Self {
            args,
            jsonpath_selector,
        })
    }
}

fn get_bufreader(_args: &Cli, file_path: &std::path::PathBuf) -> Result<Box<dyn BufRead + Send>> {
    let extension = file_path.extension().and_then(OsStr::to_str);
    let file = File::open(file_path)?;
    if extension == Some("gz") {
        let file = GzDecoder::new(file);
        Ok(Box::new(io::BufReader::new(file)))
    } else {
        Ok(Box::new(io::BufReader::new(file)))
    }
}

fn process_ndjson_file_path(settings: &Settings, file_path: &PathBuf) -> Result<Stats> {
    let errors = Errors::default();

    let json_iter = parse_ndjson_file_path(&settings.args, file_path, &errors)?;
    let file_stats = process_json_iterable(settings, json_iter, &errors);

    if !settings.args.quiet {
        errors.eprint();
    }

    Ok(file_stats)
}

fn process_ndjson_file_path_par(settings: &Settings, file_path: &PathBuf) -> Result<Stats> {
    let errors = ErrorsPar::default();

    let json_iter = parse_ndjson_bufreader_par(&settings.args, file_path, &errors)?;
    let file_stats = process_json_iterable_par(settings, json_iter, &errors);

    if !settings.args.quiet {
        errors.eprint();
    }

    Ok(file_stats)
}

fn run_stdin(settings: Settings) -> Result<()> {
    let stdin = io::stdin().lock();
    let errors = Errors::default();
    let json_iter = parse_ndjson_bufreader(&settings.args, stdin, &errors)?;
    let stdin_stats = process_json_iterable(&settings, json_iter, &errors);

    if !settings.args.quiet {
        errors.eprint();
    }

    stdin_stats.print()?;
    Ok(())
}

fn run_stdin_par(settings: Settings) -> Result<()> {
    let stdin = io_helpers::stdin::spawn_stdin_channel(1_000_000);
    let errors = ErrorsPar::default();
    let json_iter = parse_ndjson_receiver_par(&settings.args, stdin, &errors);
    let stdin_stats = process_json_iterable_par(&settings, json_iter, &errors);

    if !settings.args.quiet {
        errors.eprint();
    }

    stdin_stats.print()?;
    Ok(())
}

fn run_no_stdin(settings: Settings) -> Result<()> {
    if let Some(file_path) = &settings.args.file_path {
        let file_stats = process_ndjson_file_path(&settings, file_path)?;

        file_stats.print()?;
        return Ok(());
    }

    if let Some(pattern) = &settings.args.glob {
        let mut file_stats_list = Vec::new();

        println!("Glob '{}':", pattern);
        for entry in glob(pattern)? {
            let file_path = entry?;
            println!("File '{}':", file_path.display());
            let file_stats = FileStats::new(
                file_path.to_string_lossy().into_owned(),
                process_ndjson_file_path(&settings, &file_path)?,
            );

            file_stats.stats.print()?;
            if settings.args.merge {
                file_stats_list.push(file_stats)
            }
        }
        if settings.args.merge {
            println!("Overall Stats");
            let overall_file_stats: Stats = file_stats_list.iter().sum();
            overall_file_stats.print()?;
        }
        return Ok(());
    }
    Ok(())
}

fn run_no_stdin_par(settings: Settings) -> Result<()> {
    if let Some(file_path) = &settings.args.file_path {
        let file_stats = process_ndjson_file_path_par(&settings, file_path)?;

        file_stats.print()?;
        return Ok(());
    }

    if let Some(pattern) = &settings.args.glob {
        let mut file_stats_list = Vec::new();

        println!("Glob '{}':", pattern);
        for entry in glob(pattern)? {
            let file_path = entry?;
            println!("File '{}':", file_path.display());
            let file_stats = FileStats::new(
                file_path.to_string_lossy().into_owned(),
                process_ndjson_file_path_par(&settings, &file_path)?,
            );

            file_stats.stats.print()?;
            if settings.args.merge {
                file_stats_list.push(file_stats)
            }
        }
        if settings.args.merge {
            println!("Overall Stats");
            let overall_file_stats: Stats = file_stats_list.iter().sum();
            overall_file_stats.print()?;
        }
        return Ok(());
    }
    Ok(())
}

fn print_completions(args: Cli) {
    let mut cmd = Cli::into_app();
    let shell = args
        .generate_completions
        .expect("function only called when argument specified");
    let bin_name = cmd.get_name().to_string();
    clap_complete::generate(shell, &mut cmd, bin_name, &mut io::stdout());
}

pub fn run(args: Cli) -> Result<()> {
    let now = Instant::now();
    let settings = Settings::init(args)?;
    if settings.args.generate_completions.is_some() {
        print_completions(settings.args);
        return Ok(());
    } else if is_readable_stdin() {
        if settings.args.parallel {
            run_stdin_par(settings)?;
        } else {
            run_stdin(settings)?;
        }
    } else if settings.args == Cli::default() {
        let mut cmd = Cli::command();
        cmd.print_help()?;
        return Ok(());
    } else if settings.args.parallel {
        run_no_stdin_par(settings)?;
    } else {
        run_no_stdin(settings)?;
    }
    eprintln!("Completed in {}", format_duration(now.elapsed()));
    Ok(())
}

#[test]
fn verify_cli() {
    use clap::CommandFactory;
    Cli::command().debug_assert()
}