mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-25 01:40:30 +00:00
f4f61cb57a
Fixes #1810
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package transform
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
|
|
json "github.com/ghodss/yaml"
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
// argsToEnv uses reflection to convert a map[string]interface to a list
|
|
// of environment variables.
|
|
func argsToEnv(from map[string]interface{}, to map[string]string) error {
|
|
|
|
for k, v := range from {
|
|
if v == nil {
|
|
to[k] = ""
|
|
continue
|
|
}
|
|
|
|
t := reflect.TypeOf(v)
|
|
vv := reflect.ValueOf(v)
|
|
|
|
k = "PLUGIN_" + strings.ToUpper(k)
|
|
|
|
switch t.Kind() {
|
|
case reflect.Bool:
|
|
to[k] = strconv.FormatBool(vv.Bool())
|
|
|
|
case reflect.String:
|
|
to[k] = vv.String()
|
|
|
|
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8:
|
|
to[k] = fmt.Sprintf("%v", vv.Int())
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
to[k] = fmt.Sprintf("%v", vv.Float())
|
|
|
|
case reflect.Map:
|
|
yml, _ := yaml.Marshal(vv.Interface())
|
|
out, _ := json.YAMLToJSON(yml)
|
|
to[k] = string(out)
|
|
|
|
case reflect.Slice:
|
|
out, err := yaml.Marshal(vv.Interface())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
in := []string{}
|
|
err = yaml.Unmarshal(out, &in)
|
|
if err == nil {
|
|
to[k] = strings.Join(in, ",")
|
|
} else {
|
|
out, err = json.YAMLToJSON(out)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
to[k] = string(out)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|