woodpecker/pipeline/frontend/yaml/compiler/params.go

113 lines
2.5 KiB
Go
Raw Normal View History

2017-03-05 07:56:08 +00:00
package compiler
import (
"fmt"
"reflect"
"strconv"
"strings"
2019-11-14 19:16:03 +00:00
"gopkg.in/yaml.v3"
"github.com/woodpecker-ci/woodpecker/shared/yml"
2017-03-05 07:56:08 +00:00
)
// paramsToEnv uses reflection to convert a map[string]interface to a list
// of environment variables.
func paramsToEnv(from map[string]interface{}, to map[string]string, secrets map[string]Secret) (err error) {
if to == nil {
return fmt.Errorf("no map to write to")
}
2017-03-05 07:56:08 +00:00
for k, v := range from {
if v == nil || len(k) == 0 {
2017-03-05 07:56:08 +00:00
continue
}
to[sanitizeParamKey(k)], err = sanitizeParamValue(v, secrets)
if err != nil {
return err
}
}
return nil
}
2017-03-05 07:56:08 +00:00
func sanitizeParamKey(k string) string {
return "PLUGIN_" +
strings.ToUpper(
strings.ReplaceAll(k, ".", "_"),
)
}
2017-03-05 07:56:08 +00:00
func isComplex(t reflect.Kind) bool {
switch t {
case reflect.Bool,
reflect.String,
reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Float32, reflect.Float64:
return false
default:
return true
}
}
2017-03-05 07:56:08 +00:00
func sanitizeParamValue(v interface{}, secrets map[string]Secret) (string, error) {
t := reflect.TypeOf(v)
vv := reflect.ValueOf(v)
2017-03-05 07:56:08 +00:00
switch t.Kind() {
case reflect.Bool:
return strconv.FormatBool(vv.Bool()), nil
2017-03-05 07:56:08 +00:00
case reflect.String:
return vv.String(), nil
2017-03-05 07:56:08 +00:00
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8:
return fmt.Sprintf("%v", vv.Int()), nil
2017-03-05 07:56:08 +00:00
case reflect.Float32, reflect.Float64:
return fmt.Sprintf("%v", vv.Float()), nil
2017-03-05 07:56:08 +00:00
case reflect.Map:
if fromSecret, ok := v.(map[string]interface{}); ok {
if secretNameI, ok := fromSecret["from_secret"]; ok {
if secretName, ok := secretNameI.(string); ok {
if secret, ok := secrets[strings.ToLower(secretName)]; ok {
return secret.Value, nil
}
return "", fmt.Errorf("no secret found for %q", secretName)
}
}
}
ymlOut, _ := yaml.Marshal(vv.Interface())
out, _ := yml.ToJSON(ymlOut)
return string(out), nil
2017-03-05 07:56:08 +00:00
case reflect.Slice, reflect.Array:
if vv.Len() == 0 {
return "", nil
}
if !isComplex(t.Elem().Kind()) || t.Elem().Kind() == reflect.Interface {
in := make([]string, vv.Len())
for i := 0; i < vv.Len(); i++ {
var err error
if in[i], err = sanitizeParamValue(vv.Index(i).Interface(), secrets); err != nil {
return "", err
2017-03-05 07:56:08 +00:00
}
}
return strings.Join(in, ","), nil
2017-03-05 07:56:08 +00:00
}
// it's complex use yml.ToJSON
fallthrough
default:
out, err := yaml.Marshal(vv.Interface())
if err != nil {
return "", err
}
out, err = yml.ToJSON(out)
if err != nil {
return "", err
}
return string(out), nil
}
2017-03-05 07:56:08 +00:00
}