use argh::FromArgs;
#[derive(Debug, Eq, FromArgs, PartialEq)]
pub struct Update {
#[argh(subcommand)]
pub cmd: Command,
}
#[derive(Debug, Eq, FromArgs, PartialEq)]
#[argh(subcommand)]
pub enum Command {
Channel(Channel),
CheckNow(CheckNow),
MonitorUpdates(MonitorUpdates),
ForceInstall(ForceInstall),
WaitForCommit(WaitForCommit),
Revert(Revert),
}
#[derive(Debug, Eq, FromArgs, PartialEq)]
#[argh(subcommand, name = "channel")]
pub struct Channel {
#[argh(subcommand)]
pub cmd: channel::Command,
}
pub mod channel {
use argh::FromArgs;
#[derive(Debug, Eq, FromArgs, PartialEq)]
#[argh(subcommand)]
pub enum Command {
Get(Get),
Target(Target),
Set(Set),
List(List),
}
#[derive(Debug, Eq, FromArgs, PartialEq)]
#[argh(subcommand, name = "get")]
pub struct Get {}
#[derive(Debug, Eq, FromArgs, PartialEq)]
#[argh(subcommand, name = "target")]
pub struct Target {}
#[derive(Debug, Eq, FromArgs, PartialEq)]
#[argh(subcommand, name = "set")]
pub struct Set {
#[argh(positional)]
pub channel: String,
}
#[derive(Debug, Eq, FromArgs, PartialEq)]
#[argh(subcommand, name = "list")]
pub struct List {}
}
#[derive(Debug, Eq, FromArgs, PartialEq)]
#[argh(subcommand, name = "check-now")]
pub struct CheckNow {
#[argh(switch)]
pub service_initiated: bool,
#[argh(switch)]
pub monitor: bool,
}
#[derive(Debug, Eq, FromArgs, PartialEq)]
#[argh(subcommand, name = "monitor-updates")]
pub struct MonitorUpdates {}
#[derive(Debug, Eq, FromArgs, PartialEq)]
#[argh(subcommand, name = "force-install")]
pub struct ForceInstall {
#[argh(option, default = "true")]
pub reboot: bool,
#[argh(positional)]
pub update_pkg_url: String,
#[argh(switch)]
pub service_initiated: bool,
}
#[derive(Debug, Eq, FromArgs, PartialEq)]
#[argh(subcommand, name = "wait-for-commit")]
pub struct WaitForCommit {}
#[derive(Debug, Eq, FromArgs, PartialEq)]
#[argh(subcommand, name = "revert")]
pub struct Revert {}
#[cfg(test)]
mod tests {
use super::*;
use assert_matches::assert_matches;
#[test]
fn test_unknown_option() {
assert_matches!(Update::from_args(&["update"], &["--unknown"]), Err(_));
}
#[test]
fn test_unknown_subcommand() {
assert_matches!(Update::from_args(&["update"], &["unknown"]), Err(_));
}
#[test]
fn test_channel_get() {
let update = Update::from_args(&["update"], &["channel", "get"]).unwrap();
assert_eq!(
update,
Update {
cmd: Command::Channel(Channel { cmd: channel::Command::Get(channel::Get {}) })
}
);
}
#[test]
fn test_channel_target() {
let update = Update::from_args(&["update"], &["channel", "target"]).unwrap();
assert_eq!(
update,
Update {
cmd: Command::Channel(Channel {
cmd: channel::Command::Target(channel::Target {})
})
}
);
}
#[test]
fn test_channel_set() {
let update = Update::from_args(&["update"], &["channel", "set", "new-channel"]).unwrap();
assert_eq!(
update,
Update {
cmd: Command::Channel(Channel {
cmd: channel::Command::Set(channel::Set { channel: "new-channel".to_string() })
})
}
);
}
#[test]
fn test_channel_list() {
let update = Update::from_args(&["update"], &["channel", "list"]).unwrap();
assert_eq!(
update,
Update {
cmd: Command::Channel(Channel { cmd: channel::Command::List(channel::List {}) })
}
);
}
#[test]
fn test_check_now() {
let update = Update::from_args(&["update"], &["check-now"]).unwrap();
assert_eq!(
update,
Update {
cmd: Command::CheckNow(CheckNow { service_initiated: false, monitor: false })
}
);
}
#[test]
fn test_check_now_monitor() {
let update = Update::from_args(&["update"], &["check-now", "--monitor"]).unwrap();
assert_eq!(
update,
Update { cmd: Command::CheckNow(CheckNow { service_initiated: false, monitor: true }) }
);
}
#[test]
fn test_check_now_service_initiated() {
let update = Update::from_args(&["update"], &["check-now", "--service-initiated"]).unwrap();
assert_eq!(
update,
Update { cmd: Command::CheckNow(CheckNow { service_initiated: true, monitor: false }) }
);
}
#[test]
fn test_force_install_requires_positional_arg() {
assert_matches!(Update::from_args(&["update"], &["force-install"]), Err(_));
}
#[test]
fn test_force_install() {
let update = Update::from_args(&["update"], &["force-install", "url"]).unwrap();
assert_eq!(
update,
Update {
cmd: Command::ForceInstall(ForceInstall {
update_pkg_url: "url".to_owned(),
reboot: true,
service_initiated: false,
})
}
);
}
#[test]
fn test_force_install_no_reboot() {
let update =
Update::from_args(&["update"], &["force-install", "--reboot", "false", "url"]).unwrap();
assert_eq!(
update,
Update {
cmd: Command::ForceInstall(ForceInstall {
update_pkg_url: "url".to_owned(),
reboot: false,
service_initiated: false,
})
}
);
}
#[test]
fn test_force_install_service_initiated() {
let update =
Update::from_args(&["update"], &["force-install", "--service-initiated", "url"])
.unwrap();
assert_eq!(
update,
Update {
cmd: Command::ForceInstall(ForceInstall {
update_pkg_url: "url".to_owned(),
reboot: true,
service_initiated: true,
})
}
);
}
#[test]
fn test_wait_for_commit() {
let update = Update::from_args(&["update"], &["wait-for-commit"]).unwrap();
assert_eq!(update, Update { cmd: Command::WaitForCommit(WaitForCommit {}) });
}
#[test]
fn test_revert() {
let update = Update::from_args(&["update"], &["revert"]).unwrap();
assert_eq!(update, Update { cmd: Command::Revert(Revert {}) });
}
#[test]
fn test_monitor() {
let update = Update::from_args(&["update"], &["monitor-updates"]).unwrap();
assert_eq!(update, Update { cmd: Command::MonitorUpdates(MonitorUpdates {}) });
}
}