Skip to content

Commit 3732d6c

Browse files
authored
Sensuctl env vars (#4393)
* sensuctl environment variables Signed-off-by: Francis Guimond <francis@sensu.io>
1 parent 356a623 commit 3732d6c

File tree

41 files changed

+242
-137
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+242
-137
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ agent processed a particular event.
2525
- Added API key support for admin user at cluster init time.
2626
- Added `sensu_go_agentd_event_bytes` & `sensu_go_store_event_bytes` summary
2727
metrics to the `/metrics` endpoint.
28+
- Added support for environment variable arguments in `sensuctl`.
2829

2930
### Changed
3031
- When deleting resource with sensuctl, the resource type will now be displayed

cli/cli.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/sensu/sensu-go/cli/client"
99
"github.com/sensu/sensu-go/cli/client/config"
1010
"github.com/sensu/sensu-go/cli/client/config/basic"
11+
"github.com/sensu/sensu-go/cli/commands/helpers"
1112
"github.com/sirupsen/logrus"
1213
"github.com/spf13/pflag"
1314
)
@@ -29,8 +30,13 @@ type SensuCli struct {
2930

3031
// New SensuCLI given persistent flags from command
3132
func New(flags *pflag.FlagSet) *SensuCli {
32-
conf := basic.Load(flags)
33-
client := client.New(conf)
33+
v, err := helpers.InitViper(flags)
34+
if err != nil {
35+
return nil
36+
}
37+
38+
conf := basic.Load(flags, v)
39+
cliClient := client.New(conf)
3440
logger := logrus.WithFields(logrus.Fields{
3541
"component": "cli-client",
3642
})
@@ -49,10 +55,10 @@ func New(flags *pflag.FlagSet) *SensuCli {
4955
tlsConfig.InsecureSkipVerify = conf.InsecureSkipTLSVerify()
5056
tlsConfig.CipherSuites = corev2.DefaultCipherSuites
5157

52-
client.SetTLSClientConfig(&tlsConfig)
58+
cliClient.SetTLSClientConfig(&tlsConfig)
5359

5460
return &SensuCli{
55-
Client: client,
61+
Client: cliClient,
5662
Config: conf,
5763
Logger: logger,
5864
InFile: os.Stdin,

cli/client/config/basic/basic.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/sensu/sensu-go/types"
1212
"github.com/sirupsen/logrus"
1313
"github.com/spf13/pflag"
14+
"github.com/spf13/viper"
1415
)
1516

1617
const (
@@ -46,11 +47,11 @@ type Profile struct {
4647
}
4748

4849
// Load imports the CLI configuration and returns an initialized Config struct
49-
func Load(flags *pflag.FlagSet) *Config {
50+
func Load(flags *pflag.FlagSet, v *viper.Viper) *Config {
5051
conf := &Config{}
5152

5253
// Retrieve the path of the configuration directory
53-
if flags != nil {
54+
if flags != nil && v != nil {
5455
// When Load() is called, some sub-command local flags, such as
5556
// --format, are not registered yet and this leads to "unknown flags"
5657
// errors being returned by cobra. Such an error can throw off the cobra
@@ -68,7 +69,7 @@ func Load(flags *pflag.FlagSet) *Config {
6869
flags.ParseErrorsWhitelist = pflag.ParseErrorsWhitelist{UnknownFlags: true}
6970
_ = flags.Parse(os.Args[1:])
7071

71-
if value, err := flags.GetString("config-dir"); err == nil && value != "" {
72+
if value := v.GetString("config-dir"); value != "" {
7273
conf.path = value
7374
}
7475
}
@@ -83,42 +84,37 @@ func Load(flags *pflag.FlagSet) *Config {
8384
logger.Debug(err)
8485
}
8586

86-
if flags != nil {
87+
if v != nil {
8788
// Override namespace
88-
if value := helpers.GetChangedStringValueFlag("namespace", flags); value != "" {
89+
if value := helpers.GetChangedStringValueEnv("namespace", v); value != "" {
8990
conf.Profile.Namespace = value
9091
}
9192
}
9293

9394
// Load the flags config
94-
conf.flags(flags)
95+
conf.flags(v)
9596

9697
return conf
9798
}
9899

99-
func (c *Config) flags(flags *pflag.FlagSet) {
100-
if flags == nil {
100+
func (c *Config) flags(v *viper.Viper) {
101+
if v == nil {
101102
return
102103
}
103104

104-
// Set the API URL
105-
if value, err := flags.GetString("api-url"); err == nil && value != "" {
105+
if value := v.GetString("api-url"); value != "" {
106106
c.Cluster.APIUrl = value
107107
}
108-
109-
if value, err := flags.GetBool("insecure-skip-tls-verify"); err == nil && value {
108+
if value := v.GetString("api-key"); value != "" {
109+
c.Cluster.APIKey = value
110+
}
111+
if value := v.GetBool("insecure-skip-tls-verify"); value {
110112
c.Cluster.InsecureSkipTLSVerify = value
111113
}
112-
113-
if value, err := flags.GetString("trusted-ca-file"); err == nil && value != "" {
114+
if value := v.GetString("trusted-ca-file"); value != "" {
114115
c.Cluster.TrustedCAFile = value
115116
}
116-
117-
if value, err := flags.GetString("api-key"); err == nil && value != "" {
118-
c.Cluster.APIKey = value
119-
}
120-
121-
if value, err := flags.GetString("timeout"); err == nil && value != "" {
117+
if value := v.GetString("timeout"); value != "" {
122118
duration, err := time.ParseDuration(value)
123119
if err == nil {
124120
c.Cluster.Timeout = duration

cli/client/config/basic/basic_test.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,36 @@ import (
77
"path/filepath"
88
"testing"
99

10+
"github.com/sensu/sensu-go/cli/commands/helpers"
1011
"github.com/spf13/pflag"
12+
"github.com/spf13/viper"
1113
"github.com/stretchr/testify/assert"
1214
"github.com/stretchr/testify/require"
1315
)
1416

1517
func TestFlags(t *testing.T) {
1618
flags := pflag.NewFlagSet("api-url", pflag.ContinueOnError)
1719
flags.String("api-url", "foo", "")
20+
v := viper.New()
21+
_ = v.BindPFlags(flags)
1822

19-
config := Load(flags)
23+
config := Load(flags, v)
2024
assert.NotNil(t, config)
2125

2226
assert.Equal(t, "foo", config.APIUrl())
2327
}
2428

29+
func TestEnv(t *testing.T) {
30+
flags := pflag.NewFlagSet("api-url", pflag.ContinueOnError)
31+
_ = os.Setenv("SENSU_API_URL", "foo_env")
32+
v, _ := helpers.InitViper(flags)
33+
34+
config := Load(flags, v)
35+
assert.NotNil(t, config)
36+
37+
assert.Equal(t, "foo_env", config.APIUrl())
38+
}
39+
2540
func TestLoad(t *testing.T) {
2641
// Create a dummy directory for testing
2742
dir, _ := ioutil.TempDir("", "sensu")
@@ -32,6 +47,8 @@ func TestLoad(t *testing.T) {
3247
// Set flags
3348
flags := pflag.NewFlagSet("config-dir", pflag.ContinueOnError)
3449
flags.String("config-dir", dir, "")
50+
v := viper.New()
51+
_ = v.BindPFlags(flags)
3552

3653
// Create a dummy cluster file
3754
cluster := &Cluster{APIUrl: "localhost"}
@@ -45,7 +62,7 @@ func TestLoad(t *testing.T) {
4562
profilePath := filepath.Join(dir, profileFilename)
4663
_ = ioutil.WriteFile(profilePath, profileBytes, 0644)
4764

48-
config := Load(flags)
65+
config := Load(flags, v)
4966
assert.NotNil(t, config)
5067
assert.Equal(t, profile.Format, config.Format())
5168
assert.Equal(t, cluster.APIUrl, config.APIUrl())
@@ -55,8 +72,10 @@ func TestLoadMissingFiles(t *testing.T) {
5572
// Set flags
5673
flags := pflag.NewFlagSet("config-dir", pflag.ContinueOnError)
5774
flags.String("config-dir", "/tmp/sensu", "")
75+
v := viper.New()
76+
_ = v.BindPFlags(flags)
5877

59-
config := Load(flags)
78+
config := Load(flags, v)
6079
assert.NotNil(t, config)
6180
}
6281

cli/client/config/basic/writer_test.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/sensu/sensu-go/types"
1212
"github.com/spf13/pflag"
13+
"github.com/spf13/viper"
1314
"github.com/stretchr/testify/assert"
1415
"github.com/stretchr/testify/require"
1516
)
@@ -38,8 +39,10 @@ func TestSaveAPIUrl(t *testing.T) {
3839
// Set flags
3940
flags := pflag.NewFlagSet("config-dir", pflag.ContinueOnError)
4041
flags.String("config-dir", dir, "")
42+
v := viper.New()
43+
_ = v.BindPFlags(flags)
4144

42-
config := Load(flags)
45+
config := Load(flags, v)
4346

4447
url := "http://127.0.0.1:8080"
4548
require.NoError(t, config.SaveAPIUrl(url))
@@ -53,8 +56,10 @@ func TestSaveFormat(t *testing.T) {
5356
// Set flags
5457
flags := pflag.NewFlagSet("config-dir", pflag.ContinueOnError)
5558
flags.String("config-dir", dir, "")
59+
v := viper.New()
60+
_ = v.BindPFlags(flags)
5661

57-
config := Load(flags)
62+
config := Load(flags, v)
5863

5964
format := "json"
6065
require.NoError(t, config.SaveFormat(format))
@@ -68,8 +73,10 @@ func TestSaveNamespace(t *testing.T) {
6873
// Set flags
6974
flags := pflag.NewFlagSet("config-dir", pflag.ContinueOnError)
7075
flags.String("config-dir", dir, "")
76+
v := viper.New()
77+
_ = v.BindPFlags(flags)
7178

72-
config := Load(flags)
79+
config := Load(flags, v)
7380

7481
namespace := "json"
7582
require.NoError(t, config.SaveNamespace(namespace))
@@ -83,8 +90,10 @@ func TestSaveTimeout(t *testing.T) {
8390
// Set flags
8491
flags := pflag.NewFlagSet("config-dir", pflag.ContinueOnError)
8592
flags.String("config-dir", dir, "")
93+
v := viper.New()
94+
_ = v.BindPFlags(flags)
8695

87-
config := Load(flags)
96+
config := Load(flags, v)
8897

8998
timeout := 30 * time.Second
9099
require.NoError(t, config.SaveTimeout(timeout))
@@ -98,8 +107,10 @@ func TestSaveTokens(t *testing.T) {
98107
// Set flags
99108
flags := pflag.NewFlagSet("config-dir", pflag.ContinueOnError)
100109
flags.String("config-dir", dir, "")
110+
v := viper.New()
111+
_ = v.BindPFlags(flags)
101112

102-
config := Load(flags)
113+
config := Load(flags, v)
103114

104115
tokens := &types.Tokens{Access: "foo"}
105116
_ = config.SaveTokens(tokens)
@@ -114,25 +125,29 @@ func TestSaveTokensWithAPIUrlFlag(t *testing.T) {
114125
// Set flags
115126
flags := pflag.NewFlagSet("api-url", pflag.ContinueOnError)
116127
flags.String("api-url", "setFromFlag", "")
128+
v := viper.New()
129+
_ = v.BindPFlags(flags)
117130

118131
dirFlag := pflag.NewFlagSet("config-dir", pflag.ContinueOnError)
119132
dirFlag.String("config-dir", dir, "")
120133
flags.AddFlagSet(dirFlag)
134+
dirViper := viper.New()
135+
_ = dirViper.BindPFlags(dirFlag)
121136

122137
// Create a dummy cluster file
123138
cluster := &Cluster{APIUrl: "setFromFile"}
124139
clusterBytes, _ := json.Marshal(cluster)
125140
clusterPath := filepath.Join(dir, clusterFilename)
126141
require.NoError(t, ioutil.WriteFile(clusterPath, clusterBytes, 0644))
127142

128-
config := Load(flags)
143+
config := Load(flags, v)
129144

130145
tokens := &types.Tokens{Access: "foo"}
131146
require.NoError(t, config.SaveTokens(tokens))
132147
assert.Equal(t, tokens, config.Tokens())
133148

134-
// Make sure we didn't override the orginal API URL
135-
configFile := Load(dirFlag)
149+
// Make sure we didn't override the original API URL
150+
configFile := Load(flags, dirViper)
136151
assert.Equal(t, "setFromFile", configFile.APIUrl())
137152
}
138153

@@ -143,14 +158,16 @@ func TestWrite(t *testing.T) {
143158
// Set flags
144159
flags := pflag.NewFlagSet("config-dir", pflag.ContinueOnError)
145160
flags.String("config-dir", dir, "")
161+
v := viper.New()
162+
_ = v.BindPFlags(flags)
146163

147-
config := Load(flags)
164+
config := Load(flags, v)
148165

149166
url := "http://127.0.0.1:8080"
150167
require.NoError(t, config.SaveAPIUrl(url))
151168
assert.Equal(t, url, config.APIUrl())
152169

153170
// Reload the config files to make sure the changes were saved
154-
config2 := Load(flags)
171+
config2 := Load(flags, v)
155172
assert.Equal(t, config.APIUrl(), config2.APIUrl())
156173
}

cli/commands/apikey/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func InfoCommand(cli *cli.SensuCli) *cobra.Command {
3535
return err
3636
}
3737

38-
flag := helpers.GetChangedStringValueFlag("format", cmd.Flags())
38+
flag := helpers.GetChangedStringValueViper("format", cmd.Flags())
3939
format := cli.Config.Format()
4040
return helpers.PrintFormatted(flag, format, apikey, cmd.OutOrStdout(), printToList)
4141
},

cli/commands/asset/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func InfoCommand(cli *cli.SensuCli) *cobra.Command {
3434
}
3535

3636
// Determine the format to use to output the data
37-
flag := helpers.GetChangedStringValueFlag("format", cmd.Flags())
37+
flag := helpers.GetChangedStringValueViper("format", cmd.Flags())
3838
format := cli.Config.Format()
3939
return helpers.PrintFormatted(flag, format, r, cmd.OutOrStdout(), printToList)
4040
},

cli/commands/asset/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func ListCommand(cli *cli.SensuCli) *cobra.Command {
5050

5151
// Determine the user's preferred format
5252
var format string
53-
if format = helpers.GetChangedStringValueFlag("format", cmd.Flags()); format == "" {
53+
if format = helpers.GetChangedStringValueViper("format", cmd.Flags()); format == "" {
5454
format = cli.Config.Format()
5555
}
5656

cli/commands/asset/list_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,7 @@ func TestListBuildsWithTabular(t *testing.T) {
205205
func TestListBuildsWithJSON(t *testing.T) {
206206
assert := assert.New(t)
207207

208-
cli := test.NewMockCLI()
209-
config := cli.Config.(*client.MockConfig)
210-
config.On("Format").Return("json")
208+
cli := test.NewCLI()
211209

212210
asset := *corev2.FixtureAsset("builds")
213211
asset.Builds = []*corev2.AssetBuild{

cli/commands/check/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func InfoCommand(cli *cli.SensuCli) *cobra.Command {
3535
}
3636

3737
// Determine the format to use to output the data
38-
flag := helpers.GetChangedStringValueFlag("format", cmd.Flags())
38+
flag := helpers.GetChangedStringValueViper("format", cmd.Flags())
3939
format := cli.Config.Format()
4040
return helpers.PrintFormatted(flag, format, r, cmd.OutOrStdout(), printToList)
4141
},

0 commit comments

Comments
 (0)