Skip to content

Commit 8a3e180

Browse files
committed
Untested Nix provider for Command
1 parent 2abc66d commit 8a3e180

File tree

5 files changed

+115
-134
lines changed

5 files changed

+115
-134
lines changed

core/src/command/mod.rs

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,83 +8,51 @@
88
99
mod providers;
1010

11-
pub use self::providers::Macos;
11+
pub use self::providers::Nix;
1212

13+
use erased_serde::Serialize;
1314
use errors::*;
1415
use ExecutableProvider;
1516
use host::Host;
16-
use self::providers::MacosRemoteProvider;
17-
18-
pub trait TelemetryProvider<'a> {
19-
fn available(&Host) -> bool where Self: Sized;
20-
fn try_new(&'a Host) -> Option<Self> where Self: Sized;
21-
fn load(&self) -> Result<Telemetry>;
22-
}
17+
use self::providers::NixRemoteProvider;
2318

2419
#[derive(Serialize, Deserialize)]
25-
pub enum RemoteProvider {
26-
Macos(MacosRemoteProvider)
27-
}
28-
29-
impl <'de>ExecutableProvider<'de> for RemoteProvider {
30-
fn exec(&self, host: &Host) -> Result<()> {
31-
match *self {
32-
RemoteProvider::Macos(ref p) => p.exec(host)
33-
}
34-
}
20+
pub struct Command {
21+
shell: String,
22+
cmd: String,
3523
}
3624

3725
#[derive(Debug, Serialize, Deserialize)]
38-
pub struct Telemetry {
39-
pub cpu: Cpu,
40-
pub fs: Vec<FsMount>,
41-
pub hostname: String,
42-
pub memory: u64,
43-
pub net: Vec<Netif>,
44-
pub os: Os,
26+
pub struct CommandResult {
27+
pub success: bool,
28+
pub exit_code: Option<i32>,
29+
pub stdout: Vec<u8>,
30+
pub stderr: Vec<u8>,
4531
}
4632

47-
#[derive(Debug, Serialize, Deserialize)]
48-
pub struct Cpu {
49-
pub vendor: String,
50-
pub brand_string: String,
51-
pub cores: u32,
52-
}
53-
54-
#[derive(Debug, Serialize, Deserialize)]
55-
pub struct FsMount {
56-
pub filesystem: String,
57-
pub mountpoint: String,
58-
pub size: u64,
59-
pub used: u64,
60-
pub available: u64,
61-
pub capacity: f32,
33+
pub trait CommandProvider<'a> {
34+
fn available(&Host) -> bool where Self: Sized;
35+
fn try_new<S: Into<String>>(&'a Host, S, Option<&str>) -> Option<Self> where Self: Sized;
36+
fn exec(&self) -> Result<CommandResult>;
6237
}
6338

64-
#[derive(Debug, Serialize, Deserialize)]
65-
pub struct Netif {
66-
pub name: String,
67-
pub index: u32,
68-
pub mac: Option<String>,
69-
pub ips: Option<Vec<String>>,
70-
pub flags: u32,
39+
#[derive(Serialize, Deserialize)]
40+
pub enum RemoteProvider {
41+
Nix(NixRemoteProvider)
7142
}
7243

73-
#[derive(Debug, Serialize, Deserialize)]
74-
pub struct Os {
75-
pub arch: String,
76-
pub family: String,
77-
pub platform: String,
78-
pub version_str: String,
79-
pub version_maj: u32,
80-
pub version_min: u32,
81-
pub version_patch: u32,
44+
impl <'de>ExecutableProvider<'de> for RemoteProvider {
45+
fn exec(self, host: &Host) -> Result<Box<Serialize>> {
46+
match self {
47+
RemoteProvider::Nix(p) => p.exec(host)
48+
}
49+
}
8250
}
8351

84-
pub fn factory<'a>(host: &'a Host) -> Result<Box<TelemetryProvider + 'a>> {
85-
if let Some(p) = Macos::try_new(host) {
52+
pub fn factory<'a, S: Into<String>>(host: &'a Host, cmd: S, shell: Option<&str>) -> Result<Box<CommandProvider<'a> + 'a>> {
53+
if let Some(p) = Nix::try_new(host, cmd, shell) {
8654
Ok(Box::new(p))
8755
} else {
88-
Err(ErrorKind::ProviderUnavailable("Telemetry").into())
56+
Err(ErrorKind::ProviderUnavailable("Command").into())
8957
}
9058
}

core/src/command/providers/macos.rs

Lines changed: 0 additions & 71 deletions
This file was deleted.

core/src/command/providers/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
55
// modified, or distributed except according to those terms.
66

7-
mod macos;
7+
mod nix;
88

9-
pub use self::macos::{Macos, RemoteProvider as MacosRemoteProvider};
9+
pub use self::nix::{Nix, RemoteProvider as NixRemoteProvider};

core/src/command/providers/nix.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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::{Command, CommandProvider, CommandResult};
8+
use erased_serde::Serialize;
9+
use errors::*;
10+
use ExecutableProvider;
11+
use host::*;
12+
use std::process;
13+
14+
pub struct Nix<'a> {
15+
host: &'a Host,
16+
inner: Command
17+
}
18+
19+
#[derive(Serialize, Deserialize)]
20+
pub enum RemoteProvider {
21+
Available,
22+
Exec(Command),
23+
}
24+
25+
impl <'de>ExecutableProvider<'de> for RemoteProvider {
26+
fn exec(self, host: &Host) -> Result<Box<Serialize>> {
27+
match self {
28+
RemoteProvider::Available => Ok(Box::new(Nix::available(host))),
29+
RemoteProvider::Exec(inner) => {
30+
let p = Nix { host, inner };
31+
Ok(Box::new(p.exec()?))
32+
}
33+
}
34+
}
35+
}
36+
37+
impl <'a>CommandProvider<'a> for Nix<'a> {
38+
fn available(host: &Host) -> bool {
39+
if host.is_local() {
40+
cfg!(not(windows))
41+
} else {
42+
unimplemented!();
43+
// let r = RemoteProvider::Available;
44+
// self.host.send(r).chain_err(|| ErrorKind::RemoteProvider("Command", "available"))?;
45+
// Ok(self.host.recv()?)
46+
}
47+
}
48+
49+
fn try_new<S: Into<String>>(host: &'a Host, cmd: S, shell: Option<&str>) -> Option<Nix<'a>> {
50+
if Self::available(host) {
51+
let inner = Command {
52+
shell: shell.unwrap_or("/bin/sh").into(),
53+
cmd: cmd.into(),
54+
};
55+
Some(Nix { host, inner })
56+
} else {
57+
None
58+
}
59+
}
60+
61+
fn exec(&self) -> Result<CommandResult> {
62+
if self.host.is_local() {
63+
let out = process::Command::new(&self.inner.shell)
64+
.arg(&self.inner.cmd)
65+
.output()
66+
.chain_err(|| "Command execution failed")?;
67+
Ok(CommandResult {
68+
success: out.status.success(),
69+
exit_code: out.status.code(),
70+
stdout: out.stdout,
71+
stderr: out.stderr
72+
})
73+
} else {
74+
unimplemented!();
75+
// let r = RemoteProvider::Load;
76+
// self.host.send(r).chain_err(|| ErrorKind::RemoteProvider("Command", "exec"))?;
77+
// let result: CommandResult = self.host.recv()?;
78+
// Ok(result)
79+
}
80+
}
81+
}

core/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern crate serde;
2121
#[macro_use] extern crate serde_derive;
2222
extern crate serde_json;
2323

24+
pub mod command;
2425
pub mod errors;
2526
pub mod host;
2627
mod target;
@@ -37,13 +38,15 @@ pub trait ExecutableProvider<'de>: serde::Serialize + serde::Deserialize<'de> {
3738

3839
#[derive(Serialize, Deserialize)]
3940
pub enum RemoteProvider {
40-
Telemetry(telemetry::RemoteProvider)
41+
Command(command::RemoteProvider),
42+
Telemetry(telemetry::RemoteProvider),
4143
}
4244

4345
impl <'de>ExecutableProvider<'de> for RemoteProvider {
4446
fn exec(self, host: &host::Host) -> Result<Box<Serialize>> {
4547
match self {
46-
RemoteProvider::Telemetry(p) => p.exec(host)
48+
RemoteProvider::Command(p) => p.exec(host),
49+
RemoteProvider::Telemetry(p) => p.exec(host),
4750
}
4851
}
4952
}

0 commit comments

Comments
 (0)