-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathparam_collision_cli.rs
More file actions
161 lines (146 loc) · 5.07 KB
/
param_collision_cli.rs
File metadata and controls
161 lines (146 loc) · 5.07 KB
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
use crate::param_collision_builder::*;
use anyhow::Context as _;
pub struct Cli<T: CliConfig> {
client: Client,
config: T,
}
impl<T: CliConfig> Cli<T> {
pub fn new(client: Client, config: T) -> Self {
Self { client, config }
}
pub fn get_command(cmd: CliCommand) -> ::clap::Command {
match cmd {
CliCommand::KeyGet => Self::cli_key_get(),
}
}
pub fn cli_key_get() -> ::clap::Command {
::clap::Command::new("")
.arg(
::clap::Arg::new("client")
.long("client")
.value_parser(::clap::value_parser!(bool))
.required(true)
.help("Parameter name that was previously colliding"),
)
.arg(
::clap::Arg::new("query")
.long("query")
.value_parser(::clap::value_parser!(bool))
.required(true)
.help("Parameter name that was previously colliding"),
)
.arg(
::clap::Arg::new("request")
.long("request")
.value_parser(::clap::value_parser!(bool))
.required(true)
.help("Parameter name that was previously colliding"),
)
.arg(
::clap::Arg::new("response")
.long("response")
.value_parser(::clap::value_parser!(bool))
.required(true)
.help("Parameter name that was previously colliding"),
)
.arg(
::clap::Arg::new("result")
.long("result")
.value_parser(::clap::value_parser!(bool))
.required(true)
.help("Parameter name that was previously colliding"),
)
.arg(
::clap::Arg::new("url")
.long("url")
.value_parser(::clap::value_parser!(bool))
.required(true)
.help("Parameter name that was previously colliding"),
)
.long_about("Gets a key")
}
pub async fn execute(
&self,
cmd: CliCommand,
matches: &::clap::ArgMatches,
) -> anyhow::Result<()> {
match cmd {
CliCommand::KeyGet => self.execute_key_get(matches).await,
}
}
pub async fn execute_key_get(&self, matches: &::clap::ArgMatches) -> anyhow::Result<()> {
let mut request = self.client.key_get();
if let Some(value) = matches.get_one::<bool>("client") {
request = request.client(value.clone());
}
if let Some(value) = matches.get_one::<bool>("query") {
request = request.query(value.clone());
}
if let Some(value) = matches.get_one::<bool>("request") {
request = request.request(value.clone());
}
if let Some(value) = matches.get_one::<bool>("response") {
request = request.response(value.clone());
}
if let Some(value) = matches.get_one::<bool>("result") {
request = request.result(value.clone());
}
if let Some(value) = matches.get_one::<bool>("url") {
request = request.url(value.clone());
}
self.config.execute_key_get(matches, &mut request)?;
let result = request.send().await;
match result {
Ok(r) => {
self.config.success_no_item(&r);
Ok(())
}
Err(r) => {
self.config.error(&r);
Err(anyhow::Error::new(r))
}
}
}
}
pub trait CliConfig {
fn success_item<T>(&self, value: &ResponseValue<T>)
where
T: std::clone::Clone + schemars::JsonSchema + serde::Serialize + std::fmt::Debug;
fn success_no_item(&self, value: &ResponseValue<()>);
fn error<T>(&self, value: &Error<T>)
where
T: std::clone::Clone + schemars::JsonSchema + serde::Serialize + std::fmt::Debug;
fn list_start<T>(&self)
where
T: std::clone::Clone + schemars::JsonSchema + serde::Serialize + std::fmt::Debug;
fn list_item<T>(&self, value: &T)
where
T: std::clone::Clone + schemars::JsonSchema + serde::Serialize + std::fmt::Debug;
fn list_end_success<T>(&self)
where
T: std::clone::Clone + schemars::JsonSchema + serde::Serialize + std::fmt::Debug;
fn list_end_error<T>(&self, value: &Error<T>)
where
T: std::clone::Clone + schemars::JsonSchema + serde::Serialize + std::fmt::Debug;
fn execute_key_get(
&self,
matches: &::clap::ArgMatches,
request: &mut builder::KeyGet,
) -> anyhow::Result<()> {
Ok(())
}
}
#[derive(Copy, Clone, Debug)]
pub enum CliCommand {
KeyGet,
}
impl CliCommand {
pub fn iter() -> impl Iterator<Item = CliCommand> {
vec![CliCommand::KeyGet].into_iter()
}
pub fn operation_id(&self) -> &'static str {
match self {
CliCommand::KeyGet => "key_get",
}
}
}