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
280
281
282
283
284
use clap::{App, Arg};
use log::{debug, error, LevelFilter, Metadata, Record};
use std::io::{self, Read};
use yaml_rust::{Yaml, YamlLoader};

use crate::{
    convert_length, convert_single_node, debug_print_doc_structure, parse_path, traverse,
    VisitedNode,
};

static LOGGER: SimpleLogger = SimpleLogger;

struct SimpleLogger;

impl log::Log for SimpleLogger {
    fn enabled(&self, _metadata: &Metadata) -> bool {
        true
    }

    fn log(&self, record: &Record) {
        if self.enabled(record.metadata()) {
            println!("{}: {}", record.level(), record.args());
        }
    }

    fn flush(&self) {}
}

#[derive(Debug)]
enum PrintMode {
    Value,
    Path,
    ValueAndPath,
}

fn parse_print_mode(mode: &str) -> PrintMode {
    match mode {
        "v" => PrintMode::Value,
        "p" => PrintMode::Path,
        "pv" => PrintMode::ValueAndPath,
        "vp" => PrintMode::ValueAndPath,
        _ => PrintMode::Value,
    }
}

pub fn run_cli() {
    let yaml_file_arg = "yaml_file";
    let path_expression_arg = "path_expression";
    let default_value_arg = "default_value";
    let length_arg = "length";
    let print_mode_arg = "print_mode";
    let collect_arg = "collect";
    let doc_idx_arg = "doc_idx";
    let debug_arg = "debug";

    let matches = App::new("ry")
        .version("0.0")
        .author("Will Deuschle")
        .about("structured search in yaml files")
        .arg(
            Arg::with_name(yaml_file_arg)
                .help("sets the input yaml file to use")
                .required(true)
                .index(1),
        )
        .arg(
            Arg::with_name(path_expression_arg)
                .help("path to search against")
                .required(true)
                .index(2),
        )
        .arg(
            Arg::with_name(default_value_arg)
                .takes_value(true)
                .help("default value to print if there are no matching nodes")
                .long("defaultValue"),
        )
        .arg(
            Arg::with_name(length_arg)
                .help("prints length of results")
                .long("length")
                .short("L"),
        )
        .arg(
            Arg::with_name(print_mode_arg)
                .takes_value(true)
                .help("what mode to print results in")
                .long("printMode")
                .short("p"),
        )
        .arg(
            Arg::with_name(collect_arg)
                .takes_value(false)
                .help("collect results into an array")
                .long("collect")
                .short("C"),
        )
        .arg(
            Arg::with_name(doc_idx_arg)
                .takes_value(true)
                .help("document index to search")
                .long("docIndex")
                .short("d"),
        )
        .arg(
            Arg::with_name(debug_arg)
                .help("enable debug logging")
                .long("debug")
                .short("v"),
        )
        .get_matches();

    let file_name = matches.value_of(yaml_file_arg).unwrap();
    let path = matches.value_of(path_expression_arg).unwrap();

    let log_level = if matches.is_present(debug_arg) {
        LevelFilter::Debug
    } else {
        LevelFilter::Error
    };
    let _ = log::set_logger(&LOGGER)
        .map(|()| log::set_max_level(log_level))
        .unwrap_or_else(|err| {
            eprintln!("failed to set logger: `{}`", err);
        });

    let docs_str = if file_name == "-" {
        let mut buffer = String::new();
        io::stdin()
            .read_to_string(&mut buffer)
            .unwrap_or_else(|err| {
                error!("failed to read from stdin: `{}`", err);
                std::process::exit(1);
            });
        buffer
    } else {
        std::fs::read_to_string(file_name).unwrap_or_else(|err| {
            error!("failed to read file `{}`: `{}`", file_name, err);
            std::process::exit(1);
        })
    };
    let mut docs: &[Yaml] = &YamlLoader::load_from_str(&docs_str).unwrap_or_else(|err| {
        error!("failed to load yaml file `{}`: `{}`", file_name, err);
        std::process::exit(1);
    });

    // Multi document support, doc is a yaml::Yaml
    if docs.len() == 0 {
        error!("no yaml documents found in file `{}`", file_name);
        std::process::exit(1);
    } else if matches.is_present(doc_idx_arg) {
        let doc_idx = matches.value_of(doc_idx_arg).unwrap();
        if doc_idx == "*" {
            debug!(
                "processing all `{}` documents in file `{}`",
                docs.len(),
                file_name
            );
        } else {
            let parsed_doc_idx = match doc_idx.parse::<usize>() {
                Ok(idx) => idx,
                Err(e) => {
                    error!(
                        "failed to parse document index `{}`, error: {:?}",
                        doc_idx, e
                    );
                    std::process::exit(1);
                }
            };
            if parsed_doc_idx >= docs.len() {
                error!("only `{}` documents are present in file `{}`, but document index `{}` was requested for searching",
                    docs.len(), file_name, parsed_doc_idx);
                std::process::exit(1);
            }
            debug!(
                "processing document at index `{}` in file `{}`",
                parsed_doc_idx, file_name
            );
            docs = &docs[parsed_doc_idx..parsed_doc_idx + 1]
        }
    } else {
        debug!(
            "processing all `{}` documents in file `{}`",
            docs.len(),
            file_name
        );
    }

    for ref doc in docs {
        if log_level == LevelFilter::Debug {
            debug_print_doc_structure(doc).unwrap_or_else(|err| {
                error!(
                    "unable to print display document from file `{}`: {}",
                    file_name, err
                );
                std::process::exit(1);
            });
        }

        // parse path
        let parsed_path = match parse_path(path) {
            Ok(parsed_path) => parsed_path,
            Err(e) => {
                error!("failed to parse path, error: {}", e);
                std::process::exit(1);
            }
        };
        debug!("parsed path: {:?}", parsed_path);

        let mut visited = Vec::<VisitedNode>::new();
        traverse(doc, "", &parsed_path, String::new(), false, &mut visited);

        let default_yml: Yaml;
        let default_visited_node: VisitedNode;
        if visited.len() == 0 && matches.is_present(default_value_arg) {
            let dv = matches.value_of(default_value_arg).unwrap();
            debug!("found no matches, using default value `{}`", dv);
            default_yml = Yaml::from_str(dv);
            default_visited_node = VisitedNode {
                yml: &default_yml,
                path: "".to_string(),
            };
            visited.push(default_visited_node);
        }
        debug!("matched values: {:?}", visited);

        let print_mode = parse_print_mode(matches.value_of(print_mode_arg).unwrap_or("v"));
        debug!("print_mode: {:?}", print_mode);

        let collect = matches.is_present(collect_arg);
        debug!("collect: {}", collect);

        if matches.is_present(length_arg) {
            // length mode
            if collect {
                // length and collect just prints the number of visited nodes
                println!("{}", visited.len());
            } else {
                match print_mode {
                    PrintMode::Path => {
                        for value in visited {
                            println!("{}", value.path);
                        }
                    }
                    PrintMode::Value => {
                        for value in visited {
                            println!("{}", convert_length(value.yml));
                        }
                    }
                    PrintMode::ValueAndPath => {
                        for value in visited {
                            println!("{}: {}", value.path, convert_length(value.yml));
                        }
                    }
                }
            }
        } else {
            // no length mode
            let collect_prepend = if collect { "- " } else { "" };
            match print_mode {
                PrintMode::Path => {
                    for value in visited {
                        println!("{}{}", collect_prepend, value.path);
                    }
                }
                PrintMode::Value => {
                    for value in visited {
                        println!("{}{}", collect_prepend, convert_single_node(value.yml));
                    }
                }
                PrintMode::ValueAndPath => {
                    for value in visited {
                        println!(
                            "{}{}: {}",
                            collect_prepend,
                            value.path,
                            convert_single_node(value.yml)
                        );
                    }
                }
            }
        }
    }
}