Skip to content

Commit

Permalink
break out region check
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Kania committed Jan 5, 2021
1 parent 6c85e00 commit aaa4cf4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ func initFlags(flag *pflag.FlagSet) {
flag.BoolP(flagVerbose, "v", false, "Print section lines")
}

func checkConfig(v *viper.Viper) error {

func checkRegion(v *viper.Viper) error {
region := v.GetString(flagAWSRegion)
if len(region) == 0 {
return errors.Wrap(&errInvalidRegion{Region: region}, fmt.Sprintf("%q is invalid", flagAWSRegion))
Expand All @@ -187,6 +186,16 @@ func checkConfig(v *viper.Viper) error {
return fmt.Errorf("%s is invalid: %w", flagAWSRegion, &errInvalidRegion{Region: region})
}

return nil
}

func checkConfig(v *viper.Viper) error {

err := checkRegion(v)
if err != nil {
return err
}

logLevel := strings.ToLower(v.GetString(flagLogLevel))
if len(logLevel) > 0 {
valid := false
Expand Down
9 changes: 7 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"log"
"os"
"testing"

"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/spf13/viper"
Expand Down Expand Up @@ -45,14 +46,14 @@ func (suite *cliTestSuite) TestCheckRegion() {
}
for _, testValue := range testValues {
suite.viper.Set(flagAWSRegion, testValue)
suite.NoError(checkConfig(suite.viper))
suite.NoError(checkRegion(suite.viper))
}
testValuesWithErrors := []string{
"AnyOtherRegionName",
}
for _, testValue := range testValuesWithErrors {
suite.viper.Set(flagAWSRegion, testValue)
suite.Error(checkConfig(suite.viper))
suite.Error(checkRegion(suite.viper))
}
}

Expand All @@ -63,3 +64,7 @@ func (suite *cliTestSuite) SetViper(v *viper.Viper) {
func (suite *cliTestSuite) SetLogger(logger *log.Logger) {
suite.logger = logger
}

func TestCLISuite(t *testing.T) {
suite.Run(t, &cliTestSuite{})
}

0 comments on commit aaa4cf4

Please sign in to comment.