/
flag_set.go
113 lines (95 loc) · 2.65 KB
/
flag_set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2023 Searis AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package automationcli
import (
"encoding"
"flag"
"fmt"
"os"
"strings"
)
type flagSetAdder struct {
envPrefix string
set *flag.FlagSet
}
func (set flagSetAdder) StringVar(target *string, name string, fallback string, usage string) {
k := envKey(set.envPrefix, name)
usage = fmt.Sprintf("%s (env: %s)", usage, k)
if v := os.Getenv(k); v != "" {
fallback = v
}
set.set.StringVar(target, name, fallback, usage)
}
func (set flagSetAdder) StringSliceVar(target *[]string, name string, fallback []string, usage string) {
k := envKey(set.envPrefix, name)
usage = fmt.Sprintf("%s (env: %s)", usage, k)
if v := os.Getenv(k); v != "" {
fallback = strings.Split(v, ",")
}
*target = fallback
set.set.Var(stringSlice{target: target}, name, usage)
}
func (set flagSetAdder) BoolVar(target *bool, name string, fallback bool, usage string) {
k := envKey(set.envPrefix, name)
usage = fmt.Sprintf("%s (env: %s)", usage, k)
if v := strings.ToLower(os.Getenv(k)); v != "" {
fallback = (v == "true" || v == "1")
}
set.set.BoolVar(target, name, fallback, usage)
}
func envKey(prefix, name string) string {
return prefix + strings.ReplaceAll(strings.ToUpper(name), "-", "_")
}
type stringSlice struct {
target *[]string
}
var _ flag.Value = stringSlice{}
func (tf stringSlice) String() string {
if tf.target == nil {
return ""
}
return strings.Join(*tf.target, ",")
}
func (tf stringSlice) Set(v string) error {
if v == "" {
return nil
}
*tf.target = strings.Split(v, ",")
return nil
}
// Password helps prevent a string value from being exposed in logs or
// marshalled as text or JSON.
type Password struct {
value string
}
var (
_ encoding.TextMarshaler = Password{}
_ encoding.TextUnmarshaler = &Password{}
)
func NewPassword(value string) Password {
return Password{value: value}
}
func (p Password) String() string {
return "****"
}
func (p Password) GoString() string {
return `NewPassword("****")`
}
func (p *Password) UnmarshalText(data []byte) error {
p.value = string(data)
return nil
}
func (p Password) MarshalText() ([]byte, error) {
return []byte("****"), nil
}