feat: cli: consolidate schedule-related commands (#2402)

* feat: cli: consolidate schedule-related commands

This commit makes the following changes:
- renames autostart -> schedule starat
- renames ttl -> schedule stop
- renames bump -> schedule override
- adds schedule show command
- moves some cli-related stuff to util.go
This commit is contained in:
Cian Johnston
2022-06-16 18:24:10 +01:00
committed by GitHub
parent c36b0d892b
commit c9691eafcb
17 changed files with 930 additions and 1091 deletions
+13
View File
@@ -139,6 +139,19 @@ func (s Schedule) Min() time.Duration {
return durMin
}
// Time returns a humanized form of the minute and hour fields.
func (s Schedule) Time() string {
minute := strings.Fields(s.cronStr)[0]
hour := strings.Fields(s.cronStr)[1]
maybeTime := fmt.Sprintf("%s:%s", hour, minute)
t, err := time.ParseInLocation("3:4", maybeTime, s.sched.Location)
if err != nil {
// return the original cronspec for minute and hour, who knows what's in there!
return fmt.Sprintf("cron(%s %s)", minute, hour)
}
return t.Format(time.Kitchen)
}
// DaysOfWeek returns a humanized form of the day-of-week field.
func (s Schedule) DaysOfWeek() string {
dow := strings.Fields(s.cronStr)[4]
@@ -22,6 +22,7 @@ func Test_Weekly(t *testing.T) {
expectedCron string
expectedLocation *time.Location
expectedString string
expectedTime string
}{
{
name: "with timezone",
@@ -34,6 +35,7 @@ func Test_Weekly(t *testing.T) {
expectedCron: "30 9 * * 1-5",
expectedLocation: mustLocation(t, "US/Central"),
expectedString: "CRON_TZ=US/Central 30 9 * * 1-5",
expectedTime: "9:30AM",
},
{
name: "without timezone",
@@ -46,6 +48,7 @@ func Test_Weekly(t *testing.T) {
expectedCron: "30 9 * * 1-5",
expectedLocation: time.UTC,
expectedString: "CRON_TZ=UTC 30 9 * * 1-5",
expectedTime: "9:30AM",
},
{
name: "convoluted with timezone",
@@ -58,6 +61,7 @@ func Test_Weekly(t *testing.T) {
expectedCron: "*/5 12-18 * * 1,3,6",
expectedLocation: mustLocation(t, "US/Central"),
expectedString: "CRON_TZ=US/Central */5 12-18 * * 1,3,6",
expectedTime: "cron(*/5 12-18)",
},
{
name: "another convoluted example",
@@ -70,6 +74,7 @@ func Test_Weekly(t *testing.T) {
expectedCron: "10,20,40-50 * * * *",
expectedLocation: mustLocation(t, "US/Central"),
expectedString: "CRON_TZ=US/Central 10,20,40-50 * * * *",
expectedTime: "cron(10,20,40-50 *)",
},
{
name: "time.Local will bite you",
+8
View File
@@ -17,6 +17,14 @@ func NilOrEmpty(s *string) bool {
return s == nil || *s == ""
}
// NilToEmpty coalesces a nil str to the empty string.
func NilToEmpty(s *string) string {
if s == nil {
return ""
}
return *s
}
// NilOrZero returns true if v is nil or 0.
func NilOrZero[T number](v *T) bool {
return v == nil || *v == 0