|
| 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 | +use tokio_core::reactor::Handle; |
| 19 | +use tokio_process::CommandExt; |
| 20 | + |
| 21 | +#[derive(Clone)] |
| 22 | +pub struct Generic; |
| 23 | +struct LocalGeneric; |
| 24 | +struct RemoteGeneric; |
| 25 | + |
| 26 | +#[doc(hidden)] |
| 27 | +#[derive(Serialize, Deserialize)] |
| 28 | +pub enum GenericRunnable { |
| 29 | + Available, |
| 30 | + Exec(String, Vec<String>), |
| 31 | +} |
| 32 | + |
| 33 | +impl<H: Host + 'static> Provider<H> for Generic { |
| 34 | + fn available(host: &H) -> Box<Future<Item = bool, Error = Error>> { |
| 35 | + match host.get_type() { |
| 36 | + HostType::Local(_) => LocalGeneric::available(), |
| 37 | + HostType::Remote(r) => RemoteGeneric::available(r), |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + fn try_new(host: &H) -> Box<Future<Item = Option<Generic>, Error = Error>> { |
| 42 | + let host = host.clone(); |
| 43 | + Box::new(Self::available(&host) |
| 44 | + .and_then(|available| { |
| 45 | + if available { |
| 46 | + future::ok(Some(Generic)) |
| 47 | + } else { |
| 48 | + future::ok(None) |
| 49 | + } |
| 50 | + })) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl<H: Host + 'static> CommandProvider<H> for Generic { |
| 55 | + fn exec(&self, host: &H, handle: &Handle, cmd: &str, shell: &[String]) -> Box<Future<Item = CommandResult, Error = Error>> { |
| 56 | + match host.get_type() { |
| 57 | + HostType::Local(_) => LocalGeneric::exec(handle, cmd, shell), |
| 58 | + HostType::Remote(r) => RemoteGeneric::exec(r, cmd, shell), |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl LocalGeneric { |
| 64 | + fn available() -> Box<Future<Item = bool, Error = Error>> { |
| 65 | + Box::new(future::ok(cfg!(unix))) |
| 66 | + } |
| 67 | + |
| 68 | + fn exec(handle: &Handle, cmd: &str, shell: &[String]) -> Box<Future<Item = CommandResult, Error = Error>> { |
| 69 | + let cmd = cmd.to_owned(); |
| 70 | + let shell = shell.to_owned(); |
| 71 | + let (shell, shell_args) = match shell.split_first() { |
| 72 | + Some((s, a)) => (s, a), |
| 73 | + None => return Box::new(future::err("Invalid shell provided".into())), |
| 74 | + }; |
| 75 | + |
| 76 | + Box::new(process::Command::new(shell) |
| 77 | + .args(shell_args) |
| 78 | + .arg(&cmd) |
| 79 | + .output_async(handle) |
| 80 | + .chain_err(|| "Command execution failed") |
| 81 | + .and_then(|output| { |
| 82 | + future::ok(CommandResult { |
| 83 | + success: output.status.success(), |
| 84 | + exit_code: output.status.code(), |
| 85 | + stdout: output.stdout, |
| 86 | + stderr: output.stderr, |
| 87 | + }) |
| 88 | + })) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl RemoteGeneric { |
| 93 | + fn available(host: &Plain) -> Box<Future<Item = bool, Error = Error>> { |
| 94 | + let runnable = Runnable::Command( |
| 95 | + CommandRunnable::Generic( |
| 96 | + GenericRunnable::Available)); |
| 97 | + host.run(runnable) |
| 98 | + .chain_err(|| ErrorKind::Runnable { endpoint: "Command::Generic", func: "available" }) |
| 99 | + } |
| 100 | + |
| 101 | + fn exec(host: &Plain, cmd: &str, shell: &[String]) -> Box<Future<Item = CommandResult, Error = Error>> { |
| 102 | + let runnable = Runnable::Command( |
| 103 | + CommandRunnable::Generic( |
| 104 | + GenericRunnable::Exec(cmd.into(), shell.to_owned()))); |
| 105 | + host.run(runnable) |
| 106 | + .chain_err(|| ErrorKind::Runnable { endpoint: "Command::Generic", func: "exec" }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl Executable for GenericRunnable { |
| 111 | + fn exec(self, _: &Local, handle: &Handle) -> Box<Future<Item = Box<Serialize>, Error = Error>> { |
| 112 | + match self { |
| 113 | + GenericRunnable::Available => Box::new(LocalGeneric::available().map(|b| Box::new(b) as Box<Serialize>)), |
| 114 | + GenericRunnable::Exec(cmd, shell) => Box::new(LocalGeneric::exec(handle, &cmd, &shell).map(|r| Box::new(r) as Box<Serialize>)), |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments