Skip to content

Commit 6192350

Browse files
committed
chore: add some docs
Signed-off-by: James Petersen <jpetersenames@gmail.com>
1 parent a45293f commit 6192350

4 files changed

Lines changed: 58 additions & 15 deletions

File tree

src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
use log::{error, info, warn};
22
use std::fmt;
33

4+
/// CheckResultValue is the final value for the result of an individual check.
5+
///
6+
/// Failed means a check ran successfully but did not pass. Errored means a check hit an
7+
/// error while executing.
8+
///
9+
/// Failed and Errored should contain a descriptive string explaining the result.
410
pub enum CheckResultValue {
511
Passed,
612
Failed(String),
@@ -19,8 +25,13 @@ impl fmt::Display for CheckResultValue {
1925
}
2026
}
2127

28+
/// CheckResult is the end result of an individual check. It carries the name of the individual
29+
/// check as well as the end result.
2230
pub struct CheckResult {
31+
/// name is the name of the individual check
2332
pub name: String,
33+
34+
/// result is the final result of an individual check
2435
pub result: CheckResultValue,
2536
}
2637

@@ -33,9 +44,17 @@ impl CheckResult {
3344
}
3445
}
3546

47+
/// CheckGroupResult is the result for a top-level group of checks. The result field is calculated
48+
/// from the set of individual checks within that group.
3649
pub struct CheckGroupResult {
50+
/// name is the name of the group of checks
3751
pub name: String,
52+
53+
/// result is the top-level result of the group of checks. It is calculated from the results of
54+
/// each individual check.
3855
pub result: CheckResultValue,
56+
57+
/// results is the list of results from each individual check within this group.
3958
pub results: Vec<CheckResult>,
4059
}
4160

@@ -47,6 +66,9 @@ impl CheckGroupResult {
4766
results: Vec::new(),
4867
}
4968
}
69+
70+
/// log_group is a pretty-print helper to log the result of the group based on what the result
71+
/// value is.
5072
pub fn log_group(&self) {
5173
let name = &self.name;
5274
let result = &self.result;
@@ -59,6 +81,8 @@ impl CheckGroupResult {
5981
}
6082
}
6183

84+
/// log_individual_checks is a pretty-print helper to log the results of each individual check
85+
/// within a group.
6286
pub fn log_individual_checks(&self) {
6387
let group_name = &self.name;
6488
for check_result in self.results.iter() {
@@ -75,12 +99,22 @@ impl CheckGroupResult {
7599
}
76100
}
77101

102+
/// CheckGroup is a trait representing a group of checks.
78103
pub trait CheckGroup {
104+
/// name is the name of the check group
79105
fn name(&self) -> &str;
106+
107+
/// id is the identifier for the check group. This field is used when skipping or selecting
108+
/// certain check groups to run so it should be env/cli friendly.
80109
fn id(&self) -> &str;
110+
111+
/// description is a longer form text field explaining what this check group is intended for.
81112
fn description(&self) -> &str;
113+
114+
/// run is the main entry point that runs the checks within the check group.
82115
fn run(&self) -> CheckGroupResult;
83116
}
84117

118+
// modules
85119
pub mod script;
86120
pub mod system;

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ fn main() -> Result<()> {
3131
}
3232

3333
info!("Running Group [{}] - {}", group.name(), group.description());
34+
3435
let check_group_result = group.run();
36+
3537
check_group_result.log_group();
38+
3639
if env::var("EDERA_PREFLIGHT_VERBOSE").unwrap_or_default() == "true" {
3740
check_group_result.log_individual_checks();
3841
}
@@ -48,7 +51,7 @@ fn main() -> Result<()> {
4851
}
4952

5053
match final_result {
51-
Errored(_) | Failed(_) => bail!("checks failed"),
54+
Errored(_) | Failed(_) => bail!("Preflight checks did not pass"),
5255
_ => Ok(()),
5356
}
5457
}

src/script/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ use std::process::Command;
1313
const GROUP_IDENTIFIER: &str = "ScriptedChecks";
1414
const NAME: &str = "Scripted Checks";
1515

16+
/// ScriptChecks is a special type of check that is intended to run a series of
17+
/// small shell scripts. The intent here is to make a pluggable interface for to
18+
/// quickly implement checks. Ideally all checks end up in their own CheckGroup.
1619
pub struct ScriptChecks;
1720

1821
impl ScriptChecks {
22+
/// run_all runs all shell scripts within the directory $EDERA_PREFLIGHT_SCRIPTS_DIR
1923
pub fn run_all(&self) -> CheckGroupResult {
2024
let mut results = Vec::new();
2125

@@ -53,11 +57,16 @@ impl ScriptChecks {
5357
}
5458
}
5559

60+
/// scripts_dir sets the directory to search for scripts
5661
fn scripts_dir(&self) -> PathBuf {
5762
let sdir = env::var("EDERA_PREFLIGHT_SCRIPTS_DIR").unwrap_or("./scripts".to_string());
5863
PathBuf::from(sdir)
5964
}
6065

66+
/// script_list attempts to collect a list of shell scripts to execute. It
67+
/// will return an empty list if the path does not exist or is not a
68+
/// directory. If the path is a directory it will scrape all files (without
69+
/// recursing into subdirectories) and return them as a list.
6170
fn script_list(&self) -> Result<Vec<PathBuf>> {
6271
let scripts_dir = self.scripts_dir();
6372

@@ -77,7 +86,7 @@ impl ScriptChecks {
7786
let entry = entry?;
7887
if !entry.file_type()?.is_file() {
7988
warn!(
80-
"skipping load of {}",
89+
"skipping load of {}: not a file",
8190
entry.file_name().to_str().unwrap_or("unknown")
8291
);
8392
continue;
@@ -86,11 +95,18 @@ impl ScriptChecks {
8695
scripts.push(entry.path());
8796
}
8897

89-
scripts.push(PathBuf::from("/totally/fake/script"));
90-
9198
Ok(scripts)
9299
}
93100

101+
/// run_script will run an individual script and return the result. It will
102+
/// attempt to scrape some details from the script output (like a name for
103+
/// the check).
104+
///
105+
/// If there is an error running the script (eg: script is not executable) then
106+
/// the check will return an error result.
107+
///
108+
/// If the script runs successfully but exits with a non-zero exit code, then
109+
/// the check will reutrn a fail result.
94110
fn run_script(&self, path: &PathBuf) -> CheckResult {
95111
let output = Command::new(path).output();
96112

src/system/mod.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct SystemChecks;
1414

1515
impl SystemChecks {
1616
pub fn run_all(&self) -> CheckGroupResult {
17-
let results = vec![self.enough_memory(), self.erroring(), self.failing()];
17+
let results = vec![self.enough_memory()];
1818

1919
let mut group_result = Passed;
2020
for res in results.iter() {
@@ -50,16 +50,6 @@ impl SystemChecks {
5050
}
5151
CheckResult::new(&name, result)
5252
}
53-
54-
fn failing(&self) -> CheckResult {
55-
let name = String::from("Should Fail");
56-
CheckResult::new(&name, Failed(String::from("Pretending to fail")))
57-
}
58-
59-
fn erroring(&self) -> CheckResult {
60-
let name = String::from("Should Error");
61-
CheckResult::new(&name, Errored(String::from("Pretending to error")))
62-
}
6353
}
6454

6555
impl CheckGroup for SystemChecks {

0 commit comments

Comments
 (0)