| 1 | package App::Ordo::Command::Cal::Cron::Add; | ||
| 2 | use Moo; | ||
| 3 | use feature qw(say); | ||
| 4 | extends 'App::Ordo::Command::Base'; | ||
| 5 | use Term::ANSIColor qw(colored); | ||
| 6 | |||
| 7 | sub name { "cal cron add" } | ||
| 8 | sub summary { "Add a cron expression to a calendar" } | ||
| 9 | sub usage { "<calendar> \"<cron expression\" [--description \"text\"]" } | ||
| 10 | |||
| 11 | sub option_spec { | ||
| 12 | return { | ||
| 13 | 'description=s' => 'Description for this cron', | ||
| 14 | }; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub execute { | ||
| 18 | my ($self, $opt, $cal_name, $cron_expr) = @_; | ||
| 19 | |||
| 20 | unless ($cal_name && $cron_expr) { | ||
| 21 | say colored(["bold red"], "Usage: cal cron add <calendar> \"<cron>\" [--description \"...\"]"); | ||
| 22 | return; | ||
| 23 | } | ||
| 24 | |||
| 25 | my $res = $self->api->call('create_cron', { | ||
| 26 | cal => $cal_name, | ||
| 27 | name => $cron_expr, | ||
| 28 | description => $opt->{description}, | ||
| 29 | }); | ||
| 30 | |||
| 31 | if ($res->{success}) { | ||
| 32 | say colored(["bold green"], "Cron '$cron_expr' added to calendar '$cal_name'"); | ||
| 33 | } else { | ||
| 34 | say colored(["bold red"], "Failed: " . ($res->{message} || 'calendar not found')); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | 1; |