aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 151b27762488a8dfb3e2edfd9767c03a8408b0fb (plain)
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
mod barbfile;
mod executor;
mod output;
use barbfile::BarbFile;
use clap::Parser;
use dotenv::dotenv;
use executor::{Context, Executor};
use output::BarbOutput;
use std::env;
use std::slice::Iter;

#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
    #[clap(short, long)]
    headers: bool,
    #[clap(short, long)]
    all_headers: bool,
    #[clap(short, long)]
    body: bool,
    #[clap(short, long)]
    raw: bool,
    #[clap(short, long)]
    filter: Option<String>,
    #[clap(short, long)]
    no_color: bool,
    #[clap(short = 'F', long)]
    no_filter: bool,
    files: Vec<String>,
}

impl Args {
    pub fn files_iter(&self) -> Iter<String> {
        self.files.iter()
    }

    pub fn jq_filter(&self) -> &Option<String> {
        &self.filter
    }

    pub fn no_filter(&self) -> bool {
        self.no_filter
    }

    pub fn output(&self) -> BarbOutput {
        BarbOutput::new(
            !self.body,
            self.all_headers,
            self.headers || self.all_headers || !self.body,
            !self.headers || self.body,
            self.raw,
            !self.no_color,
        )
    }
}

fn read_file_barb(file_name: &String) -> Result<BarbFile, String> {
    BarbFile::from_file(file_name.to_string())
        .map_err(|_| String::from(format!("Failed to parse file {}", file_name)))
}

fn main() {
    let args = Args::parse();
    dotenv().ok();
    let mut executor = Executor::new(Context::new(env::vars()));
    let output = args.output();

    let files: Vec<Result<BarbFile, String>> = args.files_iter()
        .map(read_file_barb)
        .collect();

    let (maybe_deps, errors): (Vec<Result<BarbFile, String>>, Vec<Result<BarbFile, String>>) = files.iter()
        .map(|x| match x.as_ref().ok() {
            Some(bfile) => bfile.dependency(),
            None => None
        })
        .filter(|x| x.is_some())
        .map(|x| read_file_barb(&String::from(x.unwrap())))
        .partition(|x| x.is_ok());

    for e in errors {
        println!("{}", e.err().unwrap());
    }

    let mut dependencies = maybe_deps.iter()
        .map(|x| x.as_ref().unwrap())
        .collect::<Vec<&BarbFile>>();
    dependencies.sort();
    dependencies.dedup();

    for dep in dependencies {
        // Always enable filters on dependencies
        match executor.execute(&dep, &output, args.jq_filter(), false) {
            Ok(()) => (),
            Err(err) => println!("{}", err),
        }
    }

    for bfile in files {
        if let Err(e) = bfile {
            println!("{}", e);
            continue;
        }
        
        match executor.execute(&bfile.unwrap(), &output, args.jq_filter(), args.no_filter()) {
            Ok(()) => (),
            Err(err) => println!("{}", err),
        }
    }
}