-
Notifications
You must be signed in to change notification settings - Fork 3
fix: schedules, restore direct update semantics and strengthen parsin… #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,13 +71,26 @@ pub fn schedules_cmd() -> Command { | |
| ) | ||
| .subcommand( | ||
| Command::new("delete") | ||
| .allow_external_subcommands(true) | ||
| .arg( | ||
| Arg::new("schedule_id") | ||
| .required(true) | ||
| .value_parser(value_parser!(String)) | ||
| .help("The schedule ID to delete") | ||
| .action(clap::ArgAction::Set), | ||
| ) | ||
| .override_usage("tower schedules delete [OPTIONS] <SCHEDULE_ID>") | ||
| .after_help("Example: tower schedules delete 123") | ||
| .about("Delete a schedule"), | ||
| ) | ||
| .subcommand( | ||
| Command::new("update") | ||
| .arg( | ||
| Arg::new("schedule_id") | ||
| .required(true) | ||
| .value_parser(value_parser!(String)) | ||
| .help("The schedule ID to update") | ||
| .action(clap::ArgAction::Set), | ||
|
Comment on lines
+88
to
+92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually can be by We have a lot of confusion about parameters with our users. People get confused by the differences in how we accept name/ID parameters: and Something for us to address... |
||
| ) | ||
| .arg( | ||
| Arg::new("cron") | ||
| .short('c') | ||
|
|
@@ -93,7 +106,6 @@ pub fn schedules_cmd() -> Command { | |
| .help("Parameters (key=value) to pass to the app") | ||
| .action(clap::ArgAction::Append), | ||
| ) | ||
| .allow_external_subcommands(true) | ||
| .override_usage("tower schedules update [OPTIONS] <SCHEDULE_ID>") | ||
| .after_help("Example: tower schedules update 123 --cron \"*/15 * * * *\"") | ||
| .about("Update an existing schedule"), | ||
|
|
@@ -164,43 +176,31 @@ pub async fn do_create(config: Config, args: &ArgMatches) { | |
| } | ||
|
|
||
| pub async fn do_update(config: Config, args: &ArgMatches) { | ||
| let schedule_id = extract_schedule_id("update", args.subcommand()); | ||
| let schedule_id = args.get_one::<String>("schedule_id").unwrap(); | ||
| let cron = args.get_one::<String>("cron"); | ||
| let parameters = parse_parameters(args); | ||
|
|
||
| output::with_spinner( | ||
| "Updating schedule", | ||
| api::update_schedule(&config, &schedule_id, cron, parameters), | ||
| api::update_schedule(&config, schedule_id, cron, parameters), | ||
| ) | ||
| .await; | ||
|
|
||
| output::success(&format!("Schedule {} updated", schedule_id)); | ||
| } | ||
|
|
||
| pub async fn do_delete(config: Config, args: &ArgMatches) { | ||
| let schedule_id = extract_schedule_id("delete", args.subcommand()); | ||
| let schedule_id = args.get_one::<String>("schedule_id").unwrap(); | ||
|
|
||
| output::with_spinner( | ||
| "Deleting schedule", | ||
| api::delete_schedule(&config, &schedule_id), | ||
| api::delete_schedule(&config, schedule_id), | ||
| ) | ||
| .await; | ||
|
|
||
| output::success(&format!("Schedule {} deleted", schedule_id)); | ||
| } | ||
|
|
||
| fn extract_schedule_id(subcmd: &str, cmd: Option<(&str, &ArgMatches)>) -> String { | ||
| if let Some((id, _)) = cmd { | ||
| return id.to_string(); | ||
| } | ||
|
|
||
| let line = format!( | ||
| "Schedule ID is required. Example: tower schedules {} <schedule-id>", | ||
| subcmd | ||
| ); | ||
| output::die(&line); | ||
| } | ||
|
|
||
| /// Parses `--parameter` arguments into a HashMap of key-value pairs. | ||
| /// Handles format like "--parameter key=value" | ||
| fn parse_parameters(args: &ArgMatches) -> Option<HashMap<String, String>> { | ||
|
|
@@ -228,8 +228,177 @@ fn parse_parameters(args: &ArgMatches) -> Option<HashMap<String, String>> { | |
| } | ||
| } | ||
|
|
||
| Some(param_map) | ||
| if param_map.is_empty() { | ||
| None | ||
| } else { | ||
| Some(param_map) | ||
| } | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{parse_parameters, schedules_cmd}; | ||
|
|
||
| #[test] | ||
| fn update_accepts_positional_schedule_id_and_flags() { | ||
| let matches = schedules_cmd() | ||
| .try_get_matches_from([ | ||
| "schedules", | ||
| "update", | ||
| "sch_123", | ||
| "--cron", | ||
| "*/10 * * * *", | ||
| "--parameter", | ||
| "env=prod", | ||
| "-p", | ||
| "team=platform", | ||
| ]) | ||
| .expect("update args should parse"); | ||
|
|
||
| let ("update", update_args) = matches.subcommand().expect("expected update subcommand") | ||
| else { | ||
| panic!("expected update subcommand"); | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| update_args | ||
| .get_one::<String>("schedule_id") | ||
| .map(String::as_str), | ||
| Some("sch_123") | ||
| ); | ||
| assert_eq!( | ||
| update_args.get_one::<String>("cron").map(String::as_str), | ||
| Some("*/10 * * * *") | ||
| ); | ||
|
|
||
| let params: Vec<&str> = update_args | ||
| .get_many::<String>("parameters") | ||
| .expect("expected parameters") | ||
| .map(String::as_str) | ||
| .collect(); | ||
| assert_eq!(params, vec!["env=prod", "team=platform"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn update_accepts_equals_sign_flag_forms() { | ||
| let matches = schedules_cmd() | ||
| .try_get_matches_from([ | ||
| "schedules", | ||
| "update", | ||
| "sch_456", | ||
| "--cron=*/5 * * * *", | ||
| "--parameter=region=us-east-1", | ||
| ]) | ||
| .expect("equals-form args should parse"); | ||
|
|
||
| let ("update", update_args) = matches.subcommand().expect("expected update subcommand") | ||
| else { | ||
| panic!("expected update subcommand"); | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| update_args | ||
| .get_one::<String>("schedule_id") | ||
| .map(String::as_str), | ||
| Some("sch_456") | ||
| ); | ||
| assert_eq!( | ||
| update_args.get_one::<String>("cron").map(String::as_str), | ||
| Some("*/5 * * * *") | ||
| ); | ||
| assert_eq!( | ||
| update_args | ||
| .get_many::<String>("parameters") | ||
| .expect("expected parameter") | ||
| .next() | ||
| .map(String::as_str), | ||
| Some("region=us-east-1") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn update_requires_schedule_id() { | ||
| let result = | ||
| schedules_cmd().try_get_matches_from(["schedules", "update", "--cron", "*/15 * * * *"]); | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn delete_requires_schedule_id() { | ||
| let result = schedules_cmd().try_get_matches_from(["schedules", "delete"]); | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_parameters_valid_pairs() { | ||
| let matches = schedules_cmd() | ||
| .try_get_matches_from([ | ||
| "schedules", | ||
| "update", | ||
| "sch_789", | ||
| "--parameter", | ||
| "env=prod", | ||
| "-p", | ||
| "team=platform", | ||
| ]) | ||
| .expect("update args should parse"); | ||
|
|
||
| let ("update", update_args) = matches.subcommand().expect("expected update subcommand") | ||
| else { | ||
| panic!("expected update subcommand"); | ||
| }; | ||
|
|
||
| let params = parse_parameters(update_args).expect("expected parsed parameters"); | ||
| assert_eq!(params.get("env"), Some(&"prod".to_string())); | ||
| assert_eq!(params.get("team"), Some(&"platform".to_string())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_parameters_invalid_entries_return_none() { | ||
| let matches = schedules_cmd() | ||
| .try_get_matches_from([ | ||
| "schedules", | ||
| "update", | ||
| "sch_789", | ||
| "--parameter", | ||
| "invalid", | ||
| "-p", | ||
| "=missing-key", | ||
| ]) | ||
| .expect("update args should parse"); | ||
|
|
||
| let ("update", update_args) = matches.subcommand().expect("expected update subcommand") | ||
| else { | ||
| panic!("expected update subcommand"); | ||
| }; | ||
|
|
||
| assert_eq!(parse_parameters(update_args), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_parameters_mixed_valid_and_invalid_keeps_valid() { | ||
| let matches = schedules_cmd() | ||
| .try_get_matches_from([ | ||
| "schedules", | ||
| "update", | ||
| "sch_789", | ||
| "--parameter", | ||
| "env=prod", | ||
| "-p", | ||
| "invalid", | ||
| ]) | ||
| .expect("update args should parse"); | ||
|
|
||
| let ("update", update_args) = matches.subcommand().expect("expected update subcommand") | ||
| else { | ||
| panic!("expected update subcommand"); | ||
| }; | ||
|
|
||
| let params = parse_parameters(update_args).expect("expected parsed parameters"); | ||
| assert_eq!(params.get("env"), Some(&"prod".to_string())); | ||
| assert_eq!(params.len(), 1); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah this is a good reminder that we need to support deleting schedules by name 😄