-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make only specific keys configurable by validating them (#18)
- Loading branch information
Showing
2 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type Validator func(interface{}) (interface{}, error) | ||
|
||
// Enum is just a string of a list | ||
func EnumValidator(validValues ...string) Validator { | ||
return func(inputValue interface{}) (interface{}, error) { | ||
_, ok := inputValue.(string) | ||
if !ok { | ||
return "", fmt.Errorf("value \"%s\" is not a string, but of type %T", inputValue, inputValue) | ||
} | ||
isInList := false | ||
for _, validValue := range validValues { | ||
if inputValue == validValue { | ||
isInList = true | ||
break | ||
} | ||
} | ||
if !isInList { | ||
return "", fmt.Errorf("value \"%s\" is not a valid value. Valid Values are %s", inputValue, validValues) | ||
} | ||
|
||
return inputValue, nil | ||
} | ||
} | ||
|
||
// SimpleStringValidator validates if a input is a "simple" string - only single-line strings are supported | ||
func SimpleStringValidator() Validator { | ||
return func(inputValue interface{}) (interface{}, error) { | ||
_, ok := inputValue.(string) | ||
if !ok { | ||
return "", fmt.Errorf("value \"%s\" is not a string, but of type %T", inputValue, inputValue) | ||
} | ||
|
||
if strings.ContainsAny(inputValue.(string), "\r\n") { | ||
return "", fmt.Errorf("value \"%s\" contains illegal line break", inputValue) | ||
} | ||
|
||
return inputValue, nil | ||
} | ||
} | ||
|
||
// Entry contains all the data required for Validation and Convertion | ||
type Entry struct { | ||
Validator Validator | ||
} | ||
|
||
type Configuration map[string]Entry | ||
|
||
func (c Configuration) ValidateEntry(key string, value interface{}) (interface{}, error) { | ||
e, ok := c[key] | ||
if !ok { | ||
return "", fmt.Errorf("key \"%s\" is not a valid key", key) | ||
} | ||
return e.Validator(value) | ||
} | ||
|
||
var BbConfigurationValidation Configuration = map[string]Entry{ | ||
"username": { | ||
Validator: SimpleStringValidator(), | ||
}, | ||
"password": { | ||
Validator: SimpleStringValidator(), | ||
}, | ||
"remote": { | ||
Validator: SimpleStringValidator(), | ||
}, | ||
"git_protocol": { | ||
Validator: EnumValidator("ssh", "https"), | ||
}, | ||
"sync-method": { | ||
Validator: EnumValidator("merge", "rebase"), | ||
}, | ||
} |