Skip to content

Commit df5d7f2

Browse files
committed
Fix
1 parent 8db0568 commit df5d7f2

File tree

7 files changed

+288
-146
lines changed

7 files changed

+288
-146
lines changed

internal/core/context.go

+8
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,11 @@ func ReloadClient(ctx context.Context) error {
117117
meta.Client, err = createClient(meta.BuildInfo, "")
118118
return err
119119
}
120+
121+
func ExtractConfigPathFlag(ctx context.Context) string {
122+
return extractMeta(ctx).ConfigPathFlag
123+
}
124+
125+
func ExtractProfileFlag(ctx context.Context) string {
126+
return extractMeta(ctx).ProfileFlag
127+
}

internal/human/marshal.go

-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ type Marshaler interface {
2424
}
2525

2626
func Marshal(data interface{}, opt *MarshalOpt) (string, error) {
27-
// Debug infos
28-
logger.Debugf("marshalling type '%v'", reflect.TypeOf(data))
29-
3027
if opt == nil {
3128
opt = &MarshalOpt{}
3229
}

internal/namespaces/get_commands.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
baremetal "github.com/scaleway/scaleway-cli/internal/namespaces/baremetal/v1"
88
configNamespace "github.com/scaleway/scaleway-cli/internal/namespaces/config"
99
"github.com/scaleway/scaleway-cli/internal/namespaces/feedback"
10-
"github.com/scaleway/scaleway-cli/internal/namespaces/infos"
10+
"github.com/scaleway/scaleway-cli/internal/namespaces/info"
1111
initNamespace "github.com/scaleway/scaleway-cli/internal/namespaces/init"
1212
"github.com/scaleway/scaleway-cli/internal/namespaces/instance/v1"
1313
k8s "github.com/scaleway/scaleway-cli/internal/namespaces/k8s/v1"
@@ -36,6 +36,6 @@ func GetCommands() *core.Commands {
3636
commands.Merge(versionNamespace.GetCommands())
3737
commands.Merge(registry.GetCommands())
3838
commands.Merge(feedback.GetCommands())
39-
commands.Merge(infos.GetCommands())
39+
commands.Merge(info.GetCommands())
4040
return commands
4141
}

internal/namespaces/info/custom.go

+236
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
package info
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"reflect"
8+
9+
"github.com/scaleway/scaleway-cli/internal/core"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
11+
)
12+
13+
func GetCommands() *core.Commands {
14+
return core.NewCommands(
15+
infosRoot(),
16+
)
17+
}
18+
19+
type setting struct {
20+
Key string
21+
Value string
22+
Origin string
23+
}
24+
25+
func infosRoot() *core.Command {
26+
type infoArgs struct {
27+
ShowSecret bool
28+
}
29+
30+
return &core.Command{
31+
Short: `Get settings about current settings`,
32+
Namespace: "info",
33+
AllowAnonymousClient: true,
34+
ArgsType: reflect.TypeOf(infoArgs{}),
35+
ArgSpecs: core.ArgSpecs{
36+
{
37+
Name: "show-secret",
38+
Short: `Reveal secret`,
39+
Required: false,
40+
Default: core.DefaultValueSetter("false"),
41+
},
42+
},
43+
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
44+
req := argsI.(*infoArgs)
45+
config, _ := scw.LoadConfigFromPath(core.ExtractConfigPath(ctx))
46+
profileName := core.ExtractProfileName(ctx)
47+
return []*setting{
48+
configPath(ctx),
49+
defaultRegion(ctx, config, profileName),
50+
defaultZone(ctx, config, profileName),
51+
defaultOrganizationId(ctx, config, profileName),
52+
accessKey(ctx, config, profileName),
53+
secretKey(ctx, config, profileName, req.ShowSecret),
54+
profile(ctx),
55+
}, nil
56+
},
57+
}
58+
}
59+
60+
func configPath(ctx context.Context) *setting {
61+
setting := &setting{
62+
Key: "config_path",
63+
Value: core.ExtractConfigPath(ctx),
64+
}
65+
switch {
66+
case core.ExtractConfigPathFlag(ctx) != "":
67+
setting.Origin = "flag --config/-c"
68+
case os.Getenv(scw.ScwConfigPathEnv) != "":
69+
setting.Origin = fmt.Sprintf("env (%s)", scw.ScwConfigPathEnv)
70+
default:
71+
setting.Origin = "default"
72+
}
73+
return setting
74+
}
75+
76+
func defaultRegion(ctx context.Context, config *scw.Config, profileName string) *setting {
77+
setting := &setting{Key: "default_region"}
78+
client := core.ExtractClient(ctx)
79+
defaultRegion, exists := client.GetDefaultRegion()
80+
if exists {
81+
setting.Value = defaultRegion.String()
82+
}
83+
switch {
84+
// Environment variable check
85+
case os.Getenv(scw.ScwDefaultRegionEnv) != "":
86+
setting.Origin = fmt.Sprintf("env (%s)", scw.ScwDefaultRegionEnv)
87+
// There is no config file
88+
case config == nil:
89+
setting.Origin = "default"
90+
// Config file with profile name
91+
case config.Profiles[profileName] != nil && config.Profiles[profileName].DefaultRegion != nil:
92+
setting.Origin = fmt.Sprintf("profile (%s)", profileName)
93+
// Default config
94+
case config.Profile.DefaultRegion != nil:
95+
setting.Origin = "default profile"
96+
default:
97+
setting.Origin = "default"
98+
}
99+
return setting
100+
}
101+
102+
func defaultZone(ctx context.Context, config *scw.Config, profileName string) *setting {
103+
setting := &setting{Key: "default_zone"}
104+
client := core.ExtractClient(ctx)
105+
defaultZone, exists := client.GetDefaultZone()
106+
if exists {
107+
setting.Value = defaultZone.String()
108+
}
109+
switch {
110+
// Environment variable check
111+
case os.Getenv(scw.ScwDefaultZoneEnv) != "":
112+
setting.Origin = fmt.Sprintf("env (%s)", scw.ScwDefaultZoneEnv)
113+
// There is no config file
114+
case config == nil:
115+
setting.Origin = "default"
116+
// Config file with profile name
117+
case config.Profiles[profileName] != nil && config.Profiles[profileName].DefaultZone != nil:
118+
setting.Origin = fmt.Sprintf("profile (%s)", profileName)
119+
// Default config
120+
case config.Profile.DefaultZone != nil:
121+
setting.Origin = "default profile"
122+
default:
123+
setting.Origin = "default"
124+
}
125+
return setting
126+
}
127+
128+
func defaultOrganizationId(ctx context.Context, config *scw.Config, profileName string) *setting {
129+
setting := &setting{Key: "default_organization_id"}
130+
client := core.ExtractClient(ctx)
131+
defaultOrganizationId, exists := client.GetDefaultOrganizationID()
132+
if exists {
133+
setting.Value = defaultOrganizationId
134+
}
135+
switch {
136+
// Environment variable check
137+
case os.Getenv(scw.ScwDefaultOrganizationIDEnv) != "":
138+
setting.Origin = fmt.Sprintf("env (%s)", scw.ScwDefaultOrganizationIDEnv)
139+
// There is no config file
140+
case config == nil:
141+
setting.Origin = ""
142+
// Config file with profile name
143+
case config.Profiles[profileName] != nil && config.Profiles[profileName].DefaultOrganizationID != nil:
144+
setting.Origin = fmt.Sprintf("profile (%s)", profileName)
145+
// Default config
146+
case config.Profile.DefaultOrganizationID != nil:
147+
setting.Origin = "default profile"
148+
default:
149+
setting.Origin = "unknown"
150+
}
151+
return setting
152+
}
153+
154+
func accessKey(ctx context.Context, config *scw.Config, profileName string) *setting {
155+
setting := &setting{Key: "access_key"}
156+
client := core.ExtractClient(ctx)
157+
aK, exists := client.GetAccessKey()
158+
if exists {
159+
setting.Value = aK
160+
}
161+
switch {
162+
// Environment variable check
163+
case os.Getenv(scw.ScwAccessKeyEnv) != "":
164+
setting.Origin = fmt.Sprintf("env (%s)", scw.ScwAccessKeyEnv)
165+
// There is no config file
166+
case config == nil:
167+
setting.Origin = ""
168+
// Config file with profile name
169+
case config.Profiles[profileName] != nil && config.Profiles[profileName].AccessKey != nil:
170+
setting.Origin = fmt.Sprintf("profile (%s)", profileName)
171+
// Default config
172+
case config.Profile.AccessKey != nil:
173+
setting.Origin = "default profile"
174+
default:
175+
setting.Origin = "unknown"
176+
}
177+
return setting
178+
}
179+
180+
func hideSecretKey(k string) string {
181+
switch {
182+
case len(k) == 0:
183+
return ""
184+
case len(k) > 8:
185+
return k[0:8] + "-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
186+
default:
187+
return "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
188+
}
189+
}
190+
191+
func secretKey(ctx context.Context, config *scw.Config, profileName string, showSecret bool) *setting {
192+
setting := &setting{Key: "secret_key"}
193+
client := core.ExtractClient(ctx)
194+
sK, exists := client.GetSecretKey()
195+
if exists {
196+
if showSecret {
197+
setting.Value = sK
198+
} else {
199+
setting.Value = hideSecretKey(sK)
200+
}
201+
}
202+
switch {
203+
// Environment variable check
204+
case os.Getenv(scw.ScwSecretKeyEnv) != "":
205+
setting.Origin = fmt.Sprintf("env (%s)", scw.ScwSecretKeyEnv)
206+
// There is no config file
207+
case config == nil:
208+
setting.Origin = ""
209+
// Config file with profile name
210+
case config.Profiles[profileName] != nil && config.Profiles[profileName].SecretKey != nil:
211+
setting.Origin = fmt.Sprintf("profile (%s)", profileName)
212+
// Default config
213+
case config.Profile.SecretKey != nil:
214+
setting.Origin = "default profile"
215+
default:
216+
setting.Origin = "unknown"
217+
}
218+
219+
return setting
220+
}
221+
222+
func profile(ctx context.Context) *setting {
223+
setting := &setting{
224+
Key: "profile",
225+
Value: core.ExtractProfileName(ctx),
226+
}
227+
switch {
228+
case core.ExtractProfileFlag(ctx) != "":
229+
setting.Origin = "flag --profile/-p"
230+
case os.Getenv(scw.ScwActiveProfileEnv) != "":
231+
setting.Origin = fmt.Sprintf("env (%s)", scw.ScwActiveProfileEnv)
232+
default:
233+
setting.Origin = ""
234+
}
235+
return setting
236+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package info
2+
3+
import (
4+
"testing"
5+
6+
"github.com/scaleway/scaleway-cli/internal/core"
7+
"github.com/scaleway/scaleway-sdk-go/scw"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func Test_Info(t *testing.T) {
12+
client, err := scw.NewClient(
13+
scw.WithAuth(
14+
"SCWXXXXXXXXXXXXXXXXX",
15+
"11111111-1111-1111-1111-111111111111",
16+
),
17+
scw.WithDefaultOrganizationID("11111111-1111-1111-1111-111111111111"),
18+
scw.WithDefaultZone(scw.ZoneFrPar1),
19+
scw.WithDefaultRegion(scw.RegionFrPar),
20+
)
21+
require.NoError(t, err)
22+
23+
t.Run("Default", func(t *testing.T) {
24+
t.Run("Simple", core.Test(&core.TestConfig{
25+
Commands: GetCommands(),
26+
Cmd: "scw info",
27+
Check: core.TestCheckCombine(
28+
core.TestCheckGolden(),
29+
core.TestCheckExitCode(0),
30+
),
31+
Client: client,
32+
}))
33+
})
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
KEY VALUE ORIGIN
2+
config_path /Users/sieben/.config/scw/config.yaml default
3+
default_region fr-par default
4+
default_zone fr-par-1 default
5+
default_organization_id 11111111-1111-1111-1111-111111111111 -
6+
access_key SCWXXXXXXXXXXXXXXXXX -
7+
secret_key 11111111-xxxx-xxxx-xxxx-xxxxxxxxxxxx -
8+
profile - -

0 commit comments

Comments
 (0)