Skip to content

Commit b3c7c43

Browse files
authored
Enable environment variable hints for --token flag (#945)
Moved envVars to env.Vars Add test case for env.Vars Add slices package for slices.Contains Actually test something sensible Check error value from Setenv since windows does not set values Test with common variables instead Mark tests on other platforms as skip Document Vars method
1 parent 70d4c68 commit b3c7c43

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ require (
5656
github.com/rogpeppe/go-internal v1.8.0 // indirect
5757
github.com/ulikunitz/xz v0.5.10 // indirect
5858
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
59+
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
5960
golang.org/x/net v0.10.0 // indirect
6061
golang.org/x/text v0.9.0 // indirect
6162
gopkg.in/yaml.v2 v2.4.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofm
125125
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
126126
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
127127
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
128+
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
129+
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
128130
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
129131
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
130132
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

pkg/app/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func Run(opts RunOpts) error {
9191
app.Flag("non-interactive", "Do not prompt for user input - suitable for CI processes. Equivalent to --accept-defaults and --auto-yes").Short('i').BoolVar(&g.Flags.NonInteractive)
9292
app.Flag("profile", "Switch account profile for single command execution (see also: 'fastly profile switch')").Short('o').StringVar(&g.Flags.Profile)
9393
app.Flag("quiet", "Silence all output except direct command output. This won't prevent interactive prompts (see: --accept-defaults, --auto-yes, --non-interactive)").Short('q').BoolVar(&g.Flags.Quiet)
94-
app.Flag("token", tokenHelp).Short('t').StringVar(&g.Flags.Token)
94+
app.Flag("token", tokenHelp).HintAction(env.Vars).Short('t').StringVar(&g.Flags.Token)
9595
app.Flag("verbose", "Verbose logging").Short('v').BoolVar(&g.Flags.Verbose)
9696

9797
commands := defineCommands(app, &g, md, opts)

pkg/env/env.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package env
22

3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/fastly/cli/pkg/runtime"
9+
)
10+
311
const (
412
// Token is the env var we look in for the Fastly API token.
513
// gosec flagged this:
@@ -17,3 +25,30 @@ const (
1725
// CustomerID is the env var we look in for a Customer ID.
1826
CustomerID = "FASTLY_CUSTOMER_ID"
1927
)
28+
29+
// Vars returns a slice of environment variables appropriate to platform.
30+
// *nix: $HOME, $USER, ...
31+
// Windows: %HOME%, %USER%, ...
32+
func Vars() []string {
33+
vars := []string{}
34+
for _, e := range os.Environ() {
35+
pair := strings.SplitN(e, "=", 2)
36+
vars = append(vars, toVar(pair[0]))
37+
}
38+
return vars
39+
}
40+
41+
func toVar(v string) string {
42+
if runtime.Windows {
43+
return toWin(v)
44+
}
45+
return toNix(v)
46+
}
47+
48+
func toNix(v string) string {
49+
return fmt.Sprintf("\\$%s", v)
50+
}
51+
52+
func toWin(v string) string {
53+
return fmt.Sprintf("%%%s%%", v)
54+
}

pkg/env/env_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package env
2+
3+
import (
4+
"runtime"
5+
"testing"
6+
7+
"golang.org/x/exp/slices"
8+
)
9+
10+
func TestVars(t *testing.T) {
11+
tcs := []struct {
12+
os string
13+
vars map[string]string
14+
expected []string
15+
}{
16+
{
17+
os: "windows",
18+
expected: []string{"%HOME%", "%PATH%"},
19+
},
20+
{
21+
os: "darwin",
22+
expected: []string{"\\$HOME", "\\$PATH"},
23+
},
24+
{
25+
os: "linux",
26+
expected: []string{"\\$HOME", "\\$PATH"},
27+
},
28+
}
29+
for _, tc := range tcs {
30+
t.Run(tc.os, func(t *testing.T) {
31+
vars := Vars()
32+
if runtime.GOOS == tc.os {
33+
for _, v := range tc.expected {
34+
if !slices.Contains(vars, v) {
35+
t.Errorf("expected %s in %v", v, vars)
36+
}
37+
}
38+
} else {
39+
t.Skip()
40+
}
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)