Skip to content

Commit f219c8c

Browse files
committed
More accurate check for installed package
1 parent e8f2bfc commit f219c8c

File tree

6 files changed

+50
-13
lines changed

6 files changed

+50
-13
lines changed

src/error.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#[cfg(feature = "remote-run")]
1010
use czmq;
11+
use regex;
1112
use rustc_serialize::json;
1213
use std::{convert, error, fmt, io, num, str, string};
1314
#[cfg(feature = "remote-run")]
@@ -18,6 +19,9 @@ pub enum Error {
1819
/// An error string returned from the host's Intecture Agent
1920
Agent(String),
2021
#[cfg(feature = "remote-run")]
22+
/// An error string returned from the host's Intecture Auth
23+
Auth(String),
24+
#[cfg(feature = "remote-run")]
2125
/// CZMQ error
2226
Czmq(czmq::Error),
2327
/// JSON decoder error
@@ -39,6 +43,8 @@ pub enum Error {
3943
ParseFloat(num::ParseFloatError),
4044
/// Cast str as int
4145
ParseInt(num::ParseIntError),
46+
/// Regex error
47+
Regex(regex::Error),
4248
/// Cast str
4349
StrFromUtf8(str::Utf8Error),
4450
/// Cast String
@@ -53,6 +59,8 @@ impl fmt::Display for Error {
5359
match *self {
5460
Error::Agent(ref e) => write!(f, "Agent error: {}", e),
5561
#[cfg(feature = "remote-run")]
62+
Error::Auth(ref e) => write!(f, "Auth error: {}", e),
63+
#[cfg(feature = "remote-run")]
5664
Error::Czmq(ref e) => write!(f, "CZMQ error: {}", e),
5765
Error::JsonDecoder(ref e) => write!(f, "JSON decoder error: {}", e),
5866
#[cfg(feature = "remote-run")]
@@ -65,6 +73,7 @@ impl fmt::Display for Error {
6573
Error::Io(ref e) => write!(f, "IO error: {}", e),
6674
Error::ParseFloat(ref e) => write!(f, "Parse error: {}", e),
6775
Error::ParseInt(ref e) => write!(f, "Parse error: {}", e),
76+
Error::Regex(ref e) => write!(f, "Regex error: {}", e),
6877
Error::StrFromUtf8(ref e) => write!(f, "Convert from UTF8 slice to str error: {}", e),
6978
Error::StringFromUtf8(ref e) => write!(f, "Convert from UTF8 slice to String error: {}", e),
7079
#[cfg(feature = "remote-run")]
@@ -78,6 +87,8 @@ impl error::Error for Error {
7887
match *self {
7988
Error::Agent(ref e) => e,
8089
#[cfg(feature = "remote-run")]
90+
Error::Auth(ref e) => e,
91+
#[cfg(feature = "remote-run")]
8192
Error::Czmq(ref e) => e.description(),
8293
Error::JsonDecoder(ref e) => e.description(),
8394
#[cfg(feature = "remote-run")]
@@ -90,6 +101,7 @@ impl error::Error for Error {
90101
Error::Io(ref e) => e.description(),
91102
Error::ParseFloat(ref e) => e.description(),
92103
Error::ParseInt(ref e) => e.description(),
104+
Error::Regex(ref e) => e.description(),
93105
Error::StrFromUtf8(ref e) => e.description(),
94106
Error::StringFromUtf8(ref e) => e.description(),
95107
#[cfg(feature = "remote-run")]
@@ -124,6 +136,12 @@ impl convert::From<io::Error> for Error {
124136
}
125137
}
126138

139+
impl convert::From<regex::Error> for Error {
140+
fn from(err: regex::Error) -> Error {
141+
Error::Regex(err)
142+
}
143+
}
144+
127145
impl convert::From<str::Utf8Error> for Error {
128146
fn from(err: str::Utf8Error) -> Error {
129147
Error::StrFromUtf8(err)

src/host/remote.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl Host {
8888
let pk = try!(reply.popstr().unwrap().or(Err(Error::HostResponse)));
8989
Ok(ZCert::from_txt(&pk, "0000000000000000000000000000000000000000"))
9090
},
91-
"Err" => Err(Error::Agent(try!(reply.popstr().unwrap().or(Err(Error::HostResponse))))),
91+
"Err" => Err(Error::Auth(try!(reply.popstr().unwrap().or(Err(Error::HostResponse))))),
9292
_ => unreachable!(),
9393
}
9494
}

src/package/providers/dnf.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
//! Dnf package provider
1010
11-
use {Command, CommandResult, Host};
12-
use Result;
11+
use {Command, CommandResult, Host, Telemetry};
12+
use {Error, Result};
13+
use regex::Regex;
1314
use super::*;
1415

1516
pub struct Dnf;
@@ -27,10 +28,16 @@ impl Provider for Dnf {
2728
}
2829

2930
fn is_installed(&self, host: &mut Host, name: &str) -> Result<bool> {
30-
let cmd = Command::new(&format!("dnf list installed | grep {}", name));
31+
let cmd = Command::new("dnf list installed");
3132
let result = try!(cmd.exec(host));
33+
if result.exit_code != 0 {
34+
return Err(Error::Agent(result.stderr));
35+
}
3236

33-
Ok(result.exit_code == 0)
37+
let telemetry = try!(Telemetry::init(host));
38+
39+
let re = try!(Regex::new(&format!("(?m)^{}\\.({}|noarch)\\s+", name, telemetry.os.arch)));
40+
Ok(re.is_match(&result.stdout))
3441
}
3542

3643
fn install(&self, host: &mut Host, name: &str) -> Result<CommandResult> {

src/package/providers/homebrew.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
//! Homebrew package provider
1010
1111
use {Command, CommandResult, Host};
12-
use Result;
12+
use {Error, Result};
13+
use regex::Regex;
1314
use super::*;
1415

1516
pub struct Homebrew;
@@ -27,10 +28,14 @@ impl Provider for Homebrew {
2728
}
2829

2930
fn is_installed(&self, host: &mut Host, name: &str) -> Result<bool> {
30-
let cmd = Command::new(&format!("brew list | grep {}", name));
31+
let cmd = Command::new("brew list");
3132
let result = try!(cmd.exec(host));
33+
if result.exit_code != 0 {
34+
return Err(Error::Agent(result.stderr));
35+
}
3236

33-
Ok(result.exit_code == 0)
37+
let re = try!(Regex::new(&format!("(?m)(^|\\s+){}\\s+", name)));
38+
Ok(re.is_match(&result.stdout))
3439
}
3540

3641
fn install(&self, host: &mut Host, name: &str) -> Result<CommandResult> {

src/package/providers/pkg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Provider for Pkg {
2727
}
2828

2929
fn is_installed(&self, host: &mut Host, name: &str) -> Result<bool> {
30-
let cmd = Command::new(&format!("pkg info | grep {}", name));
30+
let cmd = Command::new(&format!("pkg query \"%n\" {}", name));
3131
let result = try!(cmd.exec(host));
3232

3333
Ok(result.exit_code == 0)

src/package/providers/yum.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
//! Yum package provider
1010
11-
use {Command, CommandResult, Host};
12-
use Result;
11+
use {Command, CommandResult, Host, Telemetry};
12+
use {Error, Result};
13+
use regex::Regex;
1314
use super::*;
1415

1516
pub struct Yum;
@@ -27,10 +28,16 @@ impl Provider for Yum {
2728
}
2829

2930
fn is_installed(&self, host: &mut Host, name: &str) -> Result<bool> {
30-
let cmd = Command::new(&format!("yum list installed | grep {}", name));
31+
let cmd = Command::new("yum list installed");
3132
let result = try!(cmd.exec(host));
33+
if result.exit_code != 0 {
34+
return Err(Error::Agent(result.stderr));
35+
}
3236

33-
Ok(result.exit_code == 0)
37+
let telemetry = try!(Telemetry::init(host));
38+
39+
let re = try!(Regex::new(&format!("(?m)^{}\\.({}|noarch)\\s+", name, telemetry.os.arch)));
40+
Ok(re.is_match(&result.stdout))
3441
}
3542

3643
fn install(&self, host: &mut Host, name: &str) -> Result<CommandResult> {

0 commit comments

Comments
 (0)