podman
172 строки · 4.4 Кб
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) {
20return 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]
27return 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) {
34s := strings.ToLower(strings.Trim(env.Get(key), " \t"))
35if s == "" || s == "0" || s == "no" || s == "false" || s == "none" {
36return false
37}
38return true
39}
40
41// SetBool defines a boolean value to the given key.
42func (env *Env) SetBool(key string, value bool) {
43if value {
44env.Set(key, "1")
45} else {
46env.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 {
54return int(env.GetInt64(key))
55}
56
57// SetInt defines an integer value to the given key.
58func (env *Env) SetInt(key string, value int) {
59env.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 {
66s := strings.Trim(env.Get(key), " \t")
67val, err := strconv.ParseInt(s, 10, 64)
68if err != nil {
69return -1
70}
71return val
72}
73
74// SetInt64 defines an integer (64-bit wide) value to the given key.
75func (env *Env) SetInt64(key string, value int64) {
76env.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 any) error {
83sval := env.Get(key)
84if sval == "" {
85return nil
86}
87return 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 any) error {
93sval, err := json.Marshal(value)
94if err != nil {
95return err
96}
97env.Set(key, string(sval))
98return 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 {
107sval := env.Get(key)
108if sval == "" {
109return nil
110}
111var l []string
112if err := json.Unmarshal([]byte(sval), &l); err != nil {
113l = append(l, sval)
114}
115return 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 {
121return 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 {
134m := make(map[string]any)
135if err := json.NewDecoder(src).Decode(&m); err != nil {
136return err
137}
138for k, v := range m {
139env.SetAuto(k, v)
140}
141return 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 any) {
146if fval, ok := value.(float64); ok {
147env.SetInt64(key, int64(fval))
148} else if sval, ok := value.(string); ok {
149env.Set(key, sval)
150} else if val, err := json.Marshal(value); err == nil {
151env.Set(key, string(val))
152} else {
153env.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 {
159if env == nil || len(*env) == 0 {
160return nil
161}
162m := make(map[string]string)
163for _, kv := range *env {
164parts := strings.SplitN(kv, "=", 2)
165if len(parts) == 1 {
166m[parts[0]] = ""
167} else {
168m[parts[0]] = parts[1]
169}
170}
171return m
172}
173