dagger input bool implementation

Signed-off-by: Guillaume de Rouville <guillaume.derouville@gmail.com>
This commit is contained in:
Guillaume de Rouville
2021-08-24 16:00:53 +02:00
parent 36d0ebd863
commit 6fe0ec367a
5 changed files with 103 additions and 0 deletions

View File

@@ -35,6 +35,7 @@ type Input struct {
JSON *jsonInput `yaml:"json,omitempty"`
YAML *yamlInput `yaml:"yaml,omitempty"`
File *fileInput `yaml:"file,omitempty"`
Bool *boolInput `yaml:"bool,omitempty"`
}
func (i Input) Compile(key string, state *State) (*compiler.Value, error) {
@@ -55,6 +56,8 @@ func (i Input) Compile(key string, state *State) (*compiler.Value, error) {
return i.YAML.Compile(key, state)
case i.File != nil:
return i.File.Compile(key, state)
case i.Bool != nil:
return i.Bool.Compile(key, state)
default:
return nil, fmt.Errorf("input has not been set")
}
@@ -201,6 +204,27 @@ func (i secretInput) PlainText() string {
return string(i)
}
// An input value encoded as Bool
func BoolInput(data string) Input {
i := boolInput(data)
return Input{
Bool: &i,
}
}
type boolInput string
func (i boolInput) Compile(_ string, _ *State) (*compiler.Value, error) {
s := map[boolInput]struct{}{
"true": {},
"false": {},
}
if _, ok := s[i]; ok {
return compiler.DecodeJSON("", []byte(i))
}
return nil, fmt.Errorf("%q is not a valid boolean: <true|false>", i)
}
// An input value encoded as JSON
func JSONInput(data string) Input {
i := jsonInput(data)