engine/env.go
a80c059b
 package engine
 
 import (
 	"bytes"
930ec9f5
 	"encoding/json"
a80c059b
 	"fmt"
930ec9f5
 	"io"
 	"strconv"
 	"strings"
2977fd2b
 	"time"
0cd30cf3
 
 	"github.com/docker/docker/utils"
a80c059b
 )
 
 type Env []string
 
6e0bc060
 // Get returns the last value associated with the given key. If there are no
 // values associated with the key, Get returns the empty string.
a80c059b
 func (env *Env) Get(key string) (value string) {
6e0bc060
 	// not using Map() because of the extra allocations https://github.com/docker/docker/pull/7488#issuecomment-51638315
a80c059b
 	for _, kv := range *env {
 		if strings.Index(kv, "=") == -1 {
 			continue
 		}
 		parts := strings.SplitN(kv, "=", 2)
 		if parts[0] != key {
 			continue
 		}
 		if len(parts) < 2 {
 			value = ""
 		} else {
 			value = parts[1]
 		}
 	}
 	return
 }
 
 func (env *Env) Exists(key string) bool {
 	_, exists := env.Map()[key]
 	return exists
 }
 
9b23178f
 // Len returns the number of keys in the environment.
 // Note that len(env) might be different from env.Len(),
 // because the same key might be set multiple times.
 func (env *Env) Len() int {
 	return len(env.Map())
 }
 
2019a73f
 func (env *Env) Init(src *Env) {
0469e7d0
 	(*env) = make([]string, 0, len(*src))
2019a73f
 	for _, val := range *src {
 		(*env) = append((*env), val)
 	}
 }
 
a80c059b
 func (env *Env) GetBool(key string) (value bool) {
 	s := strings.ToLower(strings.Trim(env.Get(key), " \t"))
 	if s == "" || s == "0" || s == "no" || s == "false" || s == "none" {
 		return false
 	}
 	return true
 }
 
 func (env *Env) SetBool(key string, value bool) {
 	if value {
 		env.Set(key, "1")
 	} else {
 		env.Set(key, "0")
 	}
 }
 
2977fd2b
 func (env *Env) GetTime(key string) (time.Time, error) {
 	t, err := time.Parse(time.RFC3339Nano, env.Get(key))
 	return t, err
 }
 
 func (env *Env) SetTime(key string, t time.Time) {
 	env.Set(key, t.Format(time.RFC3339Nano))
 }
 
85b93382
 func (env *Env) GetInt(key string) int {
 	return int(env.GetInt64(key))
 }
 
 func (env *Env) GetInt64(key string) int64 {
a80c059b
 	s := strings.Trim(env.Get(key), " \t")
 	val, err := strconv.ParseInt(s, 10, 64)
 	if err != nil {
28b5ae8c
 		return 0
a80c059b
 	}
 	return val
 }
 
85b93382
 func (env *Env) SetInt(key string, value int) {
 	env.Set(key, fmt.Sprintf("%d", value))
 }
 
 func (env *Env) SetInt64(key string, value int64) {
a80c059b
 	env.Set(key, fmt.Sprintf("%d", value))
 }
 
 // Returns nil if key not found
 func (env *Env) GetList(key string) []string {
 	sval := env.Get(key)
 	if sval == "" {
 		return nil
 	}
 	l := make([]string, 0, 1)
 	if err := json.Unmarshal([]byte(sval), &l); err != nil {
 		l = append(l, sval)
 	}
 	return l
 }
 
8fbdb7b5
 func (env *Env) GetSubEnv(key string) *Env {
 	sval := env.Get(key)
 	if sval == "" {
 		return nil
 	}
 	buf := bytes.NewBufferString(sval)
 	var sub Env
 	if err := sub.Decode(buf); err != nil {
 		return nil
 	}
 	return &sub
 }
 
 func (env *Env) SetSubEnv(key string, sub *Env) error {
 	var buf bytes.Buffer
 	if err := sub.Encode(&buf); err != nil {
 		return err
 	}
 	env.Set(key, string(buf.Bytes()))
 	return nil
 }
 
d5f5ecb6
 func (env *Env) GetJson(key string, iface interface{}) error {
930ec9f5
 	sval := env.Get(key)
 	if sval == "" {
 		return nil
 	}
d5f5ecb6
 	return json.Unmarshal([]byte(sval), iface)
930ec9f5
 }
 
a80c059b
 func (env *Env) SetJson(key string, value interface{}) error {
 	sval, err := json.Marshal(value)
 	if err != nil {
 		return err
 	}
 	env.Set(key, string(sval))
 	return nil
 }
 
 func (env *Env) SetList(key string, value []string) error {
 	return env.SetJson(key, value)
 }
 
 func (env *Env) Set(key, value string) {
 	*env = append(*env, key+"="+value)
 }
 
 func NewDecoder(src io.Reader) *Decoder {
 	return &Decoder{
 		json.NewDecoder(src),
 	}
 }
 
 type Decoder struct {
 	*json.Decoder
 }
 
 func (decoder *Decoder) Decode() (*Env, error) {
 	m := make(map[string]interface{})
 	if err := decoder.Decoder.Decode(&m); err != nil {
 		return nil, err
 	}
 	env := &Env{}
 	for key, value := range m {
 		env.SetAuto(key, value)
 	}
 	return env, nil
 }
 
 // DecodeEnv decodes `src` as a json dictionary, and adds
 // each decoded key-value pair to the environment.
 //
 // If `src` cannot be decoded as a json dictionary, an error
 // is returned.
 func (env *Env) Decode(src io.Reader) error {
 	m := make(map[string]interface{})
 	if err := json.NewDecoder(src).Decode(&m); err != nil {
 		return err
 	}
 	for k, v := range m {
 		env.SetAuto(k, v)
 	}
 	return nil
 }
 
 func (env *Env) SetAuto(k string, v interface{}) {
a7cd25b8
 	// Issue 7941 - if the value in the incoming JSON is null then treat it
 	// as if they never specified the property at all.
 	if v == nil {
 		return
 	}
 
a80c059b
 	// FIXME: we fix-convert float values to int, because
 	// encoding/json decodes integers to float64, but cannot encode them back.
 	// (See http://golang.org/src/pkg/encoding/json/decode.go#L46)
 	if fval, ok := v.(float64); ok {
85b93382
 		env.SetInt64(k, int64(fval))
a80c059b
 	} else if sval, ok := v.(string); ok {
 		env.Set(k, sval)
 	} else if val, err := json.Marshal(v); err == nil {
 		env.Set(k, string(val))
 	} else {
 		env.Set(k, fmt.Sprintf("%v", v))
 	}
 }
 
1d3d1c5d
 func changeFloats(v interface{}) interface{} {
 	switch v := v.(type) {
 	case float64:
 		return int(v)
 	case map[string]interface{}:
 		for key, val := range v {
 			v[key] = changeFloats(val)
 		}
 	case []interface{}:
 		for idx, val := range v {
 			v[idx] = changeFloats(val)
 		}
 	}
 	return v
 }
 
a80c059b
 func (env *Env) Encode(dst io.Writer) error {
95e6fd81
 	m := make(map[string]interface{})
 	for k, v := range env.Map() {
 		var val interface{}
 		if err := json.Unmarshal([]byte(v), &val); err == nil {
 			// FIXME: we fix-convert float values to int, because
 			// encoding/json decodes integers to float64, but cannot encode them back.
 			// (See http://golang.org/src/pkg/encoding/json/decode.go#L46)
1d3d1c5d
 			m[k] = changeFloats(val)
95e6fd81
 		} else {
 			m[k] = v
 		}
 	}
 	if err := json.NewEncoder(dst).Encode(&m); err != nil {
 		return err
 	}
 	return nil
a80c059b
 }
 
0cd30cf3
 func (env *Env) WriteTo(dst io.Writer) (int64, error) {
 	wc := utils.NewWriteCounter(dst)
 	err := env.Encode(wc)
 	return wc.Count, err
a80c059b
 }
 
 func (env *Env) Import(src interface{}) (err error) {
 	defer func() {
 		if err != nil {
 			err = fmt.Errorf("ImportEnv: %s", err)
 		}
 	}()
 	var buf bytes.Buffer
 	if err := json.NewEncoder(&buf).Encode(src); err != nil {
 		return err
 	}
 	if err := env.Decode(&buf); err != nil {
 		return err
 	}
 	return nil
 }
 
 func (env *Env) Map() map[string]string {
 	m := make(map[string]string)
 	for _, kv := range *env {
 		parts := strings.SplitN(kv, "=", 2)
 		m[parts[0]] = parts[1]
 	}
 	return m
 }
c7978c98
 
 // MultiMap returns a representation of env as a
 // map of string arrays, keyed by string.
 // This is the same structure as http headers for example,
 // which allow each key to have multiple values.
 func (env *Env) MultiMap() map[string][]string {
 	m := make(map[string][]string)
 	for _, kv := range *env {
 		parts := strings.SplitN(kv, "=", 2)
 		m[parts[0]] = append(m[parts[0]], parts[1])
 	}
 	return m
 }
 
 // InitMultiMap removes all values in env, then initializes
 // new values from the contents of m.
 func (env *Env) InitMultiMap(m map[string][]string) {
 	(*env) = make([]string, 0, len(m))
 	for k, vals := range m {
 		for _, v := range vals {
 			env.Set(k, v)
 		}
 	}
 }