Adds validation to setting and loading configuration options.
This commit is contained in:
parent
b219e659ab
commit
df2a7fbd05
|
@ -375,12 +375,12 @@ func (c *client) doCommandAs(action string, values []string) {
|
||||||
case "SET", "S":
|
case "SET", "S":
|
||||||
if _, ok := c.Options[values[0]]; ok {
|
if _, ok := c.Options[values[0]]; ok {
|
||||||
val := strings.Join(values[1:], " ")
|
val := strings.Join(values[1:], " ")
|
||||||
if values[0] == "theme" && val != "normal" && val != "inverse" {
|
if !validateOpt(values[0], val) {
|
||||||
c.SetMessage("Theme can only be set to 'normal' or 'inverse'", true)
|
c.SetMessage(fmt.Sprintf("Invalid setting for %q", values[0]), true)
|
||||||
c.DrawMessage()
|
c.DrawMessage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Options[values[0]] = val
|
c.Options[values[0]] = lowerCaseOpt(values[0], val)
|
||||||
err := saveConfig()
|
err := saveConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.SetMessage("Value set, but error saving config to file", true)
|
c.SetMessage("Value set, but error saving config to file", true)
|
||||||
|
|
38
main.go
38
main.go
|
@ -56,6 +56,37 @@ func saveConfig() error {
|
||||||
return ioutil.WriteFile(bombadillo.Options["configlocation"] + "/.bombadillo.ini", []byte(opts.String()), 0644)
|
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 {
|
func loadConfig() error {
|
||||||
file, err := os.Open(bombadillo.Options["configlocation"] + "/.bombadillo.ini")
|
file, err := os.Open(bombadillo.Options["configlocation"] + "/.bombadillo.ini")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -79,10 +110,11 @@ func loadConfig() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := bombadillo.Options[lowerkey]; ok {
|
if _, ok := bombadillo.Options[lowerkey]; ok {
|
||||||
if lowerkey == "theme" && v.Value != "normal" && v.Value != "inverse" {
|
if validateOpt(lowerkey, v.Value) {
|
||||||
v.Value = "normal"
|
bombadillo.Options[lowerkey] = v.Value
|
||||||
|
} else {
|
||||||
|
bombadillo.Options[lowerkey] = defaultOptions[lowerkey]
|
||||||
}
|
}
|
||||||
bombadillo.Options[lowerkey] = v.Value
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue