Skip to content

Commit 2f4be7e

Browse files
committed
DRY up the configs a bit
1 parent 4f3d457 commit 2f4be7e

File tree

9 files changed

+89
-89
lines changed

9 files changed

+89
-89
lines changed

internal/goverseer/executioner/log_executioner/log_executioner.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ const (
1313
)
1414

1515
// LogExecutionerConfig is the configuration for a log executioner
16-
type LogExecutionerConfig struct {
16+
type Config struct {
1717
// Tag is a tag to add to the logs, by default it is empty
1818
Tag string
1919
}
2020

2121
// ParseConfig parses the config for a log executioner
2222
// It validates the config, sets defaults if missing, and returns the config
23-
func ParseConfig(config interface{}) (*LogExecutionerConfig, error) {
23+
func ParseConfig(config interface{}) (*Config, error) {
2424
cfgMap, ok := config.(map[string]interface{})
2525
if !ok {
2626
return nil, fmt.Errorf("invalid config")
2727
}
2828

29-
lec := &LogExecutionerConfig{
29+
lec := &Config{
3030
Tag: DefaultTag,
3131
}
3232

@@ -40,6 +40,8 @@ func ParseConfig(config interface{}) (*LogExecutionerConfig, error) {
4040
// LogExecutioner logs the data to stdout
4141
// It implements the Executioner interface
4242
type LogExecutioner struct {
43+
Config
44+
4345
// log is the logger
4446
log *slog.Logger
4547
}

internal/goverseer/executioner/shell_executioner/shell_executioner.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const (
2222
DefaultShell = "/bin/sh"
2323
)
2424

25-
// ShellExecutionerConfig is the configuration for a shell executioner
26-
type ShellExecutionerConfig struct {
25+
// Config is the configuration for a shell executioner
26+
type Config struct {
2727
// Command is the command to execute
2828
Command string
2929

@@ -33,13 +33,13 @@ type ShellExecutionerConfig struct {
3333

3434
// ParseConfig parses the config for a log executioner
3535
// It validates the config, sets defaults if missing, and returns the config
36-
func ParseConfig(config interface{}) (*ShellExecutionerConfig, error) {
36+
func ParseConfig(config interface{}) (*Config, error) {
3737
cfgMap, ok := config.(map[string]interface{})
3838
if !ok {
3939
return nil, fmt.Errorf("invalid config")
4040
}
4141

42-
cfg := &ShellExecutionerConfig{
42+
cfg := &Config{
4343
Shell: DefaultShell,
4444
}
4545

@@ -73,11 +73,7 @@ func ParseConfig(config interface{}) (*ShellExecutionerConfig, error) {
7373
// ShellExecutioner runs a shell command
7474
// It implements the Executioner interface
7575
type ShellExecutioner struct {
76-
// Command is the command to execute
77-
Command string
78-
79-
// Shell is the shell to use when executing the command
80-
Shell string
76+
Config
8177

8278
// workDir is the directory in which the ShellExecutioner will store
8379
// the command to run and the data to pass into the command
@@ -112,8 +108,10 @@ func New(cfg config.Config, log *slog.Logger) (*ShellExecutioner, error) {
112108
ctx, cancel := context.WithCancel(context.Background())
113109

114110
return &ShellExecutioner{
115-
Command: pcfg.Command,
116-
Shell: pcfg.Shell,
111+
Config: Config{
112+
Command: pcfg.Command,
113+
Shell: pcfg.Shell,
114+
},
117115
workDir: workDir,
118116
log: log,
119117
stop: make(chan struct{}),

internal/goverseer/executioner/shell_executioner/shell_executioner_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func TestParseConfig(t *testing.T) {
16-
var parsedConfig *ShellExecutionerConfig
16+
var parsedConfig *Config
1717

1818
parsedConfig, err := ParseConfig(map[string]interface{}{
1919
"command": "echo 123",
@@ -125,8 +125,10 @@ func TestShellExecutioner_Execute(t *testing.T) {
125125
tempDir, _ := os.MkdirTemp("", "goverseer-test")
126126
ctx, cancel := context.WithCancel(context.Background())
127127
executioner := ShellExecutioner{
128-
Command: "echo ${GOVERSEER_DATA}",
129-
Shell: DefaultShell,
128+
Config: Config{
129+
Command: "echo ${GOVERSEER_DATA}",
130+
Shell: DefaultShell,
131+
},
130132
workDir: tempDir,
131133
log: log,
132134
stop: make(chan struct{}),
@@ -144,8 +146,10 @@ func TestShellExecutioner_Stop(t *testing.T) {
144146
tempDir, _ := os.MkdirTemp("", "goverseer-test")
145147
ctx, cancel := context.WithCancel(context.Background())
146148
executioner := ShellExecutioner{
147-
Command: "for i in {1..1000}; do echo $i; sleep 1; done",
148-
Shell: DefaultShell,
149+
Config: Config{
150+
Command: "for i in {1..1000}; do echo $i; sleep 1; done",
151+
Shell: DefaultShell,
152+
},
149153
workDir: tempDir,
150154
log: log,
151155
stop: make(chan struct{}),

internal/goverseer/watcher/file_watcher/file_watcher.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const (
1515
DefaultPollSeconds = 5
1616
)
1717

18-
// FileWatcherConfig is the configuration for a file watcher
19-
type FileWatcherConfig struct {
18+
// Config is the configuration for a file watcher
19+
type Config struct {
2020
// Path is the path to the file to watch
2121
Path string
2222

@@ -26,13 +26,13 @@ type FileWatcherConfig struct {
2626

2727
// ParseConfig parses the config for a file watcher
2828
// It validates the config, sets defaults if missing, and returns the config
29-
func ParseConfig(config interface{}) (*FileWatcherConfig, error) {
29+
func ParseConfig(config interface{}) (*Config, error) {
3030
cfgMap, ok := config.(map[string]interface{})
3131
if !ok {
3232
return nil, fmt.Errorf("invalid config")
3333
}
3434

35-
cfg := &FileWatcherConfig{
35+
cfg := &Config{
3636
PollSeconds: DefaultPollSeconds,
3737
}
3838

@@ -63,11 +63,7 @@ func ParseConfig(config interface{}) (*FileWatcherConfig, error) {
6363

6464
// FileWatcher watches a file for changes and sends the path thru change channel
6565
type FileWatcher struct {
66-
// Path is the path to the file to watch
67-
Path string
68-
69-
// PollInterval is the interval to poll the file for changes
70-
PollInterval time.Duration
66+
Config
7167

7268
// lastValue is the last time the file was modified
7369
lastValue time.Time
@@ -87,11 +83,13 @@ func New(cfg config.Config, log *slog.Logger) (*FileWatcher, error) {
8783
}
8884

8985
return &FileWatcher{
90-
Path: tcfg.Path,
91-
PollInterval: time.Duration(tcfg.PollSeconds) * time.Second,
92-
lastValue: time.Now(),
93-
log: log,
94-
stop: make(chan struct{}),
86+
Config: Config{
87+
Path: tcfg.Path,
88+
PollSeconds: tcfg.PollSeconds,
89+
},
90+
lastValue: time.Now(),
91+
log: log,
92+
stop: make(chan struct{}),
9593
}, nil
9694
}
9795

@@ -104,7 +102,7 @@ func (w *FileWatcher) Watch(changes chan interface{}) {
104102
select {
105103
case <-w.stop:
106104
return
107-
case <-time.After(w.PollInterval):
105+
case <-time.After(time.Duration(w.PollSeconds) * time.Second):
108106
info, err := os.Stat(w.Path)
109107
if err != nil {
110108
w.log.Error("error getting file info",

internal/goverseer/watcher/file_watcher/file_watcher_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func touchFile(t *testing.T, filename string) {
3535
}
3636

3737
func TestParseConfig(t *testing.T) {
38-
var parsedConfig *FileWatcherConfig
38+
var parsedConfig *Config
3939
var err error
4040

4141
parsedConfig, err = ParseConfig(map[string]interface{}{
@@ -172,11 +172,13 @@ func TestFileWatcher_Stop(t *testing.T) {
172172
touchFile(t, testFilePath)
173173

174174
watcher := FileWatcher{
175-
Path: testFilePath,
176-
PollInterval: 1 * time.Second,
177-
lastValue: time.Now(),
178-
log: log,
179-
stop: make(chan struct{}),
175+
Config: Config{
176+
Path: testFilePath,
177+
PollSeconds: 1,
178+
},
179+
lastValue: time.Now(),
180+
log: log,
181+
stop: make(chan struct{}),
180182
}
181183

182184
changes := make(chan interface{})

internal/goverseer/watcher/gce_metadata_watcher/gce_metadata_watcher.go

+15-24
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ const (
3434
DefaultMetadataErrorWaitSeconds = 1
3535
)
3636

37-
// GceMetadataWatcherConfig is the configuration for a GCE metadata watcher
38-
type GceMetadataWatcherConfig struct {
37+
// Config is the configuration for a GCE metadata watcher
38+
type Config struct {
3939
// Source is the metadata source to watch
4040
// Valid values are 'instance' and 'project'
4141
// Default is 'instance'
@@ -63,13 +63,13 @@ type GceMetadataWatcherConfig struct {
6363

6464
// ParseConfig parses the config for the watcher
6565
// It validates the config, sets defaults if missing, and returns the config
66-
func ParseConfig(config interface{}) (*GceMetadataWatcherConfig, error) {
66+
func ParseConfig(config interface{}) (*Config, error) {
6767
cfgMap, ok := config.(map[string]interface{})
6868
if !ok {
6969
return nil, fmt.Errorf("invalid config")
7070
}
7171

72-
cfg := &GceMetadataWatcherConfig{
72+
cfg := &Config{
7373
Source: DefaultSource,
7474
Recursive: DefaultRecursive,
7575
MetadataUrl: DefaultMetadataUrl,
@@ -134,18 +134,7 @@ func ParseConfig(config interface{}) (*GceMetadataWatcherConfig, error) {
134134
}
135135

136136
type GceMetadataWatcher struct {
137-
// Key is the key to watch in the GCE metadata
138-
Key string
139-
140-
// Recursive is whether to recurse the metadata keys
141-
Recursive bool
142-
143-
// MetadataUrl is the URL this watcher will use when reading from the GCE
144-
// metadata server
145-
MetadataUrl string
146-
147-
// MetadataErrorWait is the time to wait before trying failed metadata request
148-
MetadataErrorWait time.Duration
137+
Config
149138

150139
// lastETag is the last etag, used to compare changes
151140
lastETag string
@@ -170,13 +159,15 @@ func New(cfg config.Config, log *slog.Logger) (*GceMetadataWatcher, error) {
170159
ctx, cancel := context.WithCancel(context.Background())
171160

172161
return &GceMetadataWatcher{
173-
Key: pcfg.Key,
174-
Recursive: pcfg.Recursive,
175-
MetadataUrl: pcfg.MetadataUrl,
176-
MetadataErrorWait: time.Duration(pcfg.MetadataErrorWaitSeconds) * time.Second,
177-
log: log,
178-
ctx: ctx,
179-
cancel: cancel,
162+
Config: Config{
163+
Key: pcfg.Key,
164+
Recursive: pcfg.Recursive,
165+
MetadataUrl: pcfg.MetadataUrl,
166+
MetadataErrorWaitSeconds: pcfg.MetadataErrorWaitSeconds,
167+
},
168+
log: log,
169+
ctx: ctx,
170+
cancel: cancel,
180171
}, nil
181172
}
182173

@@ -256,7 +247,7 @@ func (w *GceMetadataWatcher) Watch(change chan interface{}) {
256247
// bit before trying again to prevent hammering the metadata server.
257248
// Since we're in a for loop here the retrys will come VERY fast without
258249
// this sleep.
259-
time.Sleep(w.MetadataErrorWait)
250+
time.Sleep(time.Duration(w.MetadataErrorWaitSeconds) * time.Second)
260251
continue
261252
}
262253

internal/goverseer/watcher/gce_metadata_watcher/gce_metadata_watcher_test.go

+19-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
// TestParseConfig tests the ParseConfig function
1919
func TestParseConfig(t *testing.T) {
20-
var parsedConfig *GceMetadataWatcherConfig
20+
var parsedConfig *Config
2121

2222
testKey := "valid"
2323
parsedConfig, err := ParseConfig(map[string]interface{}{
@@ -203,13 +203,15 @@ func TestGceMetadataWatcher_Watch(t *testing.T) {
203203
ctx, cancel := context.WithCancel(context.Background())
204204

205205
watcher := GceMetadataWatcher{
206-
Key: "test",
207-
Recursive: true,
208-
MetadataUrl: mockServer.URL,
209-
MetadataErrorWait: 1 * time.Second,
210-
log: log,
211-
ctx: ctx,
212-
cancel: cancel,
206+
Config: Config{
207+
Key: "test",
208+
Recursive: true,
209+
MetadataUrl: mockServer.URL,
210+
MetadataErrorWaitSeconds: 1,
211+
},
212+
log: log,
213+
ctx: ctx,
214+
cancel: cancel,
213215
}
214216

215217
changes := make(chan interface{})
@@ -253,13 +255,15 @@ func TestGceMetadataWatcher_Stop(t *testing.T) {
253255
ctx, cancel := context.WithCancel(context.Background())
254256

255257
watcher := GceMetadataWatcher{
256-
Key: "test",
257-
Recursive: true,
258-
MetadataUrl: mockServer.URL,
259-
MetadataErrorWait: 1 * time.Second,
260-
log: log,
261-
ctx: ctx,
262-
cancel: cancel,
258+
Config: Config{
259+
Key: "test",
260+
Recursive: true,
261+
MetadataUrl: mockServer.URL,
262+
MetadataErrorWaitSeconds: 1,
263+
},
264+
log: log,
265+
ctx: ctx,
266+
cancel: cancel,
263267
}
264268

265269
changes := make(chan interface{})

0 commit comments

Comments
 (0)