Skip to content

Commit e001429

Browse files
author
Jason Ozias
committed
Closes #14: Implement the 'update' sub-command for 'cmd' config
1 parent 1b94edc commit e001429

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/cmd/command.rs

+36
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,40 @@ pub fn remove_cmd(config: &mut Config, matches: &ArgMatches) -> Result<i32> {
120120
}
121121
}
122122

123+
/// Run the `cmd-update` sub-command.
124+
pub fn update_cmd(config: &mut Config, matches: &ArgMatches) -> Result<i32> {
125+
if let Some(name) = matches.value_of("name") {
126+
let toml = match MusshToml::new(config) {
127+
Ok(toml) => toml,
128+
Err(_) => Default::default(),
129+
};
130+
131+
let cmds = toml.cmd();
132+
133+
if let Some(cmd) = cmds.get(name) {
134+
let mut mut_cmd = cmd.clone();
135+
let mut mut_toml = toml.clone();
136+
if let Some(cmd_arg) = matches.value_of("cmd") {
137+
mut_cmd.set_command(cmd_arg);
138+
}
139+
140+
mut_toml.add_cmd(name, mut_cmd);
141+
142+
match cmd::write_toml(config, &mut_toml) {
143+
Ok(i) => {
144+
info!(config.stdout(), "'{}' updated successfully", name);
145+
Ok(i)
146+
}
147+
Err(e) => Err(e),
148+
}
149+
} else {
150+
Err(ErrorKind::HostDoesNotExist.into())
151+
}
152+
} else {
153+
Err(ErrorKind::SubCommand.into())
154+
}
155+
}
156+
123157
/// Run the `host` sub-command.
124158
pub fn cmd(config: &mut Config, sub_m: &ArgMatches, stderr: &Logger) -> Result<i32> {
125159
match sub_m.subcommand() {
@@ -129,6 +163,8 @@ pub fn cmd(config: &mut Config, sub_m: &ArgMatches, stderr: &Logger) -> Result<i
129163
("add", Some(matches)) => add_cmd(config, matches),
130164
// 'cmd-remove' subcommand
131165
("remove", Some(matches)) => remove_cmd(config, matches),
166+
// 'cmd-update' subcommand
167+
("update", Some(matches)) => update_cmd(config, matches),
132168
_ => Err(ErrorKind::SubCommand.into()),
133169
}
134170
}

src/run.rs

+13
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ pub fn run() -> Result<i32> {
4949
.help("The command string")
5050
.index(2)
5151
.required(true)))
52+
.subcommand(SubCommand::with_name("update")
53+
.about("Update 'cmd' configuration")
54+
.arg(Arg::with_name("name")
55+
.value_name("NAME")
56+
.help("The assigned 'cmd' name")
57+
.index(1)
58+
.required(true))
59+
.arg(Arg::with_name("cmd")
60+
.value_name("CMD")
61+
.help("The command to update the \
62+
'cmd' with")
63+
.index(2)
64+
.required(true)))
5265
.subcommand(SubCommand::with_name("remove")
5366
.about("Remove a 'cmd' configuration")
5467
.arg(Arg::with_name("name")

0 commit comments

Comments
 (0)