diff --git a/config/config.go b/config/config.go index 2d5a169c..0ed11f80 100644 --- a/config/config.go +++ b/config/config.go @@ -137,15 +137,27 @@ func (c *Config) Service(service string) *Service { } // String returns the string key of the Service -func (s *Service) String(str string) (string, error) { +func (s *Service) String(key string) (string, error) { if s.config == nil { - return "", fmt.Errorf("missing config") + return "", ErrMissingConfig } - out, ok := s.config.Get(str).(string) + out, ok := s.config.Get(key).(string) if ok { return out, nil } - return "", fmt.Errorf("not found") + return "", ErrNotFound +} + +// Int returns the int key of the Service +func (s *Service) Int(key string) (int, error) { + if s.config == nil { + return 0, ErrMissingConfig + } + out, ok := s.config.Get(key).(int) + if ok { + return out, nil + } + return 0, ErrNotFound } // Available returns true if the Service exists and has data diff --git a/config/config_test.go b/config/config_test.go index 33cfad81..285b2359 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -9,22 +9,22 @@ import ( ) func TestNew(t *testing.T) { - config, err := config.New() + c, err := config.New() if !assert.Nil(t, err) { return } - if !assert.NotNil(t, config) { + if !assert.NotNil(t, c) { return } - iamService := config. + iamService := c. Region("us-east"). Env("client-test"). Service("iam") if !assert.NotNil(t, iamService) { return } - url, err := iamService.String("url") + url, err := iamService.String("iam_url") if !assert.Nil(t, err) { return } @@ -32,15 +32,15 @@ func TestNew(t *testing.T) { } func TestCartel(t *testing.T) { - config, err := config.New() + c, err := config.New() if !assert.Nil(t, err) { return } - if !assert.NotNil(t, config) { + if !assert.NotNil(t, c) { return } - cartelService := config. + cartelService := c. Region("us-east"). Service("cartel") if !assert.NotNil(t, cartelService) { @@ -81,14 +81,14 @@ func TestOpts(t *testing.T) { } func TestMissing(t *testing.T) { - config, err := config.New() + c, err := config.New() if !assert.Nil(t, err) { return } - if !assert.NotNil(t, config) { + if !assert.NotNil(t, c) { return } - missingService := config. + missingService := c. Region("us-east"). Service("bogus") assert.False(t, missingService.Available()) @@ -97,33 +97,36 @@ func TestMissing(t *testing.T) { } func TestServices(t *testing.T) { - config, err := config.New( + c, err := config.New( config.WithRegion("us-east"), config.WithEnv("client-test")) if !assert.Nil(t, err) { return } - if !assert.NotNil(t, config) { + if !assert.NotNil(t, c) { return } - services := config.Services() + services := c.Services() assert.Less(t, 0, len(services)) } func TestKeys(t *testing.T) { - config, err := config.New( + c, err := config.New( config.WithRegion("us-east"), config.WithEnv("client-test")) if !assert.Nil(t, err) { return } - if !assert.NotNil(t, config) { + if !assert.NotNil(t, c) { return } - cartel := config.Service("cartel") + cartel := c.Service("cartel") assert.True(t, cartel.Available()) keys := cartel.Keys() assert.Less(t, 0, len(keys)) _, err = cartel.String("bogus") assert.NotNil(t, err) + port, err := cartel.Int("port") + assert.Equal(t, config.ErrNotFound, err) + assert.Equal(t, 0, port) } diff --git a/config/errors.go b/config/errors.go new file mode 100644 index 00000000..8f79c621 --- /dev/null +++ b/config/errors.go @@ -0,0 +1,10 @@ +package config + +import ( + errors "golang.org/x/xerrors" +) + +var ( + ErrMissingConfig = errors.New("missing config") + ErrNotFound = errors.New("not found") +) \ No newline at end of file