1 use clap::ArgMatches;
2 use ffsend_api::action::version::{Error as VersionError, Version as ApiVersion};
3 
4 use crate::client::create_config;
5 use crate::cmd::matcher::main::MainMatcher;
6 use crate::cmd::matcher::{version::VersionMatcher, Matcher};
7 use crate::error::ActionError;
8 
9 /// A file version action.
10 pub struct Version<'a> {
11     cmd_matches: &'a ArgMatches<'a>,
12 }
13 
14 impl<'a> Version<'a> {
15     /// Construct a new version action.
new(cmd_matches: &'a ArgMatches<'a>) -> Self16     pub fn new(cmd_matches: &'a ArgMatches<'a>) -> Self {
17         Self { cmd_matches }
18     }
19 
20     /// Invoke the version action.
21     // TODO: create a trait for this method
invoke(&self) -> Result<(), ActionError>22     pub fn invoke(&self) -> Result<(), ActionError> {
23         // Create the command matchers
24         let matcher_version = VersionMatcher::with(self.cmd_matches).unwrap();
25         let matcher_main = MainMatcher::with(self.cmd_matches).unwrap();
26 
27         // Get the host
28         let host = matcher_version.host();
29 
30         // Create a reqwest client
31         let client_config = create_config(&matcher_main);
32         let client = client_config.client(false);
33 
34         // Make sure the file version
35         let response = ApiVersion::new(host).invoke(&client);
36 
37         // Print the result
38         match response {
39             Ok(v) => println!("API version: {}", v),
40             Err(VersionError::Unknown) => println!("Version: unknown"),
41             Err(VersionError::Unsupported(v)) => println!("Version: {} (unsupported)", v),
42             Err(e) => return Err(e.into()),
43         }
44 
45         Ok(())
46     }
47 }
48 
49 #[derive(Debug, Fail)]
50 pub enum Error {
51     /// An error occurred while attempting to determine the Send server version.
52     #[fail(display = "failed to check the server version")]
53     Version(#[cause] VersionError),
54 }
55 
56 impl From<VersionError> for Error {
from(err: VersionError) -> Error57     fn from(err: VersionError) -> Error {
58         Error::Version(err)
59     }
60 }
61