Skip to content

Commit

Permalink
fixes region validation (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
jritsema authored Mar 20, 2019
1 parent eb68b9a commit 7d90ae4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
22 changes: 16 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,8 @@ CloudWatch Logs, and Amazon Route 53 into an easy-to-use CLI.`,
}
}

for _, validRegion := range validRegions {
if region == validRegion {
break
}

console.IssueExit("Invalid region: %s [valid regions: %s]", region, strings.Join(validRegions, ", "))
if err := validateRegion(region); err != nil {
console.IssueExit(err.Error())
}

config := &aws.Config{
Expand Down Expand Up @@ -268,3 +264,17 @@ func validateCpuAndMemory(inputCpuUnits, inputMebibytes string) error {
func validateMebibytes(mebibytes, min, max int64) bool {
return mebibytes >= min && mebibytes <= max && mebibytes%mebibytesInGibibyte == 0
}

func validateRegion(region string) error {
found := false
for _, validRegion := range validRegions {
if region == validRegion {
found = true
break
}
}
if !found {
return fmt.Errorf("Invalid region: %s [valid regions: %s]", region, strings.Join(validRegions, ", "))
}
return nil
}
22 changes: 22 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,25 @@ func TestReadEnvFileMissing(t *testing.T) {
t.Errorf("Expected 0 results, got %d results", len(results))
}
}

func TestRegion(t *testing.T) {
region := "us-east-1"
err := validateRegion(region)
if err != nil {
t.Error(err)
}
region = "ap-south-1"
err = validateRegion(region)
if err != nil {
t.Error(err)
}

}

func TestRegion_Invalid(t *testing.T) {
region := "invalid"
err := validateRegion(region)
if err == nil {
t.Error("expecting invalid region")
}
}

0 comments on commit 7d90ae4

Please sign in to comment.