summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/location.rs
diff options
context:
space:
mode:
authorEmīls Piņķis <emils@mullvad.net>2019-05-28 17:33:55 +0100
committerEmīls Piņķis <emils@mullvad.net>2019-05-28 17:33:55 +0100
commitec6674bd044b866f388d6e68f8380e669c067193 (patch)
tree589c784398b81ef09bb98d69fbf12750b55c9921 /mullvad-cli/src/location.rs
parent5da250dc85c96f3f83b6b1c1d490e6afb5f2db53 (diff)
parent52e91fba03b8f1acbc07f73864445b8e85089875 (diff)
downloadmullvadvpn-ec6674bd044b866f388d6e68f8380e669c067193.tar.xz
mullvadvpn-ec6674bd044b866f388d6e68f8380e669c067193.zip
Merge branch 'shadowsocks-cli'
Diffstat (limited to 'mullvad-cli/src/location.rs')
-rw-r--r--mullvad-cli/src/location.rs69
1 files changed, 69 insertions, 0 deletions
diff --git a/mullvad-cli/src/location.rs b/mullvad-cli/src/location.rs
new file mode 100644
index 0000000000..10b27b61d8
--- /dev/null
+++ b/mullvad-cli/src/location.rs
@@ -0,0 +1,69 @@
+use mullvad_types::relay_constraints::{Constraint, LocationConstraint};
+
+pub fn get_subcommand() -> clap::App<'static, 'static> {
+ clap::SubCommand::with_name("location")
+ .arg(
+ clap::Arg::with_name("country")
+ .help("The two letter country code, or 'any' for no preference.")
+ .required(true)
+ .index(1)
+ .validator(country_code_validator),
+ )
+ .arg(
+ clap::Arg::with_name("city")
+ .help("The three letter city code")
+ .index(2)
+ .validator(city_code_validator),
+ )
+ .arg(
+ clap::Arg::with_name("hostname")
+ .help("The hostname")
+ .index(3),
+ )
+}
+
+pub fn get_constraint(matches: &clap::ArgMatches<'_>) -> Constraint<LocationConstraint> {
+ let country = matches.value_of("country").unwrap();
+ let city = matches.value_of("city");
+ let hostname = matches.value_of("hostname");
+
+ match (country, city, hostname) {
+ ("any", None, None) => Constraint::Any,
+ ("any", ..) => clap::Error::with_description(
+ "City can't be given when selecting 'any' country",
+ clap::ErrorKind::InvalidValue,
+ )
+ .exit(),
+ (country, None, None) => Constraint::Only(LocationConstraint::Country(country.to_owned())),
+ (country, Some(city), None) => Constraint::Only(LocationConstraint::City(
+ country.to_owned(),
+ city.to_owned(),
+ )),
+ (country, Some(city), Some(hostname)) => Constraint::Only(LocationConstraint::Hostname(
+ country.to_owned(),
+ city.to_owned(),
+ hostname.to_owned(),
+ )),
+ (..) => clap::Error::with_description(
+ "Invalid country, city and hostname combination given",
+ clap::ErrorKind::InvalidValue,
+ )
+ .exit(),
+ }
+}
+
+fn country_code_validator(code: String) -> ::std::result::Result<(), String> {
+ if code.len() == 2 || code == "any" {
+ Ok(())
+ } else {
+ Err(String::from("Country codes must be two letters, or 'any'."))
+ }
+}
+
+fn city_code_validator(code: String) -> ::std::result::Result<(), String> {
+ if code.len() == 3 {
+ Ok(())
+ } else {
+ Err(String::from("City codes must be three letters"))
+ }
+}