woodpecker/pipeline/frontend/yaml/compiler/params.go
6543 d3eea72663
Bump deps (#1125)
* upgrade to codeberg.org/6543/go-yaml2json v0.2.1

* upgraded github.com/bmatcuk/doublestar/v4 v4.0.2 => v4.2.0

* upgraded github.com/docker/cli v20.10.14+incompatible => v20.10.17+incompatible

* upgraded github.com/docker/docker v20.10.14+incompatible => v20.10.17+incompatible

* upgraded github.com/gin-gonic/gin v1.7.7 => v1.8.1

* upgraded github.com/golang-jwt/jwt/v4 v4.4.1 => v4.4.2

* upgraded github.com/moby/moby v20.10.14+incompatible => v20.10.17+incompatible

* upgraded github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 => v0.0.0-20220808134915-39b0c02b01ae

* upgraded github.com/lafriks/ttlcache/v3 v3.1.0 => v3.2.0

* upgraded github.com/mattn/go-sqlite3 v1.14.12 => v1.14.15

* upgraded github.com/lib/pq v1.10.5 => v1.10.6

* github.com/prometheus/client_golang v1.12.1 => v1.13.0

* upgraded github.com/urfave/cli/v2 v2.5.1 => v2.11.2

* upgraded github.com/rs/zerolog v1.26.1 => v1.27.0

* upgraded golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 => v0.0.0-20220822191816-0ebed06d0094

* upgraded github.com/xanzy/go-gitlab v0.64.0 => v0.73.1

* upgraded google.golang.org/grpc v1.47.0 => v1.49.0
2022-08-25 08:09:05 +02:00

111 lines
2.5 KiB
Go

package compiler
import (
"fmt"
"reflect"
"strconv"
"strings"
"codeberg.org/6543/go-yaml2json"
"gopkg.in/yaml.v3"
)
// 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")
}
for k, v := range from {
if v == nil || len(k) == 0 {
continue
}
to[sanitizeParamKey(k)], err = sanitizeParamValue(v, secrets)
if err != nil {
return err
}
}
return nil
}
func sanitizeParamKey(k string) string {
return "PLUGIN_" +
strings.ToUpper(
strings.ReplaceAll(k, ".", "_"),
)
}
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
}
}
func sanitizeParamValue(v interface{}, secrets map[string]Secret) (string, error) {
t := reflect.TypeOf(v)
vv := reflect.ValueOf(v)
switch t.Kind() {
case reflect.Bool:
return strconv.FormatBool(vv.Bool()), nil
case reflect.String:
return vv.String(), nil
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8:
return fmt.Sprintf("%v", vv.Int()), nil
case reflect.Float32, reflect.Float64:
return fmt.Sprintf("%v", vv.Float()), nil
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, _ := yaml2json.Convert(ymlOut)
return string(out), nil
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
}
}
return strings.Join(in, ","), nil
}
// it's complex use yml.ToJSON
fallthrough
default:
out, err := yaml.Marshal(vv.Interface())
if err != nil {
return "", err
}
out, err = yaml2json.Convert(out)
if err != nil {
return "", err
}
return string(out), nil
}
}