mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
cli: streamline autostart ux (#2251)
This commit adds the following changes: - autostart enable|disable => autostart set|unset - autostart enable now accepts a more natual schedule format: <time> <days-of-week> <location> - autostart show now shows configured timezone - 🎉 automatic timezone detection across mac, windows, linux 🎉 Fixes #1647
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
package schedule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -74,7 +75,8 @@ func Weekly(raw string) (*Schedule, error) {
|
||||
}
|
||||
|
||||
// Schedule represents a cron schedule.
|
||||
// It's essentially a thin wrapper for robfig/cron/v3 that implements Stringer.
|
||||
// It's essentially a wrapper for robfig/cron/v3 that has additional
|
||||
// convenience methods.
|
||||
type Schedule struct {
|
||||
sched *cron.SpecSchedule
|
||||
// XXX: there isn't any nice way for robfig/cron to serialize
|
||||
@@ -92,9 +94,9 @@ func (s Schedule) String() string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// Timezone returns the timezone for the schedule.
|
||||
func (s Schedule) Timezone() string {
|
||||
return s.sched.Location.String()
|
||||
// Location returns the IANA location for the schedule.
|
||||
func (s Schedule) Location() *time.Location {
|
||||
return s.sched.Location
|
||||
}
|
||||
|
||||
// Cron returns the cron spec for the schedule with the leading CRON_TZ
|
||||
@@ -137,6 +139,26 @@ func (s Schedule) Min() time.Duration {
|
||||
return durMin
|
||||
}
|
||||
|
||||
// DaysOfWeek returns a humanized form of the day-of-week field.
|
||||
func (s Schedule) DaysOfWeek() string {
|
||||
dow := strings.Fields(s.cronStr)[4]
|
||||
if dow == "*" {
|
||||
return "daily"
|
||||
}
|
||||
for _, weekday := range []time.Weekday{
|
||||
time.Sunday,
|
||||
time.Monday,
|
||||
time.Tuesday,
|
||||
time.Wednesday,
|
||||
time.Thursday,
|
||||
time.Friday,
|
||||
time.Saturday,
|
||||
} {
|
||||
dow = strings.Replace(dow, fmt.Sprintf("%d", weekday), weekday.String()[:3], 1)
|
||||
}
|
||||
return dow
|
||||
}
|
||||
|
||||
// validateWeeklySpec ensures that the day-of-month and month options of
|
||||
// spec are both set to *
|
||||
func validateWeeklySpec(spec string) error {
|
||||
|
||||
@@ -12,59 +12,64 @@ import (
|
||||
func Test_Weekly(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := []struct {
|
||||
name string
|
||||
spec string
|
||||
at time.Time
|
||||
expectedNext time.Time
|
||||
expectedMin time.Duration
|
||||
expectedError string
|
||||
expectedCron string
|
||||
expectedTz string
|
||||
expectedString string
|
||||
name string
|
||||
spec string
|
||||
at time.Time
|
||||
expectedNext time.Time
|
||||
expectedMin time.Duration
|
||||
expectedDaysOfWeek string
|
||||
expectedError string
|
||||
expectedCron string
|
||||
expectedLocation *time.Location
|
||||
expectedString string
|
||||
}{
|
||||
{
|
||||
name: "with timezone",
|
||||
spec: "CRON_TZ=US/Central 30 9 * * 1-5",
|
||||
at: time.Date(2022, 4, 1, 14, 29, 0, 0, time.UTC),
|
||||
expectedNext: time.Date(2022, 4, 1, 14, 30, 0, 0, time.UTC),
|
||||
expectedMin: 24 * time.Hour,
|
||||
expectedError: "",
|
||||
expectedCron: "30 9 * * 1-5",
|
||||
expectedTz: "US/Central",
|
||||
expectedString: "CRON_TZ=US/Central 30 9 * * 1-5",
|
||||
name: "with timezone",
|
||||
spec: "CRON_TZ=US/Central 30 9 * * 1-5",
|
||||
at: time.Date(2022, 4, 1, 14, 29, 0, 0, time.UTC),
|
||||
expectedNext: time.Date(2022, 4, 1, 14, 30, 0, 0, time.UTC),
|
||||
expectedMin: 24 * time.Hour,
|
||||
expectedDaysOfWeek: "Mon-Fri",
|
||||
expectedError: "",
|
||||
expectedCron: "30 9 * * 1-5",
|
||||
expectedLocation: mustLocation(t, "US/Central"),
|
||||
expectedString: "CRON_TZ=US/Central 30 9 * * 1-5",
|
||||
},
|
||||
{
|
||||
name: "without timezone",
|
||||
spec: "30 9 * * 1-5",
|
||||
at: time.Date(2022, 4, 1, 9, 29, 0, 0, time.UTC),
|
||||
expectedNext: time.Date(2022, 4, 1, 9, 30, 0, 0, time.UTC),
|
||||
expectedMin: 24 * time.Hour,
|
||||
expectedError: "",
|
||||
expectedCron: "30 9 * * 1-5",
|
||||
expectedTz: "UTC",
|
||||
expectedString: "CRON_TZ=UTC 30 9 * * 1-5",
|
||||
name: "without timezone",
|
||||
spec: "30 9 * * 1-5",
|
||||
at: time.Date(2022, 4, 1, 9, 29, 0, 0, time.UTC),
|
||||
expectedNext: time.Date(2022, 4, 1, 9, 30, 0, 0, time.UTC),
|
||||
expectedMin: 24 * time.Hour,
|
||||
expectedDaysOfWeek: "Mon-Fri",
|
||||
expectedError: "",
|
||||
expectedCron: "30 9 * * 1-5",
|
||||
expectedLocation: time.UTC,
|
||||
expectedString: "CRON_TZ=UTC 30 9 * * 1-5",
|
||||
},
|
||||
{
|
||||
name: "convoluted with timezone",
|
||||
spec: "CRON_TZ=US/Central */5 12-18 * * 1,3,6",
|
||||
at: time.Date(2022, 4, 1, 14, 29, 0, 0, time.UTC),
|
||||
expectedNext: time.Date(2022, 4, 2, 17, 0, 0, 0, time.UTC), // Apr 1 was a Friday in 2022
|
||||
expectedMin: 5 * time.Minute,
|
||||
expectedError: "",
|
||||
expectedCron: "*/5 12-18 * * 1,3,6",
|
||||
expectedTz: "US/Central",
|
||||
expectedString: "CRON_TZ=US/Central */5 12-18 * * 1,3,6",
|
||||
name: "convoluted with timezone",
|
||||
spec: "CRON_TZ=US/Central */5 12-18 * * 1,3,6",
|
||||
at: time.Date(2022, 4, 1, 14, 29, 0, 0, time.UTC),
|
||||
expectedNext: time.Date(2022, 4, 2, 17, 0, 0, 0, time.UTC), // Apr 1 was a Friday in 2022
|
||||
expectedMin: 5 * time.Minute,
|
||||
expectedDaysOfWeek: "Mon,Wed,Sat",
|
||||
expectedError: "",
|
||||
expectedCron: "*/5 12-18 * * 1,3,6",
|
||||
expectedLocation: mustLocation(t, "US/Central"),
|
||||
expectedString: "CRON_TZ=US/Central */5 12-18 * * 1,3,6",
|
||||
},
|
||||
{
|
||||
name: "another convoluted example",
|
||||
spec: "CRON_TZ=US/Central 10,20,40-50 * * * *",
|
||||
at: time.Date(2022, 4, 1, 14, 29, 0, 0, time.UTC),
|
||||
expectedNext: time.Date(2022, 4, 1, 14, 40, 0, 0, time.UTC),
|
||||
expectedMin: time.Minute,
|
||||
expectedError: "",
|
||||
expectedCron: "10,20,40-50 * * * *",
|
||||
expectedTz: "US/Central",
|
||||
expectedString: "CRON_TZ=US/Central 10,20,40-50 * * * *",
|
||||
name: "another convoluted example",
|
||||
spec: "CRON_TZ=US/Central 10,20,40-50 * * * *",
|
||||
at: time.Date(2022, 4, 1, 14, 29, 0, 0, time.UTC),
|
||||
expectedNext: time.Date(2022, 4, 1, 14, 40, 0, 0, time.UTC),
|
||||
expectedMin: time.Minute,
|
||||
expectedDaysOfWeek: "daily",
|
||||
expectedError: "",
|
||||
expectedCron: "10,20,40-50 * * * *",
|
||||
expectedLocation: mustLocation(t, "US/Central"),
|
||||
expectedString: "CRON_TZ=US/Central 10,20,40-50 * * * *",
|
||||
},
|
||||
{
|
||||
name: "time.Local will bite you",
|
||||
@@ -127,9 +132,10 @@ func Test_Weekly(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, testCase.expectedNext, nextTime)
|
||||
require.Equal(t, testCase.expectedCron, actual.Cron())
|
||||
require.Equal(t, testCase.expectedTz, actual.Timezone())
|
||||
require.Equal(t, testCase.expectedLocation, actual.Location())
|
||||
require.Equal(t, testCase.expectedString, actual.String())
|
||||
require.Equal(t, testCase.expectedMin, actual.Min())
|
||||
require.Equal(t, testCase.expectedDaysOfWeek, actual.DaysOfWeek())
|
||||
} else {
|
||||
require.EqualError(t, err, testCase.expectedError)
|
||||
require.Nil(t, actual)
|
||||
@@ -137,3 +143,10 @@ func Test_Weekly(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func mustLocation(t *testing.T, s string) *time.Location {
|
||||
t.Helper()
|
||||
loc, err := time.LoadLocation(s)
|
||||
require.NoError(t, err)
|
||||
return loc
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user