Skip to content

Commit 17706bd

Browse files
committed
Remove state from providers
1 parent 280f537 commit 17706bd

File tree

11 files changed

+140
-153
lines changed

11 files changed

+140
-153
lines changed

core/src/command/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const DEFAULT_SHELL: [&'static str; 2] = ["/bin/sh", "-c"];
1919
const DEFAULT_SHELL: [&'static str; 1] = ["yeah...we don't currently support windows :("];
2020

2121
pub struct Command<H: Host> {
22+
host: H,
2223
inner: Box<CommandProvider<H>>,
2324
shell: Vec<String>,
2425
cmd: String,
@@ -34,25 +35,28 @@ pub struct CommandResult {
3435

3536
impl<H: Host + 'static> Command<H> {
3637
pub fn new(host: &H, cmd: &str, shell: Option<&[&str]>) -> Box<Future<Item = Command<H>, Error = Error>> {
37-
let cmd_owned = cmd.to_owned();
38-
let shell_owned: Vec<String> = shell.unwrap_or(&DEFAULT_SHELL)
38+
let host = host.clone();
39+
let cmd = cmd.to_owned();
40+
let shell: Vec<String> = shell.unwrap_or(&DEFAULT_SHELL)
3941
.to_owned()
4042
.iter()
4143
.map(|s| s.to_string())
4244
.collect();
4345

4446
Box::new(host.command_provider()
4547
.and_then(|provider| future::ok(Command {
48+
host: host,
4649
inner: provider,
47-
shell: shell_owned,
48-
cmd: cmd_owned,
50+
shell: shell,
51+
cmd: cmd,
4952
})))
5053
}
5154

52-
pub fn with_provider<P>(provider: P, cmd: &str, shell: Option<&[&str]>) -> Command<H>
55+
pub fn with_provider<P>(host: &H, provider: P, cmd: &str, shell: Option<&[&str]>) -> Command<H>
5356
where P: CommandProvider<H> + 'static
5457
{
5558
Command {
59+
host: host.clone(),
5660
inner: Box::new(provider),
5761
shell: shell.unwrap_or(&DEFAULT_SHELL)
5862
.to_owned()
@@ -64,6 +68,6 @@ impl<H: Host + 'static> Command<H> {
6468
}
6569

6670
pub fn exec(&mut self) -> Box<Future<Item = CommandResult, Error = Error>> {
67-
self.inner.exec(&self.cmd, &self.shell)
71+
self.inner.exec(&self.host, &self.cmd, &self.shell)
6872
}
6973
}

core/src/command/providers/generic.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ use std::process;
1717
use super::{CommandProvider, CommandRunnable};
1818

1919
#[derive(Clone)]
20-
pub struct Generic<H: Host> {
21-
host: H
22-
}
23-
20+
pub struct Generic;
2421
struct LocalGeneric;
2522
struct RemoteGeneric;
2623

@@ -31,42 +28,42 @@ pub enum GenericRunnable {
3128
Exec(String, Vec<String>),
3229
}
3330

34-
impl<H: Host + 'static> Provider<H> for Generic<H> {
31+
impl<H: Host + 'static> Provider<H> for Generic {
3532
fn available(host: &H) -> Box<Future<Item = bool, Error = Error>> {
3633
match host.get_type() {
37-
HostType::Local(l) => LocalGeneric::available(l),
34+
HostType::Local(_) => LocalGeneric::available(),
3835
HostType::Remote(r) => RemoteGeneric::available(r),
3936
}
4037
}
4138

42-
fn try_new(host: &H) -> Box<Future<Item = Option<Generic<H>>, Error = Error>> {
39+
fn try_new(host: &H) -> Box<Future<Item = Option<Generic>, Error = Error>> {
4340
let host = host.clone();
4441
Box::new(Self::available(&host)
4542
.and_then(|available| {
4643
if available {
47-
future::ok(Some(Generic { host }))
44+
future::ok(Some(Generic))
4845
} else {
4946
future::ok(None)
5047
}
5148
}))
5249
}
5350
}
5451

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),
52+
impl<H: Host + 'static> CommandProvider<H> for Generic {
53+
fn exec(&self, host: &H, cmd: &str, shell: &[String]) -> Box<Future<Item = CommandResult, Error = Error>> {
54+
match host.get_type() {
55+
HostType::Local(_) => LocalGeneric::exec(cmd, shell),
5956
HostType::Remote(r) => RemoteGeneric::exec(r, cmd, shell),
6057
}
6158
}
6259
}
6360

6461
impl LocalGeneric {
65-
fn available(_: &Local) -> Box<Future<Item = bool, Error = Error>> {
62+
fn available() -> Box<Future<Item = bool, Error = Error>> {
6663
Box::new(future::ok(cfg!(unix)))
6764
}
6865

69-
fn exec(_: &Local, cmd: &str, shell: &[String]) -> Box<Future<Item = CommandResult, Error = Error>> {
66+
fn exec(cmd: &str, shell: &[String]) -> Box<Future<Item = CommandResult, Error = Error>> {
7067
let cmd_owned = cmd.to_owned();
7168
let shell_owned = shell.to_owned();
7269

@@ -113,10 +110,10 @@ impl RemoteGeneric {
113110
}
114111

115112
impl Executable for GenericRunnable {
116-
fn exec(self, host: &Local) -> Box<Future<Item = Box<Serialize>, Error = Error>> {
113+
fn exec(self, _: &Local) -> Box<Future<Item = Box<Serialize>, Error = Error>> {
117114
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>)),
115+
GenericRunnable::Available => Box::new(LocalGeneric::available().map(|b| Box::new(b) as Box<Serialize>)),
116+
GenericRunnable::Exec(cmd, shell) => Box::new(LocalGeneric::exec(&cmd, &shell).map(|r| Box::new(r) as Box<Serialize>)),
120117
}
121118
}
122119
}

core/src/command/providers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use self::generic::{Generic, GenericRunnable};
1717
use super::CommandResult;
1818

1919
pub trait CommandProvider<H: Host>: Provider<H> {
20-
fn exec(&mut self, &str, &[String]) -> Box<Future<Item = CommandResult, Error = Error>>;
20+
fn exec(&self, &H, &str, &[String]) -> Box<Future<Item = CommandResult, Error = Error>>;
2121
}
2222

2323
#[doc(hidden)]

core/src/telemetry/providers/centos.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ use target::{default, linux, redhat};
1919
use target::linux::LinuxFlavour;
2020
use telemetry::{Cpu, Os, OsFamily, OsPlatform, Telemetry, serializable};
2121

22-
pub struct Centos<H: Host> {
23-
host: H,
24-
}
25-
22+
pub struct Centos;
2623
struct LocalCentos;
2724
struct RemoteCentos;
2825

@@ -33,42 +30,42 @@ pub enum CentosRunnable {
3330
Load,
3431
}
3532

36-
impl<H: Host + 'static> Provider<H> for Centos<H> {
33+
impl<H: Host + 'static> Provider<H> for Centos {
3734
fn available(host: &H) -> Box<Future<Item = bool, Error = Error>> {
3835
match host.get_type() {
39-
HostType::Local(l) => LocalCentos::available(l),
36+
HostType::Local(_) => LocalCentos::available(),
4037
HostType::Remote(r) => RemoteCentos::available(r),
4138
}
4239
}
4340

44-
fn try_new(host: &H) -> Box<Future<Item = Option<Centos<H>>, Error = Error>> {
41+
fn try_new(host: &H) -> Box<Future<Item = Option<Centos>, Error = Error>> {
4542
let host = host.clone();
4643
Box::new(Self::available(&host)
4744
.and_then(|available| {
4845
if available {
49-
future::ok(Some(Centos { host }))
46+
future::ok(Some(Centos))
5047
} else {
5148
future::ok(None)
5249
}
5350
}))
5451
}
5552
}
5653

57-
impl<H: Host + 'static> TelemetryProvider<H> for Centos<H> {
58-
fn load(&mut self) -> Box<Future<Item = Telemetry, Error = Error>> {
59-
match self.host.get_type() {
60-
HostType::Local(l) => LocalCentos::load(l),
54+
impl<H: Host + 'static> TelemetryProvider<H> for Centos {
55+
fn load(&self, host: &H) -> Box<Future<Item = Telemetry, Error = Error>> {
56+
match host.get_type() {
57+
HostType::Local(_) => LocalCentos::load(),
6158
HostType::Remote(r) => RemoteCentos::load(r),
6259
}
6360
}
6461
}
6562

6663
impl LocalCentos {
67-
fn available(_: &Local) -> Box<Future<Item = bool, Error = Error>> {
64+
fn available() -> Box<Future<Item = bool, Error = Error>> {
6865
Box::new(future::ok(cfg!(target_os="linux") && linux::fingerprint_os() == Some(LinuxFlavour::Centos)))
6966
}
7067

71-
fn load(_: &Local) -> Box<Future<Item = Telemetry, Error = Error>> {
68+
fn load() -> Box<Future<Item = Telemetry, Error = Error>> {
7269
Box::new(future::lazy(|| match do_load() {
7370
Ok(t) => future::ok(t),
7471
Err(e) => future::err(e),
@@ -96,10 +93,10 @@ impl RemoteCentos {
9693
}
9794

9895
impl Executable for CentosRunnable {
99-
fn exec(self, host: &Local) -> Box<Future<Item = Box<Serialize>, Error = Error>> {
96+
fn exec(self, _: &Local) -> Box<Future<Item = Box<Serialize>, Error = Error>> {
10097
match self {
101-
CentosRunnable::Available => Box::new(LocalCentos::available(host).map(|b| Box::new(b) as Box<Serialize>)),
102-
CentosRunnable::Load => Box::new(LocalCentos::load(host).map(|t| {
98+
CentosRunnable::Available => Box::new(LocalCentos::available().map(|b| Box::new(b) as Box<Serialize>)),
99+
CentosRunnable::Load => Box::new(LocalCentos::load().map(|t| {
103100
let t: serializable::Telemetry = t.into();
104101
Box::new(t) as Box<Serialize>
105102
}))

core/src/telemetry/providers/debian.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ use target::{default, linux};
1919
use target::linux::LinuxFlavour;
2020
use telemetry::{Cpu, Os, OsFamily, OsPlatform, Telemetry, serializable};
2121

22-
pub struct Debian<H: Host> {
23-
host: H,
24-
}
25-
22+
pub struct Debian;
2623
struct LocalDebian;
2724
struct RemoteDebian;
2825

@@ -33,42 +30,42 @@ pub enum DebianRunnable {
3330
Load,
3431
}
3532

36-
impl<H: Host + 'static> Provider<H> for Debian<H> {
33+
impl<H: Host + 'static> Provider<H> for Debian {
3734
fn available(host: &H) -> Box<Future<Item = bool, Error = Error>> {
3835
match host.get_type() {
39-
HostType::Local(l) => LocalDebian::available(l),
36+
HostType::Local(_) => LocalDebian::available(),
4037
HostType::Remote(r) => RemoteDebian::available(r),
4138
}
4239
}
4340

44-
fn try_new(host: &H) -> Box<Future<Item = Option<Debian<H>>, Error = Error>> {
41+
fn try_new(host: &H) -> Box<Future<Item = Option<Debian>, Error = Error>> {
4542
let host = host.clone();
4643
Box::new(Self::available(&host)
4744
.and_then(|available| {
4845
if available {
49-
future::ok(Some(Debian { host }))
46+
future::ok(Some(Debian))
5047
} else {
5148
future::ok(None)
5249
}
5350
}))
5451
}
5552
}
5653

57-
impl<H: Host + 'static> TelemetryProvider<H> for Debian<H> {
58-
fn load(&mut self) -> Box<Future<Item = Telemetry, Error = Error>> {
59-
match self.host.get_type() {
60-
HostType::Local(l) => LocalDebian::load(l),
54+
impl<H: Host + 'static> TelemetryProvider<H> for Debian {
55+
fn load(&self, host: &H) -> Box<Future<Item = Telemetry, Error = Error>> {
56+
match host.get_type() {
57+
HostType::Local(_) => LocalDebian::load(),
6158
HostType::Remote(r) => RemoteDebian::load(r),
6259
}
6360
}
6461
}
6562

6663
impl LocalDebian {
67-
fn available(_: &Local) -> Box<Future<Item = bool, Error = Error>> {
64+
fn available() -> Box<Future<Item = bool, Error = Error>> {
6865
Box::new(future::ok(cfg!(target_os="linux") && linux::fingerprint_os() == Some(LinuxFlavour::Debian)))
6966
}
7067

71-
fn load(_: &Local) -> Box<Future<Item = Telemetry, Error = Error>> {
68+
fn load() -> Box<Future<Item = Telemetry, Error = Error>> {
7269
Box::new(future::lazy(|| match do_load() {
7370
Ok(t) => future::ok(t),
7471
Err(e) => future::err(e),
@@ -98,10 +95,10 @@ impl RemoteDebian {
9895
}
9996

10097
impl Executable for DebianRunnable {
101-
fn exec(self, host: &Local) -> Box<Future<Item = Box<Serialize>, Error = Error>> {
98+
fn exec(self, _: &Local) -> Box<Future<Item = Box<Serialize>, Error = Error>> {
10299
match self {
103-
DebianRunnable::Available => Box::new(LocalDebian::available(host).map(|b| Box::new(b) as Box<Serialize>)),
104-
DebianRunnable::Load => Box::new(LocalDebian::load(host).map(|t| {
100+
DebianRunnable::Available => Box::new(LocalDebian::available().map(|b| Box::new(b) as Box<Serialize>)),
101+
DebianRunnable::Load => Box::new(LocalDebian::load().map(|t| {
105102
let t: serializable::Telemetry = t.into();
106103
Box::new(t) as Box<Serialize>
107104
}))

core/src/telemetry/providers/fedora.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ use target::{default, linux, redhat};
1919
use target::linux::LinuxFlavour;
2020
use telemetry::{Cpu, Os, OsFamily, OsPlatform, Telemetry, serializable};
2121

22-
pub struct Fedora<H: Host> {
23-
host: H,
24-
}
25-
22+
pub struct Fedora;
2623
struct LocalFedora;
2724
struct RemoteFedora;
2825

@@ -33,42 +30,42 @@ pub enum FedoraRunnable {
3330
Load,
3431
}
3532

36-
impl<H: Host + 'static> Provider<H> for Fedora<H> {
33+
impl<H: Host + 'static> Provider<H> for Fedora {
3734
fn available(host: &H) -> Box<Future<Item = bool, Error = Error>> {
3835
match host.get_type() {
39-
HostType::Local(l) => LocalFedora::available(l),
36+
HostType::Local(_) => LocalFedora::available(),
4037
HostType::Remote(r) => RemoteFedora::available(r),
4138
}
4239
}
4340

44-
fn try_new(host: &H) -> Box<Future<Item = Option<Fedora<H>>, Error = Error>> {
41+
fn try_new(host: &H) -> Box<Future<Item = Option<Fedora>, Error = Error>> {
4542
let host = host.clone();
4643
Box::new(Self::available(&host)
4744
.and_then(|available| {
4845
if available {
49-
future::ok(Some(Fedora { host }))
46+
future::ok(Some(Fedora))
5047
} else {
5148
future::ok(None)
5249
}
5350
}))
5451
}
5552
}
5653

57-
impl<H: Host + 'static> TelemetryProvider<H> for Fedora<H> {
58-
fn load(&mut self) -> Box<Future<Item = Telemetry, Error = Error>> {
59-
match self.host.get_type() {
60-
HostType::Local(l) => LocalFedora::load(l),
54+
impl<H: Host + 'static> TelemetryProvider<H> for Fedora {
55+
fn load(&self, host: &H) -> Box<Future<Item = Telemetry, Error = Error>> {
56+
match host.get_type() {
57+
HostType::Local(_) => LocalFedora::load(),
6158
HostType::Remote(r) => RemoteFedora::load(r),
6259
}
6360
}
6461
}
6562

6663
impl LocalFedora {
67-
fn available(_: &Local) -> Box<Future<Item = bool, Error = Error>> {
64+
fn available() -> Box<Future<Item = bool, Error = Error>> {
6865
Box::new(future::ok(cfg!(target_os="linux") && linux::fingerprint_os() == Some(LinuxFlavour::Fedora)))
6966
}
7067

71-
fn load(_: &Local) -> Box<Future<Item = Telemetry, Error = Error>> {
68+
fn load() -> Box<Future<Item = Telemetry, Error = Error>> {
7269
Box::new(future::lazy(|| match do_load() {
7370
Ok(t) => future::ok(t),
7471
Err(e) => future::err(e),
@@ -98,10 +95,10 @@ impl RemoteFedora {
9895
}
9996

10097
impl Executable for FedoraRunnable {
101-
fn exec(self, host: &Local) -> Box<Future<Item = Box<Serialize>, Error = Error>> {
98+
fn exec(self, _: &Local) -> Box<Future<Item = Box<Serialize>, Error = Error>> {
10299
match self {
103-
FedoraRunnable::Available => Box::new(LocalFedora::available(host).map(|b| Box::new(b) as Box<Serialize>)),
104-
FedoraRunnable::Load => Box::new(LocalFedora::load(host).map(|t| {
100+
FedoraRunnable::Available => Box::new(LocalFedora::available().map(|b| Box::new(b) as Box<Serialize>)),
101+
FedoraRunnable::Load => Box::new(LocalFedora::load().map(|t| {
105102
let t: serializable::Telemetry = t.into();
106103
Box::new(t) as Box<Serialize>
107104
}))

0 commit comments

Comments
 (0)