diff options
| author | David Lönnhager <david.l@mullvad.net> | 2023-05-03 11:20:31 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2023-05-03 11:20:31 +0200 |
| commit | 49ea114adddba1a1db6ffc6c440e743c01797a47 (patch) | |
| tree | 66f1bf1e3e1d208e233e5622045503abe85a3a89 /mullvad-cli/src/cmds/mod.rs | |
| parent | beaa6d3b80d9c9dfed99c710c793830db3ddc7ec (diff) | |
| parent | aade46c9c73c874e4153caa450e713d8f8b37760 (diff) | |
| download | mullvadvpn-49ea114adddba1a1db6ffc6c440e743c01797a47.tar.xz mullvadvpn-49ea114adddba1a1db6ffc6c440e743c01797a47.zip | |
Merge branch 'update-clap'
Diffstat (limited to 'mullvad-cli/src/cmds/mod.rs')
| -rw-r--r-- | mullvad-cli/src/cmds/mod.rs | 138 |
1 files changed, 66 insertions, 72 deletions
diff --git a/mullvad-cli/src/cmds/mod.rs b/mullvad-cli/src/cmds/mod.rs index 4c374d64ee..7adcf6c65b 100644 --- a/mullvad-cli/src/cmds/mod.rs +++ b/mullvad-cli/src/cmds/mod.rs @@ -1,86 +1,80 @@ -use crate::Command; -use std::collections::HashMap; +use clap::builder::{PossibleValuesParser, TypedValueParser, ValueParser}; +use std::ops::Deref; -mod account; -pub use self::account::Account; +pub mod account; +pub mod auto_connect; +pub mod beta_program; +pub mod bridge; +pub mod dns; +pub mod lan; +pub mod lockdown; +pub mod obfuscation; +pub mod relay; +pub mod relay_constraints; +pub mod reset; +pub mod split_tunnel; +pub mod status; +pub mod tunnel; +pub mod tunnel_state; +pub mod version; -mod auto_connect; -pub use self::auto_connect::AutoConnect; - -mod beta_program; -pub use self::beta_program::BetaProgram; - -mod block_when_disconnected; -pub use self::block_when_disconnected::BlockWhenDisconnected; - -mod bridge; -pub use self::bridge::Bridge; - -mod connect; -pub use self::connect::Connect; - -mod disconnect; -pub use self::disconnect::Disconnect; - -mod dns; -pub use self::dns::Dns; - -mod lan; -pub use self::lan::Lan; +/// A value parser that parses "on" or "off" into a boolean +#[derive(Debug, Clone, Copy)] +pub struct BooleanOption { + state: bool, + on_label: &'static str, + off_label: &'static str, +} -mod obfuscation; -pub use self::obfuscation::Obfuscation; +impl Deref for BooleanOption { + type Target = bool; -mod reconnect; -pub use self::reconnect::Reconnect; + fn deref(&self) -> &Self::Target { + &self.state + } +} -mod relay; -pub use self::relay::Relay; +impl clap::builder::ValueParserFactory for BooleanOption { + type Parser = ValueParser; -mod reset; -pub use self::reset::Reset; + /// A value parser that parses "on" or "off" into a `BooleanOption` + fn value_parser() -> Self::Parser { + Self::custom_parser("on", "off") + } +} -#[cfg(any(target_os = "linux", windows))] -mod split_tunnel; -#[cfg(any(target_os = "linux", windows))] -pub use self::split_tunnel::SplitTunnel; +impl BooleanOption { + /// A value parser that parses `on_label` and `off_label` into a `BooleanOption` + fn custom_parser(on_label: &'static str, off_label: &'static str) -> ValueParser { + assert!(on_label != off_label); -mod status; -pub use self::status::Status; + ValueParser::new( + PossibleValuesParser::new([on_label, off_label]) + .map(move |val| Self::with_labels(val == on_label, on_label, off_label)), + ) + } -mod tunnel; -pub use self::tunnel::Tunnel; + fn with_labels(state: bool, on_label: &'static str, off_label: &'static str) -> Self { + Self { + state, + on_label, + off_label, + } + } +} -mod version; -pub use self::version::Version; +impl From<bool> for BooleanOption { + fn from(state: bool) -> Self { + Self::with_labels(state, "on", "off") + } +} -/// Returns a map of all available subcommands with their name as key. -pub fn get_commands() -> HashMap<&'static str, Box<dyn Command>> { - let commands: Vec<Box<dyn Command>> = vec![ - Box::new(Account), - Box::new(AutoConnect), - Box::new(BetaProgram), - Box::new(BlockWhenDisconnected), - Box::new(Bridge), - Box::new(Connect), - Box::new(Disconnect), - Box::new(Dns), - Box::new(Reconnect), - Box::new(Lan), - Box::new(Obfuscation), - Box::new(Relay), - Box::new(Reset), - #[cfg(any(target_os = "linux", windows))] - Box::new(SplitTunnel), - Box::new(Status), - Box::new(Tunnel), - Box::new(Version), - ]; - let mut map = HashMap::new(); - for cmd in commands { - if map.insert(cmd.name(), cmd).is_some() { - panic!("Multiple commands with the same name"); +impl std::fmt::Display for BooleanOption { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if self.state { + self.on_label.fmt(f) + } else { + self.off_label.fmt(f) } } - map } |
