Skip to content

Commit

Permalink
Make schedule required when other cron monitor args present
Browse files Browse the repository at this point in the history
  • Loading branch information
szokeasaurusrex committed Nov 8, 2023
1 parent 8d52559 commit 0c8636f
Showing 1 changed file with 13 additions and 47 deletions.
60 changes: 13 additions & 47 deletions src/commands/monitors/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn make_command(command: Command) -> Command {
Arg::new("checkin_margin")
.long("check-in-margin")
.value_parser(clap::value_parser!(u64))
.requires("schedule")
.help(
"The allowed margin of minutes after the expected check-in time that the \
monitor will not be considered missed for. This parameter is ignored \
Expand All @@ -65,17 +66,23 @@ pub fn make_command(command: Command) -> Command {
Arg::new("max_runtime")
.long("max-runtime")
.value_parser(clap::value_parser!(u64))
.requires("schedule")
.help(
"The allowed duration in minutes that the monitor may be in progress for \
before being considered failed due to timeout. This parameter is ignored \
unless a cron schedule is also provided.",
),
)
.arg(Arg::new("timezone").long("timezone").help(
"A tz database string (e.g. \"Europe/Vienna\") representing the monitor's \
.arg(
Arg::new("timezone")
.long("timezone")
.requires("schedule")
.help(
"A tz database string (e.g. \"Europe/Vienna\") representing the monitor's \
execution schedule's timezone. This parameter is ignored unless a cron \
schedule is also provided.",
))
),
)
}

fn run_program(args: Vec<&String>, monitor_slug: &str) -> (bool, Option<i32>, Duration) {
Expand Down Expand Up @@ -190,36 +197,6 @@ fn token_execute(
(success, code, None)
}

fn warn_ignored_arguments(matches: &ArgMatches, possibly_ignored: Vec<&str>, reason: &str) {
let mut ignored_arguments: Vec<_> = possibly_ignored
.into_iter()
.filter(|id| matches.contains_id(id))
.map(|id| format!("`{id}`"))
.collect();

if ignored_arguments.len() > 0 {
let (possible_s, possible_and) = if ignored_arguments.len() > 1 {
("s", "and ")
} else {
("", "")
};

let last = ignored_arguments.len() - 1;
ignored_arguments[last] = format!("{possible_and}{}", ignored_arguments[last]);

let joiner = if ignored_arguments.len() > 2 {
", "
} else {
" "
};

warn!(
"Ignoring {} argument{possible_s} because {reason}.",
ignored_arguments.join(joiner)
)
}
}

fn parse_monitor_config_args(matches: &ArgMatches) -> Option<MonitorConfig> {
match matches.get_one::<String>("schedule") {

Check failure on line 201 in src/commands/monitors/run.rs

View workflow job for this annotation

GitHub Actions / Lint

manual implementation of `Option::map`
Some(schedule) => Some(MonitorConfig {
Expand All @@ -230,15 +207,7 @@ fn parse_monitor_config_args(matches: &ArgMatches) -> Option<MonitorConfig> {
max_runtime: matches.get_one("max_runtime").copied(),
timezone: matches.get_one("timezone").cloned(),
}),
None => {
warn_ignored_arguments(
matches,
vec!["checkin_margin", "max_runtime", "timezone"],
"`schedule` argument is missing",
);

None
}
None => None,
}
}

Expand All @@ -265,11 +234,8 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
// Use legacy API when DSN is not provided
None => {
if monitor_config.is_some() {
warn_ignored_arguments(
matches,
vec!["schedule", "checkin_margin", "max_runtime", "timezone"],
"cron monitor upserts are only supported with DSN auth",
);
anyhow::bail!("Crons monitor upserts are only supported with DSN auth. Please try again with \
DSN auth or repeat the command without the `schedule` argument.");
}
let (success, code, err) = token_execute(args, monitor_slug, environment);
if let Some(e) = err {
Expand Down

0 comments on commit 0c8636f

Please sign in to comment.