-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
144 lines (127 loc) · 4.33 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
mod quest;
use std::error::Error;
use std::time::Duration;
use std::{fs::File, path::PathBuf};
use clap::{Args, Parser, Subcommand};
use quest::QuestFile;
fn main() {
env_logger::init();
QuestCli::parse().run();
}
fn print_version() -> &'static str {
Box::leak(format!("v{}", env!("CARGO_PKG_VERSION")).into())
}
#[derive(Clone, Debug, Parser)]
#[command(name = "quest")]
#[command(version = print_version(), about = "Cli for all the http quests you may go on.", long_about = None)]
struct QuestCli {
#[arg(
short,
long,
global = true,
default_value = "./.quests",
help = "File to source quests from"
)]
file: PathBuf,
#[arg(
short,
long,
global = true,
default_value = "./.env",
help = "Load environment variables from file"
)]
env: PathBuf,
#[command(subcommand)]
command: Commands,
}
impl QuestCli {
pub fn run(self) {
log::debug!("{:?}", self);
if dotenvy::from_path(&self.env).is_ok() {
log::debug!("Environment loaded from {:?}", self.env);
}
let f = File::open(&self.file).expect("Could not open quest file.");
let questfile: QuestFile = serde_yaml::from_reader(f).expect("Could not parse quest file.");
match self.command {
Commands::Go(SendArgs {
name,
var,
header,
param,
timeout,
gzip,
deflate,
brotli,
}) => {
let quest = questfile
.retrieve(&name)
.expect("Could not find quest with matching name.");
let url = questfile
.url(quest, var, param)
.expect("Could not construct url");
let headers = questfile.headers(quest, header);
let client = reqwest::blocking::ClientBuilder::new()
.gzip(gzip)
.deflate(deflate)
.brotli(brotli)
.build()
.unwrap();
let mut req = client
.request(quest.method.into(), url)
.headers(headers)
.timeout(Duration::from_secs(timeout));
if let Some(json) = &quest.json {
log::debug!("{:?}", json);
req = req
.body(json.to_owned())
.header("Content-Type", "application/json");
}
if let Some(body) = &quest.body {
req = req.body(body.to_owned());
}
let resp = req.send().unwrap();
println!("{}", resp.text().unwrap());
}
Commands::Ls => {
println!("{:?}", self.file);
questfile.pretty_print()
}
}
}
}
#[derive(Clone, Debug, Subcommand)]
enum Commands {
Go(SendArgs),
Ls,
}
#[derive(Clone, Debug, Args)]
struct SendArgs {
#[arg()]
name: String,
#[arg(short, long, value_parser = parse_key_val::<String, String>, help = "Overrides or adds value. Can be used multiple times")]
var: Vec<(String, String)>,
#[arg(short = 'H', long, value_parser = parse_key_val::<String, String>, help = "Overrides or adds value. Can be used multiple times")]
header: Vec<(String, String)>,
#[arg(short, long, value_parser = parse_key_val::<String, String>, help = "Overrides or adds value. Can be used multiple times")]
param: Vec<(String, String)>,
#[arg(short, long, default_value = "30", help = "Timeout seconds")]
timeout: u64,
#[arg(long, default_value = "false", help = "Use gzip compression")]
gzip: bool,
#[arg(long, default_value = "false", help = "Use deflate compression")]
deflate: bool,
#[arg(long, default_value = "false", help = "Use brotli compression")]
brotli: bool,
}
fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn Error + Send + Sync + 'static>>
where
T: std::str::FromStr,
T::Err: Error + Send + Sync + 'static,
U: std::str::FromStr,
U::Err: Error + Send + Sync + 'static,
{
let pos = s
.find('=')
.ok_or_else(|| format!("invalid key=value: no `=` found in `{s}`"))?;
Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
}