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
|
#![deny(rust_2018_idioms)]
use clap::{crate_authors, crate_description, crate_name};
use mullvad_ipc_client::{new_standalone_ipc_client, DaemonRpcClient};
use std::io;
use talpid_types::ErrorExt;
mod cmds;
mod location;
pub const PRODUCT_VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/product-version.txt"));
pub type Result<T> = std::result::Result<T, Error>;
#[derive(err_derive::Error, Debug)]
pub enum Error {
#[error(display = "Failed to connect to daemon")]
DaemonNotRunning(#[error(source)] io::Error),
#[error(display = "Can't subscribe to daemon states")]
CantSubscribe(#[error(source)] mullvad_ipc_client::PubSubError),
#[error(display = "Failed to communicate with mullvad-daemon over RPC")]
RpcClientError(#[error(source)] mullvad_ipc_client::Error),
/// The given command is not correct in some way
#[error(display = "Invalid command: {}", _0)]
InvalidCommand(&'static str),
}
pub fn new_rpc_client() -> Result<DaemonRpcClient> {
match new_standalone_ipc_client(&mullvad_paths::get_rpc_socket_path()) {
Err(e) => Err(Error::DaemonNotRunning(e)),
Ok(client) => Ok(client),
}
}
fn main() {
let exit_code = match run() {
Ok(_) => 0,
Err(error) => {
eprintln!("{}", error.display_chain());
1
}
};
std::process::exit(exit_code);
}
fn run() -> Result<()> {
env_logger::init();
let commands = cmds::get_commands();
let app = clap::App::new(crate_name!())
.version(PRODUCT_VERSION)
.author(crate_authors!())
.about(crate_description!())
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
.global_settings(&[
clap::AppSettings::DisableHelpSubcommand,
clap::AppSettings::VersionlessSubcommands,
])
.subcommands(commands.values().map(|cmd| cmd.clap_subcommand()));
let app_matches = app.get_matches();
let (subcommand_name, subcommand_matches) = app_matches.subcommand();
if let Some(cmd) = commands.get(subcommand_name) {
cmd.run(subcommand_matches.expect("No command matched"))
} else {
unreachable!("No command matched");
}
}
pub trait Command {
fn name(&self) -> &'static str;
fn clap_subcommand(&self) -> clap::App<'static, 'static>;
fn run(&self, matches: &clap::ArgMatches<'_>) -> Result<()>;
}
|