diff --git a/cmd/wait-for/main.go b/cmd/wait-for/main.go index 675f1e3..475791a 100644 --- a/cmd/wait-for/main.go +++ b/cmd/wait-for/main.go @@ -32,7 +32,13 @@ func main() { logger = waitfor.NullLogger } - err := waitfor.WaitOn(configFile, fs, logger, timeoutParam, flag.Args(), waitfor.SupportedWaiters) + config, err := waitfor.OpenConfig(configFile, timeoutParam, fs) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "%v", err) + os.Exit(1) + } + + err = waitfor.WaitOn(config, logger, flag.Args(), waitfor.SupportedWaiters) if err != nil { _, _ = fmt.Fprintf(os.Stderr, "%v", err) os.Exit(1) diff --git a/config.go b/config.go index 61d7ffd..6651a4f 100644 --- a/config.go +++ b/config.go @@ -6,7 +6,7 @@ import ( "strings" "time" - "gopkg.in/yaml.v3" + yaml "gopkg.in/yaml.v3" ) // DefautTimeout is the amount of time to wait for target before failing diff --git a/waitfor.go b/waitfor.go index 51a3c71..c918a0f 100644 --- a/waitfor.go +++ b/waitfor.go @@ -27,11 +27,7 @@ var SupportedWaiters = map[string]WaiterFunc{ // WaitOn implements waiting for many targets, using the location of config file provided with named targets to wait until // all of those targets are responding as expected -func WaitOn(configFile string, fs afero.Fs, logger Logger, timeoutParam string, targets []string, waiters map[string]WaiterFunc) error { - config, err := openConfig(configFile, timeoutParam, fs) - if err != nil { - return err - } +func WaitOn(config *Config, logger Logger, targets []string, waiters map[string]WaiterFunc) error { for _, target := range targets { if !config.GotTarget(target) { @@ -42,7 +38,7 @@ func WaitOn(configFile string, fs afero.Fs, logger Logger, timeoutParam string, } } filtered := config.Filter(targets) - err = waitOnTargets(logger, filtered.Targets, waiters) + err := waitOnTargets(logger, filtered.Targets, waiters) if err != nil { return err } @@ -50,7 +46,7 @@ func WaitOn(configFile string, fs afero.Fs, logger Logger, timeoutParam string, return nil } -func openConfig(configFile, defaultTimeout string, fs afero.Fs) (*Config, error) { +func OpenConfig(configFile, defaultTimeout string, fs afero.Fs) (*Config, error) { var config *Config if configFile == "" { config = NewConfig() diff --git a/waitfor_test.go b/waitfor_test.go index 961ebba..a7007d8 100644 --- a/waitfor_test.go +++ b/waitfor_test.go @@ -25,7 +25,7 @@ func Test_isSuccess(t *testing.T) { func TestOpenConfig_errorOnFileOpenFailure(t *testing.T) { mockFS := afero.NewMemMapFs() - config, err := openConfig("./wait-for.yaml", "", afero.NewReadOnlyFs(mockFS)) + config, err := OpenConfig("./wait-for.yaml", "", afero.NewReadOnlyFs(mockFS)) assert.Error(t, err) assert.Nil(t, config) } @@ -34,7 +34,7 @@ func TestOpenConfig_errorOnFileParsingFailure(t *testing.T) { mockFS := afero.NewMemMapFs() _ = afero.WriteFile(mockFS, "./wait-for.yaml", []byte("this isn't yaml!"), 0444) - config, err := openConfig("./wait-for.yaml", "", afero.NewReadOnlyFs(mockFS)) + config, err := OpenConfig("./wait-for.yaml", "", afero.NewReadOnlyFs(mockFS)) assert.Error(t, err) assert.Nil(t, config) } @@ -43,7 +43,7 @@ func TestOpenConfig_errorOnParsingDefaultTimeout(t *testing.T) { mockFS := afero.NewMemMapFs() _ = afero.WriteFile(mockFS, "./wait-for.yaml", []byte(defaultConfigYaml()), 0444) - config, err := openConfig("./wait-for.yaml", "invalid duration", afero.NewReadOnlyFs(mockFS)) + config, err := OpenConfig("./wait-for.yaml", "invalid duration", afero.NewReadOnlyFs(mockFS)) assert.Error(t, err) assert.Nil(t, config) } @@ -52,24 +52,19 @@ func TestOpenConfig_defaultTimeoutCanBeSet(t *testing.T) { mockFS := afero.NewMemMapFs() _ = afero.WriteFile(mockFS, "./wait-for.yaml", []byte(defaultConfigYaml()), 0444) - config, err := openConfig("./wait-for.yaml", "19s", afero.NewReadOnlyFs(mockFS)) + config, err := OpenConfig("./wait-for.yaml", "19s", afero.NewReadOnlyFs(mockFS)) assert.NoError(t, err) assert.NotNil(t, config) assert.Equal(t, time.Second*19, config.DefaultTimeout) } -func TestRun_errorsOnConfigFileFailure(t *testing.T) { - err := WaitOn("non-existent", afero.NewMemMapFs(), NullLogger, "invalid", []string{"http://localhost"}, map[string]WaiterFunc{}) +func TestWaitOn_errorsInvalidTarget(t *testing.T) { + err := WaitOn(NewConfig(), NullLogger, []string{"localhost"}, map[string]WaiterFunc{}) assert.Error(t, err) } func TestRun_errorsOnParseFailure(t *testing.T) { - err := WaitOn("", afero.NewMemMapFs(), NullLogger, "invalid", []string{"http://localhost"}, map[string]WaiterFunc{}) - assert.Error(t, err) -} - -func TestRun_errorsOnConfigFailure(t *testing.T) { - err := WaitOn("", afero.NewMemMapFs(), NullLogger, "invalid", []string{"localhost"}, map[string]WaiterFunc{}) + err := WaitOn(NewConfig(), NullLogger, []string{"http://localhost"}, map[string]WaiterFunc{}) assert.Error(t, err) }