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"
|
2021-12-07 00:13:02 +00:00
|
|
|
|
|
|
|
"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.
|
2021-12-13 19:33:07 +00:00
|
|
|
func paramsToEnv(from map[string]interface{}, to map[string]string, secrets map[string]Secret) (err error) {
|
2021-12-07 00:13:02 +00:00
|
|
|
if to == nil {
|
|
|
|
return fmt.Errorf("no map to write to")
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
for k, v := range from {
|
2021-12-07 00:13:02 +00:00
|
|
|
if v == nil || len(k) == 0 {
|
2017-03-05 07:56:08 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-12-13 19:33:07 +00:00
|
|
|
to[sanitizeParamKey(k)], err = sanitizeParamValue(v, secrets)
|
2021-12-07 00:13:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2021-12-07 00:13:02 +00:00
|
|
|
func sanitizeParamKey(k string) string {
|
|
|
|
return "PLUGIN_" +
|
|
|
|
strings.ToUpper(
|
|
|
|
strings.ReplaceAll(k, ".", "_"),
|
|
|
|
)
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2021-12-07 00:13:02 +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
|
|
|
|
2021-12-13 19:33:07 +00:00
|
|
|
func sanitizeParamValue(v interface{}, secrets map[string]Secret) (string, error) {
|
2021-12-07 00:13:02 +00:00
|
|
|
t := reflect.TypeOf(v)
|
|
|
|
vv := reflect.ValueOf(v)
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2021-12-07 00:13:02 +00:00
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
return strconv.FormatBool(vv.Bool()), nil
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2021-12-07 00:13:02 +00:00
|
|
|
case reflect.String:
|
|
|
|
return vv.String(), nil
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2021-12-07 00:13:02 +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
|
|
|
|
2021-12-07 00:13:02 +00:00
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
return fmt.Sprintf("%v", vv.Float()), nil
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2021-12-07 00:13:02 +00:00
|
|
|
case reflect.Map:
|
2021-12-13 19:33:07 +00:00
|
|
|
if fromSecret, ok := v.(map[string]interface{}); ok {
|
|
|
|
if secretNameI, ok := fromSecret["from_secret"]; ok {
|
|
|
|
if secretName, ok := secretNameI.(string); ok {
|
2022-03-19 11:34:32 +00:00
|
|
|
if secret, ok := secrets[strings.ToLower(secretName)]; ok {
|
2021-12-13 19:33:07 +00:00
|
|
|
return secret.Value, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("no secret found for %q", secretName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-07 00:13:02 +00:00
|
|
|
ymlOut, _ := yaml.Marshal(vv.Interface())
|
|
|
|
out, _ := yml.ToJSON(ymlOut)
|
|
|
|
return string(out), nil
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2021-12-07 00:13:02 +00:00
|
|
|
case reflect.Slice, reflect.Array:
|
2021-12-08 17:17:52 +00:00
|
|
|
if vv.Len() == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
if !isComplex(t.Elem().Kind()) || t.Elem().Kind() == reflect.Interface {
|
2021-12-07 00:13:02 +00:00
|
|
|
in := make([]string, vv.Len())
|
|
|
|
for i := 0; i < vv.Len(); i++ {
|
|
|
|
var err error
|
2021-12-13 19:33:07 +00:00
|
|
|
if in[i], err = sanitizeParamValue(vv.Index(i).Interface(), secrets); err != nil {
|
2021-12-07 00:13:02 +00:00
|
|
|
return "", err
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-07 00:13:02 +00:00
|
|
|
return strings.Join(in, ","), nil
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 00:13:02 +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
|
|
|
}
|