Skip to content

Commit

Permalink
merge ProfileName and Profiles into one Profiles field
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Feb 7, 2024
1 parent ec09efd commit bffa06d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func showProfile(output io.Writer, ctx commandContext) error {

func showSchedules(output io.Writer, schedules []*config.Schedule) {
for _, schedule := range schedules {
err := config.ShowStruct(output, schedule, "schedule "+schedule.CommandName+"@"+schedule.ProfileName)
err := config.ShowStruct(output, schedule, "schedule "+schedule.CommandName+"@"+schedule.Profiles[0])
if err != nil {
fmt.Fprintln(output, err)
}
Expand Down
10 changes: 5 additions & 5 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ schedule = "daily"
declaredCount := 0

for _, jobConfig := range schedules {
scheduler := schedule.NewScheduler(schedule.NewHandler(schedule.SchedulerDefaultOS{}), jobConfig.ProfileName)
scheduler := schedule.NewScheduler(schedule.NewHandler(schedule.SchedulerDefaultOS{}), jobConfig.Profiles[0])
defer func(s *schedule.Scheduler) { s.Close() }(scheduler) // Capture current ref to scheduler to be able to close it when function returns.

if jobConfig.CommandName == "check" {
Expand Down Expand Up @@ -276,25 +276,25 @@ func TestShowSchedules(t *testing.T) {
buffer := &bytes.Buffer{}
schedules := []*config.Schedule{
{
ProfileName: "default",
Profiles: []string{"default"},
CommandName: "check",
Schedules: []string{"weekly"},
},
{
ProfileName: "default",
Profiles: []string{"default"},
CommandName: "backup",
Schedules: []string{"daily"},
},
}
expected := strings.TrimSpace(`
schedule check@default:
profile: default
run: check
profiles: default
schedule: weekly
schedule backup@default:
profile: default
run: backup
profiles: default
schedule: daily
`)
Expand Down
3 changes: 1 addition & 2 deletions config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,9 @@ func (p *Profile) Schedules() []*Schedule {
}

config := &Schedule{
ProfileName: p.Name,
CommandName: name,
Group: "",
Profiles: []string{},
Profiles: []string{p.Name},
Schedules: s.Schedule,
Permission: s.SchedulePermission,
Log: s.ScheduleLog,
Expand Down
7 changes: 3 additions & 4 deletions config/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ const (
// Schedule is an intermediary object between the configuration (v1, v2+) and the ScheduleConfig object used by the scheduler.
// The object is also used to display the scheduling configuration
type Schedule struct {
ProfileName string `mapstructure:"profile"` // v1 only
CommandName string `mapstructure:"run"` // v1 only
CommandName string `mapstructure:"run"`
Group string `mapstructure:"group"` // v2+ only
Profiles []string `mapstructure:"profiles"` // v2+ only
Profiles []string `mapstructure:"profiles"` // multiple profiles in v2+ only
Schedules []string `mapstructure:"schedule"`
Permission string `mapstructure:"permission"`
Log string `mapstructure:"log"`
Expand All @@ -42,7 +41,7 @@ type Schedule struct {

func NewEmptySchedule(profileName, command string) *Schedule {
return &Schedule{
ProfileName: profileName,
Profiles: []string{profileName},
CommandName: command,
}

Check warning on line 46 in config/schedule.go

View check run for this annotation

Codecov / codecov/patch

config/schedule.go#L42-L46

Added lines #L42 - L46 were not covered by tests
}
Expand Down
4 changes: 2 additions & 2 deletions config/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestScheduleProperties(t *testing.T) {
schedule := Schedule{
ProfileName: "profile",
Profiles: []string{"profile"},
CommandName: "command name",
Schedules: []string{"1", "2", "3"},
Permission: "admin",
Expand All @@ -25,7 +25,7 @@ func TestScheduleProperties(t *testing.T) {
}

assert.Equal(t, "config", schedule.ConfigFile)
assert.Equal(t, "profile", schedule.ProfileName)
assert.Equal(t, "profile", schedule.Profiles[0])
assert.Equal(t, "command name", schedule.CommandName)
assert.ElementsMatch(t, []string{"1", "2", "3"}, schedule.Schedules)
assert.Equal(t, "admin", schedule.Permission)
Expand Down
4 changes: 2 additions & 2 deletions schedule_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ func statusJobs(handler schedule.Handler, profileName string, configs []*config.
func scheduleToConfig(sched *config.Schedule) *schedule.Config {
if len(sched.Schedules) == 0 {
// there's no schedule defined, so this record is for removal only
return schedule.NewRemoveOnlyConfig(sched.ProfileName, sched.CommandName)
return schedule.NewRemoveOnlyConfig(sched.Profiles[0], sched.CommandName)
}
return &schedule.Config{
ProfileName: sched.ProfileName,
ProfileName: sched.Profiles[0],
CommandName: sched.CommandName,
Schedules: sched.Schedules,
Permission: sched.Permission,
Expand Down
18 changes: 9 additions & 9 deletions schedule_jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSimpleScheduleJob(t *testing.T) {
})

scheduleConfig := &config.Schedule{
ProfileName: "profile",
Profiles: []string{"profile"},
CommandName: "backup",
Schedules: []string{"sched"},
}
Expand All @@ -58,7 +58,7 @@ func TestFailScheduleJob(t *testing.T) {
Return(errors.New("error creating job"))

scheduleConfig := &config.Schedule{
ProfileName: "profile",
Profiles: []string{"profile"},
CommandName: "backup",
Schedules: []string{"sched"},
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestRemoveJob(t *testing.T) {
})

scheduleConfig := &config.Schedule{
ProfileName: "profile",
Profiles: []string{"profile"},
CommandName: "backup",
Schedules: []string{"sched"},
}
Expand All @@ -107,7 +107,7 @@ func TestRemoveJobNoConfig(t *testing.T) {
})

scheduleConfig := &config.Schedule{
ProfileName: "profile",
Profiles: []string{"profile"},
CommandName: "backup",
}
err := removeJobs(handler, "profile", []*config.Schedule{scheduleConfig})
Expand All @@ -122,7 +122,7 @@ func TestFailRemoveJob(t *testing.T) {
Return(errors.New("error removing job"))

scheduleConfig := &config.Schedule{
ProfileName: "profile",
Profiles: []string{"profile"},
CommandName: "backup",
Schedules: []string{"sched"},
}
Expand All @@ -138,7 +138,7 @@ func TestNoFailRemoveUnknownJob(t *testing.T) {
Return(schedule.ErrorServiceNotFound)

scheduleConfig := &config.Schedule{
ProfileName: "profile",
Profiles: []string{"profile"},
CommandName: "backup",
Schedules: []string{"sched"},
}
Expand All @@ -154,7 +154,7 @@ func TestNoFailRemoveUnknownRemoveOnlyJob(t *testing.T) {
Return(schedule.ErrorServiceNotFound)

scheduleConfig := &config.Schedule{
ProfileName: "profile",
Profiles: []string{"profile"},
CommandName: "backup",
}
err := removeJobs(handler, "profile", []*config.Schedule{scheduleConfig})
Expand All @@ -181,7 +181,7 @@ func TestStatusJob(t *testing.T) {
handler.EXPECT().DisplayStatus("profile").Return(nil)

scheduleConfig := &config.Schedule{
ProfileName: "profile",
Profiles: []string{"profile"},
CommandName: "backup",
Schedules: []string{"sched"},
}
Expand All @@ -195,7 +195,7 @@ func TestStatusRemoveOnlyJob(t *testing.T) {
handler.EXPECT().Close()

scheduleConfig := &config.Schedule{
ProfileName: "profile",
Profiles: []string{"profile"},
CommandName: "backup",
}
err := statusJobs(handler, "profile", []*config.Schedule{scheduleConfig})
Expand Down

0 comments on commit bffa06d

Please sign in to comment.