| 1 |
package command |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"io/ioutil" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"reflect" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"github.com/pkg/errors" |
| 13 |
"github.com/smallstep/cli/config" |
| 14 |
"github.com/smallstep/cli/usage" |
| 15 |
"github.com/urfave/cli" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
const IgnoreEnvVar = "STEP_IGNORE_ENV_VAR" |
| 21 |
|
| 22 |
var cmds []cli.Command |
| 23 |
var currentContext *cli.Context |
| 24 |
|
| 25 |
func init() { |
| 26 |
os.Unsetenv(IgnoreEnvVar) |
| 27 |
cmds = []cli.Command{ |
| 28 |
usage.HelpCommand(), |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
func Register(c cli.Command) { |
| 35 |
setEnvVar(&c) |
| 36 |
cmds = append(cmds, c) |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
func Retrieve() []cli.Command { |
| 41 |
return cmds |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func ActionFunc(fn cli.ActionFunc) cli.ActionFunc { |
| 46 |
return func(ctx *cli.Context) error { |
| 47 |
currentContext = ctx |
| 48 |
return fn(ctx) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func IsForce() bool { |
| 54 |
return currentContext != nil && currentContext.Bool("force") |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
func getConfigVars(ctx *cli.Context) error { |
| 62 |
configFile := ctx.GlobalString("config") |
| 63 |
if configFile == "" { |
| 64 |
configFile = filepath.Join(config.StepPath(), "config", "defaults.json") |
| 65 |
} |
| 66 |
|
| 67 |
b, err := ioutil.ReadFile(configFile) |
| 68 |
if err != nil { |
| 69 |
return nil |
| 70 |
} |
| 71 |
|
| 72 |
m := make(map[string]interface{}) |
| 73 |
if err := json.Unmarshal(b, &m); err != nil { |
| 74 |
return errors.Wrapf(err, "error parsing %s", configFile) |
| 75 |
} |
| 76 |
|
| 77 |
flags := make(map[string]cli.Flag) |
| 78 |
for _, f := range ctx.Command.Flags { |
| 79 |
name := strings.Split(f.GetName(), ",")[0] |
| 80 |
flags[name] = f |
| 81 |
} |
| 82 |
|
| 83 |
for _, name := range ctx.FlagNames() { |
| 84 |
if ctx.IsSet(name) { |
| 85 |
continue |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
if f, ok := flags[name]; ok { |
| 90 |
if getFlagEnvVar(f) == IgnoreEnvVar { |
| 91 |
continue |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if v, ok := m[name]; ok { |
| 96 |
ctx.Set(name, fmt.Sprintf("%v", v)) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return nil |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
func getEnvVar(name string) string { |
| 105 |
parts := strings.Split(name, ",") |
| 106 |
name = strings.TrimSpace(parts[0]) |
| 107 |
name = strings.Replace(name, "-", "_", -1) |
| 108 |
return "STEP_" + strings.ToUpper(name) |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
func getFlagEnvVar(f cli.Flag) string { |
| 113 |
v := reflect.ValueOf(f) |
| 114 |
if v.Kind() == reflect.Ptr { |
| 115 |
v = v.Elem() |
| 116 |
} |
| 117 |
if v.Kind() == reflect.Struct { |
| 118 |
envVar := v.FieldByName("EnvVar") |
| 119 |
if envVar.IsValid() { |
| 120 |
return envVar.String() |
| 121 |
} |
| 122 |
} |
| 123 |
return "" |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
func setEnvVar(c *cli.Command) { |
| 128 |
if c == nil { |
| 129 |
return |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
if c.Before == nil && c.Action != nil { |
| 134 |
c.Before = getConfigVars |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
for i := range c.Flags { |
| 139 |
envVar := getEnvVar(c.Flags[i].GetName()) |
| 140 |
switch f := c.Flags[i].(type) { |
| 141 |
case cli.BoolFlag: |
| 142 |
if f.EnvVar == "" { |
| 143 |
f.EnvVar = envVar |
| 144 |
c.Flags[i] = f |
| 145 |
} |
| 146 |
case cli.BoolTFlag: |
| 147 |
if f.EnvVar == "" { |
| 148 |
f.EnvVar = envVar |
| 149 |
c.Flags[i] = f |
| 150 |
} |
| 151 |
case cli.DurationFlag: |
| 152 |
if f.EnvVar == "" { |
| 153 |
f.EnvVar = envVar |
| 154 |
c.Flags[i] = f |
| 155 |
} |
| 156 |
case cli.Float64Flag: |
| 157 |
if f.EnvVar == "" { |
| 158 |
f.EnvVar = envVar |
| 159 |
c.Flags[i] = f |
| 160 |
} |
| 161 |
case cli.GenericFlag: |
| 162 |
if f.EnvVar == "" { |
| 163 |
f.EnvVar = envVar |
| 164 |
c.Flags[i] = f |
| 165 |
} |
| 166 |
case cli.Int64Flag: |
| 167 |
if f.EnvVar == "" { |
| 168 |
f.EnvVar = envVar |
| 169 |
c.Flags[i] = f |
| 170 |
} |
| 171 |
case cli.IntFlag: |
| 172 |
if f.EnvVar == "" { |
| 173 |
f.EnvVar = envVar |
| 174 |
c.Flags[i] = f |
| 175 |
} |
| 176 |
case cli.IntSliceFlag: |
| 177 |
if f.EnvVar == "" { |
| 178 |
f.EnvVar = envVar |
| 179 |
c.Flags[i] = f |
| 180 |
} |
| 181 |
case cli.Int64SliceFlag: |
| 182 |
if f.EnvVar == "" { |
| 183 |
f.EnvVar = envVar |
| 184 |
c.Flags[i] = f |
| 185 |
} |
| 186 |
case cli.StringFlag: |
| 187 |
if f.EnvVar == "" { |
| 188 |
f.EnvVar = envVar |
| 189 |
c.Flags[i] = f |
| 190 |
} |
| 191 |
case cli.StringSliceFlag: |
| 192 |
if f.EnvVar == "" { |
| 193 |
f.EnvVar = envVar |
| 194 |
c.Flags[i] = f |
| 195 |
} |
| 196 |
case cli.Uint64Flag: |
| 197 |
if f.EnvVar == "" { |
| 198 |
f.EnvVar = envVar |
| 199 |
c.Flags[i] = f |
| 200 |
} |
| 201 |
case cli.UintFlag: |
| 202 |
if f.EnvVar == "" { |
| 203 |
f.EnvVar = envVar |
| 204 |
c.Flags[i] = f |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
for i := range c.Subcommands { |
| 210 |
setEnvVar(&c.Subcommands[i]) |
| 211 |
} |
| 212 |
} |