]>
Commit | Line | Data |
---|---|---|
9d10f611 JN |
1 | use std::env; |
2 | use std::error; | |
3 | use std::fs; | |
4 | use std::io::{self, BufReader}; | |
5 | use std::io::prelude::*; | |
6 | ||
7 | ||
8 | pub fn run(config: Config) -> Result<(), Box<dyn error::Error>> { | |
9 | ||
10 | let paths = fs::read_dir(config.dir_path).unwrap(); | |
11 | ||
12 | for path in paths { | |
13 | let path_str = &path.unwrap().path().display().to_string(); | |
14 | let md = std::fs::metadata(path_str).unwrap(); | |
15 | if ! md.is_dir() { | |
16 | let result = search(&config.tag, path_str); | |
17 | if result.unwrap() { | |
18 | println!("{}", path_str); | |
19 | } | |
20 | } | |
21 | } | |
22 | ||
23 | Ok(()) | |
24 | } | |
25 | ||
26 | pub struct Config { | |
27 | tag: String, | |
28 | dir_path: String, | |
29 | } | |
30 | ||
31 | impl Config { | |
32 | pub fn new(args: &[String]) -> Result<Config, &'static str> { | |
33 | ||
34 | if args.len() < 3 { | |
35 | return Err("Not enough arguments"); | |
36 | } | |
37 | ||
38 | let tag = args[1].clone(); | |
39 | let dir_path = args[2].clone(); | |
40 | ||
41 | Ok(Config {tag , dir_path}) | |
42 | } | |
43 | } | |
44 | ||
45 | ||
46 | fn search<'a>(query: &str, path: &'a str) -> io::Result<bool> { | |
47 | let mut result = false; | |
48 | ||
49 | let f = fs::File::open(path)?; | |
50 | let f = BufReader::new(f); | |
51 | ||
52 | let start_sequence = &env::var("TAG_FIND_START_SEQUENCE").unwrap_or("#".to_string()); | |
53 | ||
54 | for line in f.lines() { | |
55 | let line_str = line.unwrap(); | |
56 | if line_str.starts_with(start_sequence) && line_str.contains(query) { | |
57 | result = true; | |
58 | break; | |
59 | } | |
60 | } | |
61 | ||
62 | Ok(result) | |
63 | } |