-
Notifications
You must be signed in to change notification settings - Fork 52
feat: new batt schedule command for fully automatic calibration
#95
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
base: master
Are you sure you want to change the base?
Changes from all commits
3f10331
974db76
99afc3a
3cc4418
3b72d8f
8c06437
9b8d533
79a49dc
312abb9
c42ce2a
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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewScheduleCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "schedule [cron expression | disable | postpone <duration> | skip]", | ||
| Aliases: []string{"sch"}, | ||
| Short: "Manage automatic calibration schedule", | ||
| Long: `Examples: | ||
| batt schedule '0 10 * * 0' # Note: Cron expressions containing * must be quoted! | ||
|
Owner
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. Maybe add some examples on cron expressions? For example: batt schedule 'minute hour day month weekday'
Contributor
Author
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. ok |
||
| batt schedule disable | ||
| batt schedule postpone 90m | ||
| batt schedule skip`, | ||
| GroupID: gAdvanced, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if len(args) == 0 { | ||
| return cmd.Usage() | ||
| } | ||
| switch strings.ToLower(args[0]) { | ||
| case "disable": | ||
| return runScheduleDisable(cmd) | ||
| case "postpone": | ||
| return runSchedulePostpone(cmd, args[1:]) | ||
| case "skip": | ||
| return runScheduleSkip(cmd) | ||
| default: | ||
| cronExpr := strings.Join(args, " ") | ||
| return runScheduleSet(cmd, cronExpr) | ||
| } | ||
| }, | ||
| } | ||
| return cmd | ||
| } | ||
|
|
||
| func runScheduleSet(cmd *cobra.Command, cronExpr string) error { | ||
| if strings.TrimSpace(cronExpr) == "" { | ||
| return fmt.Errorf("cron expression cannot be empty") | ||
| } | ||
| nextRuns, err := apiClient.Schedule(cronExpr) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if len(nextRuns) == 0 { | ||
| cmd.Println("Calibration schedule disabled.") | ||
| return nil | ||
| } | ||
| cmd.Printf("Calibration scheduled. Next %d run(s):\n", len(nextRuns)) | ||
| for _, run := range nextRuns { | ||
| cmd.Printf(" - %s\n", run.Local().Format(time.DateTime)) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func runScheduleDisable(cmd *cobra.Command) error { | ||
| if _, err := apiClient.Schedule(""); err != nil { | ||
| return err | ||
| } | ||
| cmd.Println("Calibration schedule disabled.") | ||
| return nil | ||
| } | ||
|
|
||
| func runSchedulePostpone(cmd *cobra.Command, args []string) error { | ||
| duration := time.Hour | ||
| if len(args) > 0 { | ||
| parsed, err := time.ParseDuration(args[0]) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid duration %q: %w", args[0], err) | ||
| } | ||
| duration = parsed | ||
| } | ||
| if _, err := apiClient.PostponeSchedule(duration); err != nil { | ||
| return err | ||
| } | ||
| cmd.Printf("Next run postponed by %s.\n", duration) | ||
| return nil | ||
| } | ||
|
|
||
| func runScheduleSkip(cmd *cobra.Command) error { | ||
| if _, err := apiClient.SkipSchedule(); err != nil { | ||
| return err | ||
| } | ||
| cmd.Println("Next scheduled run skipped.") | ||
| return nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.