Skip to content

Commit d75489f

Browse files
authored
Merge pull request #6 from GustavoCaso/remove-program-package-complexity
Remove program package complexity
2 parents c326e62 + c55d6ce commit d75489f

9 files changed

Lines changed: 956 additions & 716 deletions

File tree

cmd/meeseeks/run.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,19 @@ func startServer(ctx context.Context, cfg *config.Config, sockPath string) (*ser
130130
return nil, fmt.Errorf("failed to create program %s: %w", programConfig.Name, err)
131131
}
132132

133-
if addErr := s.AddProgram(prog); addErr != nil {
134-
return nil, fmt.Errorf("failed to add program %s: %w", programConfig.Name, addErr)
133+
// Check if this program has an interval - pass it to AddProgram
134+
if programConfig.Interval != "" {
135+
interval, intervalErr := programConfig.GetInterval()
136+
if intervalErr != nil {
137+
return nil, fmt.Errorf("failed to parse interval for program %s: %w", programConfig.Name, intervalErr)
138+
}
139+
if addErr := s.AddProgram(prog, interval); addErr != nil {
140+
return nil, fmt.Errorf("failed to add scheduled program %s: %w", programConfig.Name, addErr)
141+
}
142+
} else {
143+
if addErr := s.AddProgram(prog); addErr != nil {
144+
return nil, fmt.Errorf("failed to add program %s: %w", programConfig.Name, addErr)
145+
}
135146
}
136147
}
137148

cmd/meeseeks/status_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ func TestStatusCommand(t *testing.T) {
1717
name: "status format table",
1818
args: []string{"status"},
1919
expectedExit: 0,
20-
shouldContain: `NAME RUNS SUCCESS FAILED RUNNING INTERVAL STATUS
21-
---- ---- ------- ------ ------- -------- ------
22-
test-echo-detached 1 0 0 1 no running`,
20+
shouldContain: `NAME SUCCESS FAILED INTERVAL STATUS
21+
---- ------- ------ -------- ------
22+
test-echo-detached 0 0 no running`,
2323
},
2424
{
2525
name: "status format table single program",
2626
args: []string{"status", "test-echo-detached"},
2727
expectedExit: 0,
28-
shouldContain: `test-echo-detached 1 0 0 1 no running`,
28+
shouldContain: `test-echo-detached 0 0 no running`,
2929
},
3030
{
3131
name: "status format json",

cmd/meeseeks/utils.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"text/tabwriter"
1010

1111
"github.com/GustavoCaso/meeseeks/internal/config"
12+
"github.com/GustavoCaso/meeseeks/pkg/meeseeks"
1213
"github.com/GustavoCaso/meeseeks/pkg/program"
1314
)
1415

@@ -70,14 +71,6 @@ func createProgramFromConfig(pc config.ProgramConfig) (program.Program, error) {
7071
opts = append(opts, program.KeepStdinOpen())
7172
}
7273

73-
if pc.Interval != "" {
74-
interval, err := pc.GetInterval()
75-
if err != nil {
76-
return nil, fmt.Errorf("invalid interval: %w", err)
77-
}
78-
opts = append(opts, program.Interval(interval))
79-
}
80-
8174
if pc.Stdout != "" {
8275
file, err := os.OpenFile(pc.Stdout, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
8376
if err != nil {
@@ -102,9 +95,9 @@ func formatStatisticsAsTable(data any, programName string) error {
10295
if err != nil {
10396
return fmt.Errorf("failed to marshal data: %w", err)
10497
}
105-
programStatistics := []program.Statistics{}
98+
programStatistics := []meeseeks.Statistics{}
10699
if programName != "" {
107-
var programStatistic = program.Statistics{}
100+
var programStatistic = meeseeks.Statistics{}
108101

109102
err = json.Unmarshal(jsonBytes, &programStatistic)
110103
if err != nil {
@@ -113,7 +106,7 @@ func formatStatisticsAsTable(data any, programName string) error {
113106

114107
programStatistics = append(programStatistics, programStatistic)
115108
} else {
116-
var programsStatistic = []program.Statistics{}
109+
var programsStatistic = []meeseeks.Statistics{}
117110

118111
err = json.Unmarshal(jsonBytes, &programsStatistic)
119112
if err != nil {
@@ -124,26 +117,19 @@ func formatStatisticsAsTable(data any, programName string) error {
124117
}
125118

126119
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
127-
fmt.Fprintf(w, "NAME\tRUNS\tSUCCESS\tFAILED\tRUNNING\tINTERVAL\tSTATUS\n")
128-
fmt.Fprintf(w, "----\t----\t-------\t------\t-------\t--------\t------\n")
120+
fmt.Fprintf(w, "NAME\tSUCCESS\tFAILED\tINTERVAL\tSTATUS\n")
121+
fmt.Fprintf(w, "----\t-------\t------\t--------\t------\n")
129122

130123
for _, stats := range programStatistics {
131124
name := stats.ProgramName
132-
totalRuns := stats.TotalRuns
133125
successful := stats.Successful
134126
failed := stats.Failed
135-
running := stats.Running
136127
interval := "no"
137-
if stats.HasInterval {
138-
interval = stats.Interval.String()
139-
}
140128

141-
fmt.Fprintf(w, "%s\t%d\t%d\t%d\t%d\t%s\t%s\n",
129+
fmt.Fprintf(w, "%s\t%d\t%d\t%s\t%s\n",
142130
truncateString(name, 20),
143-
totalRuns,
144131
successful,
145132
failed,
146-
running,
147133
truncateString(interval, 10),
148134
stats.State)
149135
}

cmd/meeseeks/utils_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ func TestCreateProgramFromConfig(t *testing.T) {
4545
},
4646
expectedName: "test-interval",
4747
},
48-
{
49-
name: "program with invalid interval",
50-
config: config.ProgramConfig{
51-
Name: "test-invalid",
52-
Command: "echo",
53-
Interval: "invalid-duration",
54-
},
55-
expectError: true,
56-
errorMessage: "invalid interval",
57-
},
5848
}
5949

6050
for _, tt := range tests {

internal/server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ func (s *Server) Stop() error {
109109
return errors.Join(errs...)
110110
}
111111

112-
func (s *Server) AddProgram(prog program.Program) error {
113-
return s.meeseeks.AddProgram(prog)
112+
func (s *Server) AddProgram(prog program.Program, interval ...time.Duration) error {
113+
return s.meeseeks.AddProgram(prog, interval...)
114114
}
115115

116116
func (s *Server) StartPrograms(ctx context.Context) {

0 commit comments

Comments
 (0)