1 // Copyright 2014 Docker authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the DOCKER-LICENSE file.
15 // Env represents a list of key-pair represented in the form KEY=VALUE.
18 // Get returns the string value of the given key.
19 func (env *Env) Get(key string) (value string) {
23 // Exists checks whether the given key is defined in the internal Env
25 func (env *Env) Exists(key string) bool {
26 _, exists := env.Map()[key]
30 // GetBool returns a boolean representation of the given key. The key is false
31 // whenever its value if 0, no, false, none or an empty string. Any other value
32 // will be interpreted as true.
33 func (env *Env) GetBool(key string) (value bool) {
34 s := strings.ToLower(strings.Trim(env.Get(key), " \t"))
35 if s == "" || s == "0" || s == "no" || s == "false" || s == "none" {
41 // SetBool defines a boolean value to the given key.
42 func (env *Env) SetBool(key string, value bool) {
50 // GetInt returns the value of the provided key, converted to int.
52 // It the value cannot be represented as an integer, it returns -1.
53 func (env *Env) GetInt(key string) int {
54 return int(env.GetInt64(key))
57 // SetInt defines an integer value to the given key.
58 func (env *Env) SetInt(key string, value int) {
59 env.Set(key, strconv.Itoa(value))
62 // GetInt64 returns the value of the provided key, converted to int64.
64 // It the value cannot be represented as an integer, it returns -1.
65 func (env *Env) GetInt64(key string) int64 {
66 s := strings.Trim(env.Get(key), " \t")
67 val, err := strconv.ParseInt(s, 10, 64)
74 // SetInt64 defines an integer (64-bit wide) value to the given key.
75 func (env *Env) SetInt64(key string, value int64) {
76 env.Set(key, strconv.FormatInt(value, 10))
79 // GetJSON unmarshals the value of the provided key in the provided iface.
81 // iface is a value that can be provided to the json.Unmarshal function.
82 func (env *Env) GetJSON(key string, iface interface{}) error {
87 return json.Unmarshal([]byte(sval), iface)
90 // SetJSON marshals the given value to JSON format and stores it using the
92 func (env *Env) SetJSON(key string, value interface{}) error {
93 sval, err := json.Marshal(value)
97 env.Set(key, string(sval))
101 // GetList returns a list of strings matching the provided key. It handles the
102 // list as a JSON representation of a list of strings.
104 // If the given key matches to a single string, it will return a list
105 // containing only the value that matches the key.
106 func (env *Env) GetList(key string) []string {
112 if err := json.Unmarshal([]byte(sval), &l); err != nil {
118 // SetList stores the given list in the provided key, after serializing it to
120 func (env *Env) SetList(key string, value []string) error {
121 return env.SetJSON(key, value)
124 // Set defines the value of a key to the given string.
125 func (env *Env) Set(key, value string) {
126 *env = append(*env, key+"="+value)
129 // Decode decodes `src` as a json dictionary, and adds each decoded key-value
130 // pair to the environment.
132 // If `src` cannot be decoded as a json dictionary, an error is returned.
133 func (env *Env) Decode(src io.Reader) error {
134 m := make(map[string]interface{})
135 if err := json.NewDecoder(src).Decode(&m); err != nil {
138 for k, v := range m {
144 // SetAuto will try to define the Set* method to call based on the given value.
145 func (env *Env) SetAuto(key string, value interface{}) {
146 if fval, ok := value.(float64); ok {
147 env.SetInt64(key, int64(fval))
148 } else if sval, ok := value.(string); ok {
150 } else if val, err := json.Marshal(value); err == nil {
151 env.Set(key, string(val))
153 env.Set(key, fmt.Sprintf("%v", value))
157 // Map returns the map representation of the env.
158 func (env *Env) Map() map[string]string {
162 m := make(map[string]string)
163 for _, kv := range *env {
164 parts := strings.SplitN(kv, "=", 2)
165 m[parts[0]] = parts[1]