Skip to content

Commit 62abba8

Browse files
committed
Basic .csmrc parsing
We rely here on `serde_yaml_ng`, which is a fork of `serde_yaml` that seems at least somewhat actively maintained and used. The benefit here is that we get easy deserialization into Rust types (which we wouldn't get with something like `yaml_rust2` which just gives an AST). The disadvantage is that we're depending on a library that I wish had more community backing like the original `serde_yaml` did. The alternative here would probably be foregoing YAML entirely and using TOML instead, but it seems the rest of the Robotmk ecosystem uses YAML, so it is probably best to remain consistent here.
1 parent 5a67b8f commit 62abba8

7 files changed

Lines changed: 117 additions & 5 deletions

File tree

Cargo.lock

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ edition = "2024"
77
clap = { version = "4.5.50", features = ["derive", "wrap_help"] }
88
env_logger = "0.11.8"
99
log = "0.4.28"
10+
serde = { version = "1.0.228", features = ["derive"] }
11+
serde_yaml_ng = "0.10.0"

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# csm - Checkmk synthetic monitoring
2+
3+
(Under active development, not yet usable.)
4+
5+
## Configuration: `~/.csmrc`
6+
7+
You can optionally create a file, `~/.csmrc` (`%UserProfile%\.csmrc` on Windows)
8+
to override certain defaults. This is a YAML file with the following keys
9+
available:
10+
11+
* `mamba_root_prefix` - A string which sets where the Mamba environment(s) will
12+
be created on disk. By default, this is left up to `micromamba` and its
13+
default root prefix is used.

src/csmrc.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// Module for reading a user's ~/.csmrc, if it exists.
2+
use serde::Deserialize;
3+
use std::default::Default;
4+
use std::io::{Error, ErrorKind};
5+
use std::path::PathBuf;
6+
7+
#[derive(Debug, Default, Deserialize)]
8+
pub struct Config {
9+
/// Override the $MAMBA_ROOT_PREFIX when cshelling out to micromamba.
10+
#[serde(default)]
11+
#[allow(dead_code)]
12+
pub mamba_root_prefix: Option<String>,
13+
}
14+
15+
impl Config {
16+
/// Read the user's ~/.csmrc if it exists, merging with the Default instance for
17+
/// Config.
18+
pub fn from_csmrc() -> Result<Self, std::io::Error> {
19+
let home = match std::env::var("HOME") {
20+
Ok(home) => home,
21+
Err(_) => match std::env::var("USERPROFILE") {
22+
Ok(home) => home,
23+
Err(_) => return Ok(Config::default()),
24+
},
25+
};
26+
27+
let csmrc_path = PathBuf::from(home).join(".csmrc");
28+
let Ok(csmrc_data) = std::fs::read_to_string(csmrc_path) else {
29+
return Ok(Config::default());
30+
};
31+
serde_yaml_ng::from_str(&csmrc_data).map_err(|e| Error::new(ErrorKind::InvalidData, e))
32+
}
33+
}

src/env.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::csmrc::Config;
2+
13
#[derive(Debug, clap::Subcommand)]
24
pub enum Subcommand {
35
/// Create an environment
@@ -27,6 +29,7 @@ pub struct CreateArgs {
2729
name: Option<String>,
2830
}
2931

30-
pub fn run(subcommand: Subcommand) {
32+
pub fn run(config: Config, subcommand: Subcommand) {
33+
println!("{:?}", config);
3134
println!("{:?}", subcommand);
3235
}

src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod csmrc;
12
mod env;
23
mod robot;
34

@@ -29,7 +30,7 @@ enum Command {
2930
Robot(robot::Subcommand),
3031
}
3132

32-
fn main() {
33+
fn main() -> Result<(), std::io::Error> {
3334
let cli = Cli::parse();
3435

3536
// Set up logging
@@ -44,9 +45,10 @@ fn main() {
4445
env_logger_builder.init();
4546

4647
let _ = create_mambarc();
48+
let config = csmrc::Config::from_csmrc()?;
4749
match cli.command {
48-
Command::Env(sub) => env::run(sub),
49-
Command::Robot(sub) => robot::run(sub),
50+
Command::Env(sub) => Ok(env::run(config, sub)),
51+
Command::Robot(sub) => Ok(robot::run(config, sub)),
5052
}
5153
}
5254

src/robot.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::csmrc::Config;
2+
13
#[derive(Debug, clap::Subcommand)]
24
pub enum Subcommand {
35
/// Create a Robotmk robot
@@ -13,6 +15,7 @@ pub struct CreateArgs {
1315
path: String,
1416
}
1517

16-
pub fn run(subcommand: Subcommand) {
18+
pub fn run(config: Config, subcommand: Subcommand) {
19+
println!("{:?}", config);
1720
println!("{:?}", subcommand);
1821
}

0 commit comments

Comments
 (0)