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.
4
5package docker
6
7import (
8	"encoding/json"
9	"fmt"
10	"io"
11	"strconv"
12	"strings"
13)
14
15// Env represents a list of key-pair represented in the form KEY=VALUE.
16type Env []string
17
18// Get returns the string value of the given key.
19func (env *Env) Get(key string) (value string) {
20	return env.Map()[key]
21}
22
23// Exists checks whether the given key is defined in the internal Env
24// representation.
25func (env *Env) Exists(key string) bool {
26	_, exists := env.Map()[key]
27	return exists
28}
29
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.
33func (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" {
36		return false
37	}
38	return true
39}
40
41// SetBool defines a boolean value to the given key.
42func (env *Env) SetBool(key string, value bool) {
43	if value {
44		env.Set(key, "1")
45	} else {
46		env.Set(key, "0")
47	}
48}
49
50// GetInt returns the value of the provided key, converted to int.
51//
52// It the value cannot be represented as an integer, it returns -1.
53func (env *Env) GetInt(key string) int {
54	return int(env.GetInt64(key))
55}
56
57// SetInt defines an integer value to the given key.
58func (env *Env) SetInt(key string, value int) {
59	env.Set(key, strconv.Itoa(value))
60}
61
62// GetInt64 returns the value of the provided key, converted to int64.
63//
64// It the value cannot be represented as an integer, it returns -1.
65func (env *Env) GetInt64(key string) int64 {
66	s := strings.Trim(env.Get(key), " \t")
67	val, err := strconv.ParseInt(s, 10, 64)
68	if err != nil {
69		return -1
70	}
71	return val
72}
73
74// SetInt64 defines an integer (64-bit wide) value to the given key.
75func (env *Env) SetInt64(key string, value int64) {
76	env.Set(key, strconv.FormatInt(value, 10))
77}
78
79// GetJSON unmarshals the value of the provided key in the provided iface.
80//
81// iface is a value that can be provided to the json.Unmarshal function.
82func (env *Env) GetJSON(key string, iface interface{}) error {
83	sval := env.Get(key)
84	if sval == "" {
85		return nil
86	}
87	return json.Unmarshal([]byte(sval), iface)
88}
89
90// SetJSON marshals the given value to JSON format and stores it using the
91// provided key.
92func (env *Env) SetJSON(key string, value interface{}) error {
93	sval, err := json.Marshal(value)
94	if err != nil {
95		return err
96	}
97	env.Set(key, string(sval))
98	return nil
99}
100
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.
103//
104// If the given key matches to a single string, it will return a list
105// containing only the value that matches the key.
106func (env *Env) GetList(key string) []string {
107	sval := env.Get(key)
108	if sval == "" {
109		return nil
110	}
111	var l []string
112	if err := json.Unmarshal([]byte(sval), &l); err != nil {
113		l = append(l, sval)
114	}
115	return l
116}
117
118// SetList stores the given list in the provided key, after serializing it to
119// JSON format.
120func (env *Env) SetList(key string, value []string) error {
121	return env.SetJSON(key, value)
122}
123
124// Set defines the value of a key to the given string.
125func (env *Env) Set(key, value string) {
126	*env = append(*env, key+"="+value)
127}
128
129// Decode decodes `src` as a json dictionary, and adds each decoded key-value
130// pair to the environment.
131//
132// If `src` cannot be decoded as a json dictionary, an error is returned.
133func (env *Env) Decode(src io.Reader) error {
134	m := make(map[string]interface{})
135	if err := json.NewDecoder(src).Decode(&m); err != nil {
136		return err
137	}
138	for k, v := range m {
139		env.SetAuto(k, v)
140	}
141	return nil
142}
143
144// SetAuto will try to define the Set* method to call based on the given value.
145func (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 {
149		env.Set(key, sval)
150	} else if val, err := json.Marshal(value); err == nil {
151		env.Set(key, string(val))
152	} else {
153		env.Set(key, fmt.Sprintf("%v", value))
154	}
155}
156
157// Map returns the map representation of the env.
158func (env *Env) Map() map[string]string {
159	if env == nil || len(*env) == 0 {
160		return nil
161	}
162	m := make(map[string]string)
163	for _, kv := range *env {
164		parts := strings.SplitN(kv, "=", 2)
165		if len(parts) == 1 {
166			m[parts[0]] = ""
167		} else {
168			m[parts[0]] = parts[1]
169		}
170	}
171	return m
172}
173