env.go 4.4 KB

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