33mod robot;
44mod util;
55
6+ use crate :: csmrc:: Config ;
67use clap:: { Parser , Subcommand } ;
7- use log:: { LevelFilter , debug, error, warn} ;
8+ use log:: { LevelFilter , debug, error, info , warn} ;
89use std:: fs:: File ;
910use std:: io:: Write ;
1011use std:: path:: Path ;
@@ -14,9 +15,14 @@ use std::process::ExitCode;
1415#[ command( version) ]
1516/// Checkmk synthetic monitoring command-line tool
1617struct Cli {
18+ /// Enable verbose debugging output
1719 #[ arg( short, long) ]
1820 verbose : bool ,
1921
22+ /// Don't make any changes, only print what would happen
23+ #[ arg( short = 'n' , long = "noop" ) ]
24+ noop_mode : bool ,
25+
2026 #[ command( subcommand) ]
2127 command : Command ,
2228}
@@ -39,20 +45,38 @@ fn main() -> ExitCode {
3945 let default_verbosity = if cli. verbose {
4046 LevelFilter :: Debug
4147 } else {
42- LevelFilter :: Warn
48+ // We use info level for no-op mode messages.
49+ LevelFilter :: Info
4350 } ;
4451 let mut env_logger_builder = env_logger:: Builder :: new ( ) ;
4552 env_logger_builder. filter_level ( default_verbosity) ;
4653 env_logger_builder. parse_default_env ( ) ;
4754 env_logger_builder. format_timestamp ( None ) ;
4855 env_logger_builder. init ( ) ;
4956
57+ let config = match Config :: from_csmrc ( ) {
58+ Ok ( config) => {
59+ if cli. noop_mode {
60+ Config {
61+ noop_mode : true ,
62+ ..config
63+ }
64+ } else {
65+ config
66+ }
67+ }
68+ Err ( err) => {
69+ error ! ( "Failed to parse .csmrc: {}" , err) ;
70+ return ExitCode :: FAILURE ;
71+ }
72+ } ;
73+
5074 let Some ( home) = util:: homedir ( ) else {
5175 error ! ( "Failed to determine home directory" ) ;
5276 return ExitCode :: FAILURE ;
5377 } ;
5478
55- if let Err ( e) = create_mambarc ( & home) {
79+ if let Err ( e) = create_mambarc ( & config , & home) {
5680 let attempted_path = home. join ( ".mambarc" ) ;
5781 warn ! (
5882 "Could not create {}, but continuing: {}" ,
@@ -61,13 +85,6 @@ fn main() -> ExitCode {
6185 ) ;
6286 }
6387
64- let config = match csmrc:: Config :: from_csmrc ( ) {
65- Ok ( config) => config,
66- Err ( err) => {
67- error ! ( "Failed to parse .csmrc: {}" , err) ;
68- return ExitCode :: FAILURE ;
69- }
70- } ;
7188 match cli. command {
7289 Command :: Env ( sub) => env:: run ( config, sub) ,
7390 Command :: Robot ( sub) => robot:: run ( config, sub) ,
@@ -76,13 +93,22 @@ fn main() -> ExitCode {
7693
7794/// Create a ~/.mambarc (%UserProfile%\.mambarc on Windows) if it does not
7895/// exist.
79- fn create_mambarc ( home : & Path ) -> std:: io:: Result < ( ) > {
96+ fn create_mambarc ( config : & Config , home : & Path ) -> std:: io:: Result < ( ) > {
8097 let mambarc = include_str ! ( "../templates/mambarc" ) ;
8198 let mambarc_path = home. join ( ".mambarc" ) ;
99+
100+ if config. noop_mode && !mambarc_path. exists ( ) {
101+ info ! ( "Would create {}" , mambarc_path. display( ) ) ;
102+ return Ok ( ( ) ) ;
103+ }
104+
82105 match File :: create_new ( & mambarc_path) {
83106 Ok ( mut file) => file. write_all ( mambarc. trim_start ( ) . as_bytes ( ) ) ?,
84107 Err ( e) if e. kind ( ) == std:: io:: ErrorKind :: AlreadyExists => {
85- debug ! ( "File {mambarc_path:?} already exists, not creating" )
108+ debug ! (
109+ "File {} already exists, not creating" ,
110+ mambarc_path. display( )
111+ )
86112 }
87113 Err ( e) => return Err ( e) ,
88114 }
0 commit comments