Skip to content

Commit d7084d5

Browse files
committed
Add a no-op mode
1 parent 2328a69 commit d7084d5

4 files changed

Lines changed: 76 additions & 33 deletions

File tree

src/csmrc.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,26 @@ use serde::Deserialize;
66
use std::default::Default;
77
use std::io::{Error, ErrorKind};
88

9-
#[derive(Debug, Default, Deserialize)]
9+
#[derive(Debug, Deserialize)]
1010
pub struct Config {
1111
/// Override the $MAMBA_ROOT_PREFIX when shelling out to micromamba.
1212
#[serde(default)]
13-
#[allow(dead_code)]
1413
pub mamba_root_prefix: Option<String>,
14+
15+
/// If true, don't make any changes or call any commands, just print what
16+
/// we *would* do normally.
17+
#[serde(default)]
18+
pub noop_mode: bool,
19+
}
20+
21+
#[allow(clippy::derivable_impls)]
22+
impl Default for Config {
23+
fn default() -> Self {
24+
Config {
25+
mamba_root_prefix: None,
26+
noop_mode: false,
27+
}
28+
}
1529
}
1630

1731
impl Config {

src/env.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::csmrc::Config;
22
use crate::util::micromamba;
33

4-
use log::{debug, error};
4+
use log::{debug, error, info};
55
use serde::Deserialize;
66
use std::io::{Error, ErrorKind};
77
use std::path::Component;
@@ -96,8 +96,8 @@ pub fn run(config: Config, subcommand: Subcommand) -> ExitCode {
9696
error!("No environment name could be determined. You can specify one with --name");
9797
return ExitCode::FAILURE;
9898
};
99-
let cmd = micromamba(
100-
config,
99+
let mut cmd = micromamba(
100+
&config,
101101
vec![
102102
"env",
103103
"create",
@@ -107,19 +107,22 @@ pub fn run(config: Config, subcommand: Subcommand) -> ExitCode {
107107
&env_name,
108108
"--yes",
109109
],
110-
)
111-
.spawn()
112-
.expect("failed to call micromamba")
113-
.wait();
114-
match cmd {
115-
// Exit with whatever code micromamba gives us
116-
Ok(exit_status) => exit_status
117-
.code()
118-
.map(|c| ExitCode::from(c as u8))
119-
.unwrap_or(ExitCode::FAILURE),
120-
Err(e) => {
121-
error!("Failed to spawn micromamba: {}", e);
122-
ExitCode::FAILURE
110+
);
111+
if config.noop_mode {
112+
info!("Would run: {:?}", cmd);
113+
ExitCode::SUCCESS
114+
} else {
115+
let cmd = cmd.spawn().expect("failed to call micromamba").wait();
116+
match cmd {
117+
// Exit with whatever code micromamba gives us
118+
Ok(exit_status) => exit_status
119+
.code()
120+
.map(|c| ExitCode::from(c as u8))
121+
.unwrap_or(ExitCode::FAILURE),
122+
Err(e) => {
123+
error!("Failed to spawn micromamba: {}", e);
124+
ExitCode::FAILURE
125+
}
123126
}
124127
}
125128
}

src/main.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ mod env;
33
mod robot;
44
mod util;
55

6+
use crate::csmrc::Config;
67
use clap::{Parser, Subcommand};
7-
use log::{LevelFilter, debug, error, warn};
8+
use log::{LevelFilter, debug, error, info, warn};
89
use std::fs::File;
910
use std::io::Write;
1011
use std::path::Path;
@@ -14,9 +15,14 @@ use std::process::ExitCode;
1415
#[command(version)]
1516
/// Checkmk synthetic monitoring command-line tool
1617
struct 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
}

src/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ pub fn homedir() -> Option<PathBuf> {
1919

2020
/// Return a [`Command`] ready to shell out to `micromamba` with the appropriate
2121
/// environment variables set based on configuration.
22-
pub fn micromamba(config: Config, args: Vec<&str>) -> Command {
22+
pub fn micromamba(config: &Config, args: Vec<&str>) -> Command {
2323
let mut env_vars: HashMap<&str, String> = HashMap::new();
2424

25-
if let Some(mamba_root_prefix) = config.mamba_root_prefix {
26-
env_vars.insert("MAMBA_ROOT_PREFIX", mamba_root_prefix);
25+
if let Some(mamba_root_prefix) = &config.mamba_root_prefix {
26+
env_vars.insert("MAMBA_ROOT_PREFIX", mamba_root_prefix.to_string());
2727
}
2828

2929
let mut cmd = Command::new("micromamba");

0 commit comments

Comments
 (0)