-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathcli_gen_cli.rs
More file actions
125 lines (114 loc) · 3.88 KB
/
cli_gen_cli.rs
File metadata and controls
125 lines (114 loc) · 3.88 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
use crate::cli_gen_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::Uno => Self::cli_uno(),
}
}
pub fn cli_uno() -> ::clap::Command {
::clap::Command::new("")
.arg(
::clap::Arg::new("gateway")
.long("gateway")
.value_parser(::clap::value_parser!(::std::string::String))
.required(true),
)
.arg(
::clap::Arg::new("json-body")
.long("json-body")
.value_name("JSON-FILE")
.required(true)
.value_parser(::clap::value_parser!(std::path::PathBuf))
.help("Path to a file that contains the full json body."),
)
.arg(
::clap::Arg::new("json-body-template")
.long("json-body-template")
.action(::clap::ArgAction::SetTrue)
.help("XXX"),
)
}
pub async fn execute(
&self,
cmd: CliCommand,
matches: &::clap::ArgMatches,
) -> anyhow::Result<()> {
match cmd {
CliCommand::Uno => self.execute_uno(matches).await,
}
}
pub async fn execute_uno(&self, matches: &::clap::ArgMatches) -> anyhow::Result<()> {
let mut request = self.client.uno();
if let Some(value) = matches.get_one::<::std::string::String>("gateway") {
request = request.gateway(value.clone());
}
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
let body_txt = std::fs::read_to_string(value)
.with_context(|| format!("failed to read {}", value.display()))?;
let body_value = serde_json::from_str::<types::UnoBody>(&body_txt)
.with_context(|| format!("failed to parse {}", value.display()))?;
request = request.body(body_value);
}
self.config.execute_uno(matches, &mut request)?;
let result = request.send().await;
match result {
Ok(r) => {
todo!()
}
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_uno(
&self,
matches: &::clap::ArgMatches,
request: &mut builder::Uno,
) -> anyhow::Result<()> {
Ok(())
}
}
#[derive(Copy, Clone, Debug)]
pub enum CliCommand {
Uno,
}
impl CliCommand {
pub fn iter() -> impl Iterator<Item = CliCommand> {
vec![CliCommand::Uno].into_iter()
}
pub fn operation_id(&self) -> &'static str {
match self {
CliCommand::Uno => "uno",
}
}
}