Skip to content

Commit 6326549

Browse files
authored
Validate args passed - fail fast in case of invalid args (#79)
1 parent 5d22bc4 commit 6326549

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

cmd/set.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"reflect"
2323
"strings"
24+
"errors"
2425

2526
"github.com/apache/cloudstack-cloudmonkey/config"
2627
)
@@ -33,7 +34,7 @@ func init() {
3334
"prompt": {"🐵", "🐱", "random"},
3435
"asyncblock": {"true", "false"},
3536
"timeout": {"600", "1800", "3600"},
36-
"output": {"json", "text", "table", "column", "csv"},
37+
"output": config.GetOutputFormats(),
3738
"profile": {},
3839
"url": {},
3940
"username": {},
@@ -56,6 +57,15 @@ func init() {
5657
fmt.Println("Usage: set <subcommand> <option>. Press tab-tab to see available subcommands and options.")
5758
return nil
5859
}
60+
if subCommand == "display" {
61+
subCommand = "output"
62+
}
63+
validArgs := r.Command.SubCommands[subCommand]
64+
if len(validArgs) != 0 && subCommand != "timeout" {
65+
if !config.CheckIfValuePresent(validArgs, value) {
66+
return errors.New("Invalid value set for " + subCommand +". Supported values: " + strings.Join(validArgs, ", ") )
67+
}
68+
}
5969
r.Config.UpdateConfig(subCommand, value, true)
6070

6171
if subCommand == "profile" && r.Config.HasShell {

cmk.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ func init() {
4141
}
4242

4343
func main() {
44-
outputFormat := flag.String("o", "", "output format: json, text, table, column, csv")
44+
validFormats := strings.Join(config.GetOutputFormats(), ",")
45+
outputFormat := flag.String("o", "", "output format: " + validFormats)
4546
showVersion := flag.Bool("v", false, "show version")
4647
debug := flag.Bool("d", false, "enable debug mode")
4748
profile := flag.String("p", "", "server profile")
@@ -61,6 +62,10 @@ func main() {
6162
}
6263

6364
if *outputFormat != "" {
65+
if !config.CheckIfValuePresent(config.GetOutputFormats(), *outputFormat) {
66+
fmt.Println("Invalid value set for output format. Supported values: " + validFormats)
67+
os.Exit(1)
68+
}
6469
cfg.UpdateConfig("output", *outputFormat, false)
6570
}
6671

config/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ type Config struct {
7474
ActiveProfile *ServerProfile
7575
}
7676

77+
func GetOutputFormats() []string {
78+
return []string {"column", "csv", "json", "table", "text"}
79+
}
80+
81+
func CheckIfValuePresent(dataset []string, element string) bool {
82+
for _, arg := range dataset {
83+
if arg == element {
84+
return true
85+
}
86+
}
87+
return false
88+
}
7789
// CacheFile returns the path to the cache file for a server profile
7890
func (c Config) CacheFile() string {
7991
cacheDir := path.Join(c.Dir, "profiles")

0 commit comments

Comments
 (0)