|
| 1 | +// Copyright 2015-2017 Intecture Developers. |
| 2 | +// |
| 3 | +// Licensed under the Mozilla Public License 2.0 <LICENSE or |
| 4 | +// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied, |
| 5 | +// modified, or distributed except according to those terms. |
| 6 | + |
| 7 | +use command::CommandResult; |
| 8 | +use erased_serde::Serialize; |
| 9 | +use errors::*; |
| 10 | +use futures::{future, Future}; |
| 11 | +use host::{Host, HostType}; |
| 12 | +use host::local::Local; |
| 13 | +use host::remote::Plain; |
| 14 | +use provider::Provider; |
| 15 | +use remote::{Executable, Runnable}; |
| 16 | +use std::process; |
| 17 | +use super::{CommandProvider, CommandRunnable}; |
| 18 | + |
| 19 | +#[derive(Clone)] |
| 20 | +pub struct Generic<H: Host> { |
| 21 | + host: H |
| 22 | +} |
| 23 | + |
| 24 | +struct LocalGeneric; |
| 25 | +struct RemoteGeneric; |
| 26 | + |
| 27 | +#[doc(hidden)] |
| 28 | +#[derive(Serialize, Deserialize)] |
| 29 | +pub enum GenericRunnable { |
| 30 | + Available, |
| 31 | + Exec(String, Vec<String>), |
| 32 | +} |
| 33 | + |
| 34 | +impl<H: Host + 'static> Provider<H> for Generic<H> { |
| 35 | + fn available(host: &H) -> Box<Future<Item = bool, Error = Error>> { |
| 36 | + match host.get_type() { |
| 37 | + HostType::Local(l) => LocalGeneric::available(l), |
| 38 | + HostType::Remote(r) => RemoteGeneric::available(r), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + fn try_new(host: &H) -> Box<Future<Item = Option<Generic<H>>, Error = Error>> { |
| 43 | + let host = host.clone(); |
| 44 | + Box::new(Self::available(&host) |
| 45 | + .and_then(|available| { |
| 46 | + if available { |
| 47 | + future::ok(Some(Generic { host })) |
| 48 | + } else { |
| 49 | + future::ok(None) |
| 50 | + } |
| 51 | + })) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl<H: Host + 'static> CommandProvider<H> for Generic<H> { |
| 56 | + fn exec(&mut self, cmd: &str, shell: &[String]) -> Box<Future<Item = CommandResult, Error = Error>> { |
| 57 | + match self.host.get_type() { |
| 58 | + HostType::Local(l) => LocalGeneric::exec(l, cmd, shell), |
| 59 | + HostType::Remote(r) => RemoteGeneric::exec(r, cmd, shell), |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl LocalGeneric { |
| 65 | + fn available(_: &Local) -> Box<Future<Item = bool, Error = Error>> { |
| 66 | + Box::new(future::ok(cfg!(unix))) |
| 67 | + } |
| 68 | + |
| 69 | + fn exec(_: &Local, cmd: &str, shell: &[String]) -> Box<Future<Item = CommandResult, Error = Error>> { |
| 70 | + let cmd_owned = cmd.to_owned(); |
| 71 | + let shell_owned = shell.to_owned(); |
| 72 | + |
| 73 | + Box::new(future::lazy(move || -> future::FutureResult<CommandResult, Error> { |
| 74 | + let (shell, shell_args) = match shell_owned.split_first() { |
| 75 | + Some((s, a)) => (s, a), |
| 76 | + None => return future::err("Invalid shell provided".into()), |
| 77 | + }; |
| 78 | + |
| 79 | + let out = process::Command::new(shell) |
| 80 | + .args(shell_args) |
| 81 | + .arg(&cmd_owned) |
| 82 | + .output() |
| 83 | + .chain_err(|| "Command execution failed"); |
| 84 | + match out { |
| 85 | + Ok(output) => future::ok(CommandResult { |
| 86 | + success: output.status.success(), |
| 87 | + exit_code: output.status.code(), |
| 88 | + stdout: output.stdout, |
| 89 | + stderr: output.stderr, |
| 90 | + }), |
| 91 | + Err(e) => future::err(e), |
| 92 | + } |
| 93 | + })) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl RemoteGeneric { |
| 98 | + fn available(host: &Plain) -> Box<Future<Item = bool, Error = Error>> { |
| 99 | + let runnable = Runnable::Command( |
| 100 | + CommandRunnable::Generic( |
| 101 | + GenericRunnable::Available)); |
| 102 | + host.run(runnable) |
| 103 | + .chain_err(|| ErrorKind::Runnable { endpoint: "Command::Generic", func: "available" }) |
| 104 | + } |
| 105 | + |
| 106 | + fn exec(host: &Plain, cmd: &str, shell: &[String]) -> Box<Future<Item = CommandResult, Error = Error>> { |
| 107 | + let runnable = Runnable::Command( |
| 108 | + CommandRunnable::Generic( |
| 109 | + GenericRunnable::Exec(cmd.into(), shell.to_owned()))); |
| 110 | + host.run(runnable) |
| 111 | + .chain_err(|| ErrorKind::Runnable { endpoint: "Command::Generic", func: "exec" }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl Executable for GenericRunnable { |
| 116 | + fn exec(self, host: &Local) -> Box<Future<Item = Box<Serialize>, Error = Error>> { |
| 117 | + match self { |
| 118 | + GenericRunnable::Available => Box::new(LocalGeneric::available(host).map(|b| Box::new(b) as Box<Serialize>)), |
| 119 | + GenericRunnable::Exec(cmd, shell) => Box::new(LocalGeneric::exec(host, &cmd, &shell).map(|r| Box::new(r) as Box<Serialize>)), |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments