-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
545d7b8
commit db995b3
Showing
6 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
[defaults] | ||
timeout = 1 | ||
|
||
[[command]] | ||
name = "hostname" | ||
title = "Hostname" | ||
command = "/bin/hostname -" | ||
timeout = 1 | ||
default_run = true | ||
|
||
[[command]] | ||
name = "date" | ||
title = "Now" | ||
command = '/bin/date -u +"%Y-%m-%dT%H:%M:%SZ"' | ||
timeout = 1 | ||
default_run = true | ||
|
||
[[command]] | ||
name = "uname" | ||
title = "Host OS" | ||
description = "Basic host OS information" | ||
command = "/usr/bin/uname -a" | ||
timeout = 1 | ||
default_run = true | ||
|
||
[[command]] | ||
name = "uptime" | ||
title = "Current Load" | ||
description = "Current load and uptime" | ||
command = "/usr/bin/uptime" | ||
timeout = 1 | ||
default_run = true | ||
|
||
[[command]] | ||
name = "vm_stat" | ||
title = "Virtual Memory statistics" | ||
description = "Current memory usage and statistics in pages" | ||
command = "vm_stat -c 5 1" | ||
timeout = 5 | ||
default_run = true | ||
|
||
[[command]] | ||
name = "iostat" | ||
title = "Kernel I/O statistics" | ||
description = "Current I/O statistics per dev in KB" | ||
command = "/usr/sbin/iostat -c 5 -K" | ||
timeout = 5 | ||
default_run = true | ||
|
||
# vim: set ft=toml: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use crate::command::Command; | ||
|
||
use serde::Deserialize; | ||
use snafu::{ResultExt, Snafu}; | ||
use std::io::Read; | ||
use std::path::{Path, PathBuf}; | ||
use std::fs::File; | ||
|
||
/// Error type | ||
#[derive(Debug, Snafu)] | ||
#[allow(missing_docs)] | ||
pub enum Error { | ||
/// Failed to parse Config | ||
#[snafu(display("Failed parse config: {}", source))] | ||
ParsingFailed { source: toml::de::Error}, | ||
/// Failed to read file | ||
#[snafu(display("Failed read file config '{:?}': {}", path, source))] | ||
ReadFileFailed { path: PathBuf, source: std::io::Error }, | ||
} | ||
|
||
/// Result type | ||
pub type Result<T, E = Error> = std::result::Result<T, E>; | ||
|
||
#[derive(Debug, Deserialize, PartialEq)] | ||
pub struct Config { | ||
pub defaults: Defaults, | ||
#[serde(rename = "command")] | ||
pub commands: Vec<Command>, | ||
} | ||
|
||
impl Config { | ||
pub fn from_str(toml: &str) -> Result<Config> { | ||
let config: Config = toml::from_str(toml).context(ParsingFailed {})?; | ||
Ok(config) | ||
} | ||
|
||
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config> { | ||
let mut file = File::open(path.as_ref()) | ||
.context(ReadFileFailed{ path: path.as_ref().to_path_buf() })?; | ||
let mut toml = String::new(); | ||
file.read_to_string(&mut toml) | ||
.context(ReadFileFailed{ path: path.as_ref().to_path_buf() })?; | ||
Config::from_str(&toml) | ||
} | ||
} | ||
|
||
#[derive(Debug, Deserialize, PartialEq)] | ||
pub struct Defaults { | ||
#[serde(default = "default_timeout")] | ||
pub timeout: u64, | ||
} | ||
|
||
fn default_timeout() -> u64 { | ||
5 | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use spectral::prelude::*; | ||
|
||
#[test] | ||
fn config_read_ok() { | ||
let config_txt = r#" | ||
[defaults] | ||
timeout = 5 | ||
[[command]] | ||
name = "uname" | ||
title = "Host OS" | ||
description = "Basic host OS information" | ||
command = "/usr/bin/uname -a" | ||
timeout = 1 | ||
default_run = true | ||
"#; | ||
let defaults = Defaults { | ||
timeout: 5 | ||
}; | ||
let mut commands = Vec::new(); | ||
commands.push( Command::new("uname", "/usr/bin/uname -a", 1) | ||
.title("Host OS") | ||
.description("Basic host OS information") | ||
.run_by_default(true)); | ||
let expected = Config { | ||
defaults, | ||
commands, | ||
}; | ||
|
||
let config = Config::from_str(config_txt); | ||
|
||
asserting("Reading config from toml").that(&config) | ||
.is_ok() | ||
.is_equal_to(&expected); | ||
} | ||
|
||
#[test] | ||
fn config_file_ok() { | ||
#[cfg(target_os = "macos")] | ||
let path = "contrib/osx.conf"; | ||
#[cfg(target_os = "linux")] | ||
let path = "contrib/linux.conf"; | ||
|
||
let config = Config::from_file(path); | ||
|
||
asserting("Reading config from file").that(&config) | ||
.is_ok() | ||
.map(|x| &x.commands) | ||
.has_length(6) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters