Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6326549

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

3 files changed

+29-2Lines changed: 29 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎cmd/set.go‎

Copy file name to clipboardExpand all lines: cmd/set.go
+11-1Lines 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 {
Collapse file

‎cmk.go‎

Copy file name to clipboardExpand all lines: cmk.go
+6-1Lines 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

Collapse file

‎config/config.go‎

Copy file name to clipboardExpand all lines: config/config.go
+12Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.