File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,3 +7,5 @@ edition = "2024"
77clap = { version = " 4.5.50" , features = [" derive" , " wrap_help" ] }
88env_logger = " 0.11.8"
99log = " 0.4.28"
10+ serde = { version = " 1.0.228" , features = [" derive" ] }
11+ serde_yaml_ng = " 0.10.0"
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ use crate :: csmrc:: Config ;
2+
13#[ derive( Debug , clap:: Subcommand ) ]
24pub 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}
Original file line number Diff line number Diff line change 1+ mod csmrc;
12mod env;
23mod 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
Original file line number Diff line number Diff line change 1+ use crate :: csmrc:: Config ;
2+
13#[ derive( Debug , clap:: Subcommand ) ]
24pub 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}
You can’t perform that action at this time.
0 commit comments