Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ edition = "2024"
clap = { version = "4.5.50", features = ["derive", "wrap_help"] }
env_logger = "0.11.8"
log = "0.4.28"
serde = { version = "1.0.228", features = ["derive"] }
serde_yaml_ng = "0.10.0"
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# csm - Checkmk synthetic monitoring

(Under active development, not yet usable.)

## Configuration: `~/.csmrc`

You can optionally create a file, `~/.csmrc` (`%UserProfile%\.csmrc` on Windows)
to override certain defaults. This is a YAML file with the following keys
available:

* `mamba_root_prefix` - A string which sets where the Mamba environment(s) will
be created on disk. By default, this is left up to `micromamba` and its
default root prefix is used.
37 changes: 37 additions & 0 deletions src/csmrc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// Module for reading a user's ~/.csmrc, if it exists.
use crate::util;

use log::debug;
use serde::Deserialize;
use std::default::Default;
use std::io::{Error, ErrorKind};
use std::path::PathBuf;

#[derive(Debug, Default, Deserialize)]
pub struct Config {
/// Override the $MAMBA_ROOT_PREFIX when shelling out to micromamba.
#[serde(default)]
#[allow(dead_code)]
pub mamba_root_prefix: Option<String>,
}

impl Config {
/// Read the user's ~/.csmrc if it exists, merging with the Default instance for
/// Config. Return Err if a config file was found but failed to parse, otherwise
/// Ok with the result of merging the config file values with the Default (and
/// simply the Default if no config file exists).
pub fn from_csmrc() -> Result<Self, std::io::Error> {
let Ok(home) = util::homedir() else {
return Ok(Self::default());
};
let csmrc_path = PathBuf::from(home).join(".csmrc");
let Ok(csmrc_data) = std::fs::read_to_string(csmrc_path) else {
debug!("No .csmrc found, using defaults");
return Ok(Config::default());
};
let config =
serde_yaml_ng::from_str(&csmrc_data).map_err(|e| Error::new(ErrorKind::InvalidData, e));
debug!("config: {:?}", config);
config
}
}
5 changes: 4 additions & 1 deletion src/env.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::csmrc::Config;

#[derive(Debug, clap::Subcommand)]
pub enum Subcommand {
/// Create an environment
Expand Down Expand Up @@ -27,6 +29,7 @@ pub struct CreateArgs {
name: Option<String>,
}

pub fn run(subcommand: Subcommand) {
pub fn run(config: Config, subcommand: Subcommand) {
println!("{:?}", config);
println!("{:?}", subcommand);
}
25 changes: 18 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
mod csmrc;
mod env;
mod robot;
mod util;

use clap::{Parser, Subcommand};
use log::{LevelFilter, debug};
use log::{LevelFilter, debug, error};
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
Expand All @@ -29,7 +31,7 @@ enum Command {
Robot(robot::Subcommand),
}

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

// Set up logging
Expand All @@ -41,15 +43,26 @@ fn main() {
let mut env_logger_builder = env_logger::Builder::new();
env_logger_builder.filter_level(default_verbosity);
env_logger_builder.parse_default_env();
env_logger_builder.format_timestamp(None);
env_logger_builder.init();

let _ = create_mambarc();
let config = match csmrc::Config::from_csmrc() {
Ok(config) => config,
Err(err) => {
error!("Failed to parse .csmrc: {}", err);
panic!("Failed to parse .csmrc as valid YAML");
}
};
match cli.command {
Command::Env(sub) => env::run(sub),
Command::Robot(sub) => robot::run(sub),
Command::Env(sub) => env::run(config, sub),
Command::Robot(sub) => robot::run(config, sub),
}
Ok(())
}

/// Create a ~/.mambarc (%UserProfile%\.mambarc on Windows) if it does not
/// exist.
fn create_mambarc() -> std::io::Result<()> {
let mambarc = r#"
# Show the active environment in the shell prompt
Expand All @@ -64,9 +77,7 @@ changeps1: True
# ssl_verify: mycorpcert.crt
# ssl_no_revoke: true
"#;
let home = std::env::var("HOME")
.or_else(|_| std::env::var("USERPROFILE"))
.expect("Cannot determine home directory");
let home = util::homedir().expect("Cannot determine home directory");
let mambarc_path = PathBuf::from(home).join(".mambarc");
match File::create_new(&mambarc_path) {
Ok(mut file) => file.write_all(mambarc.trim_start().as_bytes())?,
Expand Down
5 changes: 4 additions & 1 deletion src/robot.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::csmrc::Config;

#[derive(Debug, clap::Subcommand)]
pub enum Subcommand {
/// Create a Robotmk robot
Expand All @@ -13,6 +15,7 @@ pub struct CreateArgs {
path: String,
}

pub fn run(subcommand: Subcommand) {
pub fn run(config: Config, subcommand: Subcommand) {
println!("{:?}", config);
println!("{:?}", subcommand);
}
5 changes: 5 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use std::env;

pub fn homedir() -> Result<String, env::VarError> {
env::var("HOME").or_else(|_| env::var("USERPROFILE"))
}