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
use anyhow::{Context, Result};
use clap::builder::styling::AnsiColor;
use clap::builder::Styles;
use clap::CommandFactory;
use clap::Parser;
use clap_complete::Shell;
use glob::glob;
use grep_cli::is_readable_stdin;
use humantime::format_duration;
use json::ndjson::JSONStats;
use json::ndjson::StatsResult;
use serde_json_path::JsonPath;
use std::io;
use std::path::PathBuf;
use std::time::Instant;

use crate::json::ndjson;

mod io_helpers;
pub mod json;

fn styles() -> Styles {
    Styles::styled()
        .header(AnsiColor::Yellow.on_default())
        .usage(AnsiColor::Green.on_default())
        .literal(AnsiColor::Green.on_default())
        .placeholder(AnsiColor::Green.on_default())
}

#[derive(Parser, Default, PartialEq, Eq)]
#[clap(author, version, about, long_about = None, styles = styles())]
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[*]`?
    /// See also `--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], ...)
    /// See also `--inspect-arrays`
    #[clap(long, conflicts_with = "inspect_arrays")]
    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<JsonPath>> {
        let jsonpath_selector = if let Some(jsonpath) = &self.jsonpath {
            let path = JsonPath::parse(jsonpath)
                .with_context(|| format!("Failed to parse jsonpath query string: {jsonpath}"))?;
            Some(path)
        } else {
            None
        };
        Ok(jsonpath_selector)
    }
}

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

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

fn process_ndjson_file_path(settings: &Settings, file_path: &PathBuf) -> Result<ndjson::Stats> {
    let StatsResult { stats, errors } = file_path.json_stats(settings).with_context(|| {
        format!(
            "Failed to collect stats for JSON file: {}",
            file_path.display()
        )
    })?;

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

    Ok(stats)
}

fn run_stdin(settings: Settings) -> Result<()> {
    let StatsResult { stats, errors } = io::stdin()
        .json_stats(&settings)
        .context("Failed to collect stats for JSON stdin")?;

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

    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);
        let file_paths = glob(pattern).context(
            "Failed to parse glob pattern, try quoting '<pattern>' to avoid shell parsing",
        )?;
        for entry in file_paths {
            let file_path = entry?;
            println!("File '{}':", file_path.display());
            let file_stats = ndjson::FileStats::new(
                file_path.to_string_lossy().into_owned(),
                process_ndjson_file_path(&settings, &file_path)?,
            );

            file_stats.stats.print().with_context(|| {
                format!("Failed to print stats for file: {}", file_path.display())
            })?;
            if settings.args.merge {
                file_stats_list.push(file_stats)
            }
        }
        if settings.args.merge {
            println!("Overall Stats");
            let overall_file_stats: ndjson::Stats = file_stats_list.iter().sum();
            overall_file_stats
                .print()
                .context("Failed to print combined stats")?;
        }
        return Ok(());
    }
    Ok(())
}

fn print_completions(args: Cli) {
    let mut cmd = Cli::command();
    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).context("Failed to initialise settings from CLI args")?;
    if settings.args.generate_completions.is_some() {
        print_completions(settings.args);
        return Ok(());
    } else if is_readable_stdin() {
        run_stdin(settings).context("Failed to process stdin")?;
    } else if settings.args == Cli::default() {
        let mut cmd = Cli::command();
        cmd.print_help().context("Failed to pring CLI help")?;
        return Ok(());
    } else {
        run_no_stdin(settings).context("Failed to process file(s)")?;
    }
    eprintln!("Completed in {}", format_duration(now.elapsed()));
    Ok(())
}

#[test]
fn verify_cli() {
    Cli::command().debug_assert()
}