-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rs
127 lines (111 loc) · 4.3 KB
/
config.rs
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
use getopts::Options;
use serde::Deserialize;
use serde_yaml;
use std::process::exit;
use std::{env, error::Error, fs};
#[derive(Deserialize, Debug)]
pub struct Config {
pub start_row: Option<usize>,
pub end_row: Option<usize>,
pub base_url: Option<String>,
pub test_file: Option<String>, // Add this line
pub worksheet: Option<String>,
pub verbose: bool,
pub token_key: Option<String>,
pub groups: Option<Vec<(Option<String>, String)>>,
}
impl Config {
pub fn default() -> Self {
Config {
start_row: None,
end_row: None,
base_url: None,
test_file: None,
worksheet: None,
verbose: false,
token_key: None,
groups: None,
}
}
pub fn build_config() -> Result<Self, Box<dyn Error>> {
let args: Vec<String> = env::args().collect();
let mut opts = Options::new();
opts.optopt("s", "start_row", "Set the start row", "START_ROW");
opts.optopt("e", "end_row", "Set the end row", "END_ROW");
opts.optopt("b", "base_url", "Set the base URL", "BASE_URL");
opts.optopt("t", "test_file", "Set the test file", "TEST_FILE");
opts.optopt("w", "worksheet", "Set the worksheet", "WORKSHEET");
opts.optmulti("g", "groups", "Set the test groups", "GROUPS");
opts.optflag("h", "help", "Print this help menu");
opts.optflag("v", "verbose", "Print verbose information");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => panic!("{}", f.to_string()),
};
if matches.opt_present("h") {
print_usage(&args[0], opts);
exit(0);
}
let verbose = matches.opt_present("v");
let start_row = matches.opt_str("s").map(|s| s.parse::<usize>().unwrap());
let end_row = matches.opt_str("e").map(|e| e.parse::<usize>().unwrap());
let base_url = matches.opt_str("b");
let test_file = matches.opt_str("t");
let worksheet = matches.opt_str("w");
// If conflicting arguments bail out.
if (start_row.is_some() || end_row.is_some()) && worksheet.is_none() {
eprintln!("Error: start_row and end_row options are only applicable if a worksheet option is provided.");
exit(1);
}
let groups: Vec<(Option<String>, String)> = matches
.opt_strs("g")
.into_iter()
.map(|g| {
let split: Vec<&str> = g.split(|c| c == '.' || c == ':').collect();
match split.len() {
1 => (None, split[0].to_string()),
2 => (Some(split[0].to_string()), split[1].to_string()),
_ => panic!(
"Invalid group format: {}. Expected [worksheet_name.]group_name",
g
),
}
})
.collect();
// Read from config.yaml
// Get and print the current working directory for debugging
let current_dir = env::current_dir()?;
println!("Current working directory: {}", current_dir.display());
let config_file = fs::read_to_string("config.yaml")?;
let mut config: Config = serde_yaml::from_str(&config_file)?;
// Override with command line arguments if provided
if let Some(start_row) = start_row {
config.start_row = Some(start_row);
}
if let Some(end_row) = end_row {
config.end_row = Some(end_row);
}
if let Some(base_url) = base_url {
config.base_url = Some(base_url);
}
if let Some(test_file) = test_file {
// Add this line
config.test_file = Some(test_file);
}
if !groups.is_empty() {
config.groups = Some(groups);
}
config.verbose = verbose;
Ok(config)
}
}
fn print_usage(program: &str, opts: Options) {
let version = env!("CARGO_PKG_VERSION");
let program_name = program.split('/').last().unwrap_or(program);
let description = "A delusional framework for testing / breaking the REST APIs";
let brief = format!(
"{} {} version {}\nUsage: {} [options]",
program_name, version, description, program_name
);
print!("{}", opts.usage(&brief));
}