Adds validation to setting and loading configuration options.

This commit is contained in:
sloumdrone 2019-09-23 21:18:46 -07:00
parent b219e659ab
commit df2a7fbd05
2 changed files with 38 additions and 6 deletions

View File

@ -375,12 +375,12 @@ func (c *client) doCommandAs(action string, values []string) {
case "SET", "S":
if _, ok := c.Options[values[0]]; ok {
val := strings.Join(values[1:], " ")
if values[0] == "theme" && val != "normal" && val != "inverse" {
c.SetMessage("Theme can only be set to 'normal' or 'inverse'", true)
if !validateOpt(values[0], val) {
c.SetMessage(fmt.Sprintf("Invalid setting for %q", values[0]), true)
c.DrawMessage()
return
}
c.Options[values[0]] = val
c.Options[values[0]] = lowerCaseOpt(values[0], val)
err := saveConfig()
if err != nil {
c.SetMessage("Value set, but error saving config to file", true)

38
main.go
View File

@ -56,6 +56,37 @@ func saveConfig() error {
return ioutil.WriteFile(bombadillo.Options["configlocation"] + "/.bombadillo.ini", []byte(opts.String()), 0644)
}
func validateOpt(opt, val string) bool {
var validOpts = map[string][]string{
"openhttp": []string{"true", "false"},
"theme": []string{"normal", "inverse"},
"terminalonly": []string{"true", "false"},
}
opt = strings.ToLower(opt)
val = strings.ToLower(val)
if _, ok := validOpts[opt]; ok {
for _, item := range validOpts[opt] {
if item == val {
return true
}
}
return false
} else {
return true
}
}
func lowerCaseOpt(opt, val string) string {
switch opt {
case "openhttp", "theme", "terminalonly":
return strings.ToLower(val)
default:
return val
}
}
func loadConfig() error {
file, err := os.Open(bombadillo.Options["configlocation"] + "/.bombadillo.ini")
if err != nil {
@ -79,10 +110,11 @@ func loadConfig() error {
}
if _, ok := bombadillo.Options[lowerkey]; ok {
if lowerkey == "theme" && v.Value != "normal" && v.Value != "inverse" {
v.Value = "normal"
if validateOpt(lowerkey, v.Value) {
bombadillo.Options[lowerkey] = v.Value
} else {
bombadillo.Options[lowerkey] = defaultOptions[lowerkey]
}
bombadillo.Options[lowerkey] = v.Value
}
}