-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmd_config.rs
336 lines (301 loc) · 12.2 KB
/
cmd_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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use crate::config::{ConfigOption, CONFIG_OPTIONS};
use anyhow::{bail, Result};
use clap::Parser;
// TODO: make this doc a function that parses from the config the options so it's not hardcoded
/// Manage configuration for `zoo`.
///
/// Current respected settings:
/// - editor: the text editor program to use for authoring text
/// - prompt: toggle interactive prompting in the terminal (default: "enabled")
/// - pager: the terminal pager program to send standard output to
/// - browser: the web browser to use for opening URLs
/// - format: the formatting style for command output
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdConfig {
#[clap(subcommand)]
subcmd: SubCommand,
}
#[derive(Parser, Debug, Clone)]
enum SubCommand {
Set(CmdConfigSet),
List(CmdConfigList),
Get(CmdConfigGet),
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdConfig {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
match &self.subcmd {
SubCommand::Get(cmd) => cmd.run(ctx).await,
SubCommand::Set(cmd) => cmd.run(ctx).await,
SubCommand::List(cmd) => cmd.run(ctx).await,
}
}
}
/// Print the value of a given configuration key.
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdConfigGet {
/// The key to get the value of.
#[clap(name = "key", required = true)]
pub key: String,
/// Get per-host setting.
#[clap(short = 'H', long, default_value = "")]
pub host: String,
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdConfigGet {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
match ctx.config.get(&self.host, &self.key) {
Ok(value) => writeln!(ctx.io.out, "{value}")?,
Err(err) => {
bail!("{}", err);
}
}
Ok(())
}
}
/// Update configuration with a value for the given key.
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdConfigSet {
/// The key to set the value of.
#[clap(name = "key", required = true)]
pub key: String,
/// The value to set.
#[clap(name = "value", required = true)]
pub value: String,
/// Set per-host setting.
#[clap(short = 'H', long, default_value = "")]
pub host: String,
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdConfigSet {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
crate::config::validate_key(&self.key)?;
crate::config::validate_value(&self.key, &self.value)?;
// Set the value. If self.host is empty it will be top-level set.
if let Err(err) = ctx.config.set(&self.host, &self.key, Some(&self.value)) {
bail!("{}", err);
}
// Unset the option in all other hosts if it's a mutually exclusive option.
if !self.host.is_empty() {
for option in CONFIG_OPTIONS {
if let &ConfigOption::HostLevel {
key,
mutually_exclusive,
..
} = option
{
if key != self.key || !mutually_exclusive {
continue;
}
for host in ctx.config.hosts()? {
// Skip the host that was the original value target
if host == self.host {
continue;
}
if let Err(err) = ctx.config.set(&host, &self.key, None) {
bail!("{}", err);
}
}
}
}
}
// Write the config file.
if let Err(err) = ctx.config.write() {
bail!("{}", err);
}
Ok(())
}
}
/// Print a list of configuration keys and values.
#[derive(Parser, Debug, Clone)]
#[clap(verbatim_doc_comment)]
pub struct CmdConfigList {
/// Get per-host configuration.
#[clap(short = 'H', long, default_value = "")]
pub host: String,
}
#[async_trait::async_trait(?Send)]
impl crate::cmd::Command for CmdConfigList {
async fn run(&self, ctx: &mut crate::context::Context) -> Result<()> {
let host = if self.host.is_empty() {
// We don't want to do the default host here since we want to show the default's for
// all hosts, even if ZOO_HOST is set.
// TODO: in this case we should print all the hosts configs, not just the default.
"".to_string()
} else {
self.host.to_string()
};
for option in CONFIG_OPTIONS {
if let &ConfigOption::TopLevel { key, description, .. } = option {
match ctx.config.get(&host, key) {
Ok(value) => writeln!(ctx.io.out, "{}\n{}={}\n", description, key, value)?,
Err(err) => {
if host.is_empty() {
// Only bail if the host is empty, since some hosts may not have
// all the options.
bail!("{err}");
}
}
}
}
}
Ok(())
}
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;
use crate::cmd::Command;
use crate::config;
use crate::config::Config;
pub struct TestItem {
name: String,
cmd: crate::cmd_config::SubCommand,
want_out: String,
want_err: String,
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_cmd_config() {
let tests: Vec<TestItem> = vec![
TestItem {
name: "list empty".to_string(),
cmd: crate::cmd_config::SubCommand::List(crate::cmd_config::CmdConfigList { host: "".to_string() }),
want_out: "the text editor program to use for authoring text\neditor=\n\ntoggle interactive prompting in the terminal\nprompt=enabled\n\nthe terminal pager program to send standard output to\npager=\n\nthe web browser to use for opening URLs\nbrowser=\n\nthe formatting style for command output\nformat=table\n\n".to_string(),
want_err: "".to_string(),
},
TestItem {
name: "set a key unknown".to_string(),
cmd: crate::cmd_config::SubCommand::Set(crate::cmd_config::CmdConfigSet {
key: "foo".to_string(),
value: "bar".to_string(),
host: "".to_string(),
}),
want_out: "".to_string(),
want_err: "invalid key: foo".to_string(),
},
TestItem {
name: "set a key".to_string(),
cmd: crate::cmd_config::SubCommand::Set(crate::cmd_config::CmdConfigSet {
key: "browser".to_string(),
value: "bar".to_string(),
host: "".to_string(),
}),
want_out: "".to_string(),
want_err: "".to_string(),
},
TestItem {
name: "set a key with host".to_string(),
cmd: crate::cmd_config::SubCommand::Set(crate::cmd_config::CmdConfigSet {
key: "prompt".to_string(),
value: "disabled".to_string(),
host: "example.org".to_string(),
}),
want_out: "".to_string(),
want_err: "".to_string(),
},
TestItem {
name: "get a key we set".to_string(),
cmd: crate::cmd_config::SubCommand::Get(crate::cmd_config::CmdConfigGet {
key: "browser".to_string(),
host: "".to_string(),
}),
want_out: "bar\n".to_string(),
want_err: "".to_string(),
},
TestItem {
name: "get a key we set with host".to_string(),
cmd: crate::cmd_config::SubCommand::Get(crate::cmd_config::CmdConfigGet {
key: "prompt".to_string(),
host: "example.org".to_string(),
}),
want_out: "disabled\n".to_string(),
want_err: "".to_string(),
},
TestItem {
name: "get a non existent key".to_string(),
cmd: crate::cmd_config::SubCommand::Get(crate::cmd_config::CmdConfigGet {
key: "blah".to_string(),
host: "".to_string(),
}),
want_out: "".to_string(),
want_err: "Key 'blah' not found".to_string(),
},
TestItem {
name: "list all default".to_string(),
cmd: crate::cmd_config::SubCommand::List(crate::cmd_config::CmdConfigList { host: "".to_string() }),
// want_out: "editor=\nprompt=enabled\npager=\nbrowser=bar\nformat=table\n".to_string(),
want_out: "the text editor program to use for authoring text\neditor=\n\ntoggle interactive prompting in the terminal\nprompt=enabled\n\nthe terminal pager program to send standard output to\npager=\n\nthe web browser to use for opening URLs\nbrowser=bar\n\nthe formatting style for command output\nformat=table\n\n".to_string(),
want_err: "".to_string(),
},
];
let mut config = crate::config::new_blank_config().unwrap();
let mut c = crate::config_from_env::EnvConfig::inherit_env(&mut config);
for t in tests {
let (io, stdout_path, stderr_path) = crate::iostreams::IoStreams::test();
let mut ctx = crate::context::Context {
config: &mut c,
io,
debug: false,
};
let cmd_config = crate::cmd_config::CmdConfig { subcmd: t.cmd };
match cmd_config.run(&mut ctx).await {
Ok(()) => {
let stdout = std::fs::read_to_string(stdout_path).unwrap();
let stderr = std::fs::read_to_string(stderr_path).unwrap();
assert_eq!(stdout, t.want_out, "test {}", t.name);
assert!(stderr.is_empty(), "test {}", t.name);
}
Err(err) => {
let stdout = std::fs::read_to_string(stdout_path).unwrap();
let stderr = std::fs::read_to_string(stderr_path).unwrap();
assert_eq!(stdout, t.want_out, "test {}", t.name);
assert_eq!(&err.to_string(), &t.want_err, "test {}", t.name);
assert!(stderr.is_empty(), "test {}", t.name);
}
}
}
}
#[tokio::test]
async fn test_hosts_default_mutually_exclusive() -> Result<(), anyhow::Error> {
let mut config = config::new_blank_config().unwrap();
assert!(config.set("example.com", "token", Some("abcdef")).is_ok());
assert!(config.set("zoo.computer", "token", Some("ghijkl")).is_ok());
assert_eq!(config.hosts()?.len(), 2);
let mut c = crate::config_from_env::EnvConfig::inherit_env(&mut config);
let (io, _stdout_path, _stderr_path) = crate::iostreams::IoStreams::test();
let mut ctx = crate::context::Context {
config: &mut c,
io,
debug: false,
};
let mut cmd_config = crate::cmd_config::CmdConfig {
subcmd: crate::cmd_config::SubCommand::Set(crate::cmd_config::CmdConfigSet {
key: "default".to_string(),
value: "true".to_string(),
host: "example.com".to_string(),
}),
};
cmd_config.run(&mut ctx).await?;
cmd_config = crate::cmd_config::CmdConfig {
subcmd: crate::cmd_config::SubCommand::Set(crate::cmd_config::CmdConfigSet {
key: "default".to_string(),
value: "true".to_string(),
host: "zoo.computer".to_string(),
}),
};
cmd_config.run(&mut ctx).await?;
let config_text = config.hosts_to_string().unwrap();
assert_eq!(
config_text,
r#"["example.com"]
token = "abcdef"
["zoo.computer"]
token = "ghijkl"
default = true"#
);
Ok(())
}
}