Skip to content

Commit bfacf9f

Browse files
fix: track ssl flag in profile
1 parent 00bfe8b commit bfacf9f

4 files changed

Lines changed: 88 additions & 3 deletions

File tree

internal/cmd/profile.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ var profileAddCmd = &cobra.Command{
9393
return fmt.Errorf("profile %q already exists", name)
9494
}
9595

96+
var ssl *bool
97+
if flag := cmd.Flags().Lookup("ssl"); flag != nil && flag.Changed {
98+
val, _ := cmd.Flags().GetBool("ssl")
99+
ssl = &val
100+
}
101+
96102
var verifySSL *bool
97103
if flag := cmd.Flags().Lookup("verify-ssl"); flag != nil && flag.Changed {
98104
val, _ := cmd.Flags().GetBool("verify-ssl")
@@ -106,6 +112,7 @@ var profileAddCmd = &cobra.Command{
106112
ClientSecret: profClientSecret,
107113
AccessToken: profToken,
108114
RefreshToken: profRefreshToken,
115+
SSL: ssl,
109116
VerifySSL: verifySSL,
110117
}
111118

internal/cmd/profile_test.go

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,20 @@ func TestProfileCommands(t *testing.T) {
9595
t.Errorf("expected '* test-prof (test.looker.com:19999)', got %q", out)
9696
}
9797

98-
// 4. Add another profile
99-
_, err = executeCommand("profile", "add", "test-prof2", "--host", "test2.looker.com", "--port", "20000")
98+
// 4. Add another profile with --ssl=false
99+
_, err = executeCommand("profile", "add", "test-prof2", "--host", "test2.looker.com", "--port", "20000", "--ssl=false")
100100
if err != nil {
101101
t.Fatalf("profile add failed: %v", err)
102102
}
103103

104+
cfgCheck, err := config.Load()
105+
if err != nil {
106+
t.Fatalf("failed to load config: %v", err)
107+
}
108+
if cfgCheck.Profiles["test-prof2"].SSL == nil || *cfgCheck.Profiles["test-prof2"].SSL != false {
109+
t.Errorf("expected test-prof2 SSL to be false, got %v", cfgCheck.Profiles["test-prof2"].SSL)
110+
}
111+
104112
// 5. List profiles (check default marker)
105113
out, err = executeCommand("profile", "ls")
106114
if err != nil {
@@ -176,11 +184,13 @@ func TestInitClient_Profile(t *testing.T) {
176184
_ = RootCmd.PersistentFlags().Set("port", "19999")
177185
_ = RootCmd.PersistentFlags().Set("client-id", "")
178186
_ = RootCmd.PersistentFlags().Set("client-secret", "")
187+
_ = RootCmd.PersistentFlags().Set("ssl", "true")
179188
_ = RootCmd.PersistentFlags().Set("verify-ssl", "true")
180189
RootCmd.PersistentFlags().Lookup("host").Changed = false
181190
RootCmd.PersistentFlags().Lookup("port").Changed = false
182191
RootCmd.PersistentFlags().Lookup("client-id").Changed = false
183192
RootCmd.PersistentFlags().Lookup("client-secret").Changed = false
193+
RootCmd.PersistentFlags().Lookup("ssl").Changed = false
184194
RootCmd.PersistentFlags().Lookup("verify-ssl").Changed = false
185195
cfgProfile = ""
186196

@@ -307,4 +317,64 @@ func TestInitClient_Profile(t *testing.T) {
307317
t.Errorf("expected verifySSL to be true from flag, got false")
308318
}
309319
})
320+
321+
t.Run("Profile ssl is used when flag is omitted", func(t *testing.T) {
322+
cfg, _ := config.Load()
323+
falseVal := false
324+
cfg.Profiles["prof-no-https"] = config.Profile{
325+
Host: "profile-host.com",
326+
Port: "1234",
327+
ClientID: "prof-id",
328+
ClientSecret: "prof-sec",
329+
SSL: &falseVal,
330+
}
331+
_ = cfg.Save()
332+
_ = RootCmd.PersistentFlags().Set("profile", "prof-no-https")
333+
_ = RootCmd.PersistentFlags().Set("ssl", "true")
334+
RootCmd.PersistentFlags().Lookup("ssl").Changed = false
335+
defer func() {
336+
_ = RootCmd.PersistentFlags().Set("profile", "")
337+
cfgProfile = ""
338+
}()
339+
340+
wrapper, err := initClient(context.Background(), false)
341+
if err != nil {
342+
t.Fatalf("initClient failed: %v", err)
343+
}
344+
345+
if wrapper.Session.Config.BaseUrl != "http://profile-host.com:1234" {
346+
t.Errorf("expected base URL 'http://profile-host.com:1234', got '%s'", wrapper.Session.Config.BaseUrl)
347+
}
348+
})
349+
350+
t.Run("Explicit ssl flag overrides profile value", func(t *testing.T) {
351+
cfg, _ := config.Load()
352+
falseVal := false
353+
cfg.Profiles["prof-no-https"] = config.Profile{
354+
Host: "profile-host.com",
355+
Port: "1234",
356+
ClientID: "prof-id",
357+
ClientSecret: "prof-sec",
358+
SSL: &falseVal,
359+
}
360+
_ = cfg.Save()
361+
_ = RootCmd.PersistentFlags().Set("profile", "prof-no-https")
362+
_ = RootCmd.PersistentFlags().Set("ssl", "true")
363+
RootCmd.PersistentFlags().Lookup("ssl").Changed = true
364+
defer func() {
365+
_ = RootCmd.PersistentFlags().Set("profile", "")
366+
cfgProfile = ""
367+
_ = RootCmd.PersistentFlags().Set("ssl", "true")
368+
RootCmd.PersistentFlags().Lookup("ssl").Changed = false
369+
}()
370+
371+
wrapper, err := initClient(context.Background(), false)
372+
if err != nil {
373+
t.Fatalf("initClient failed: %v", err)
374+
}
375+
376+
if wrapper.Session.Config.BaseUrl != "https://profile-host.com:1234" {
377+
t.Errorf("expected base URL 'https://profile-host.com:1234', got '%s'", wrapper.Session.Config.BaseUrl)
378+
}
379+
})
310380
}

internal/cmd/root.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ func initClient(ctx context.Context, oauth bool) (*client.ClientWrapper, error)
140140
token = cfgToken
141141
}
142142

143+
ssl := cfgSSL
144+
if !RootCmd.PersistentFlags().Lookup("ssl").Changed {
145+
if prof.SSL != nil {
146+
ssl = *prof.SSL
147+
}
148+
}
149+
143150
verifySSL := cfgVerifySSL
144151
if !RootCmd.PersistentFlags().Lookup("verify-ssl").Changed {
145152
if prof.VerifySSL != nil {
@@ -157,7 +164,7 @@ func initClient(ctx context.Context, oauth bool) (*client.ClientWrapper, error)
157164
clientSecret,
158165
token,
159166
cfgSuUser,
160-
cfgSSL,
167+
ssl,
161168
verifySSL,
162169
oauth,
163170
cfgTokenFile,

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Profile struct {
3131
AccessToken string `yaml:"access_token,omitempty"`
3232
RefreshToken string `yaml:"refresh_token,omitempty"`
3333
Expiration string `yaml:"expiration,omitempty"`
34+
SSL *bool `yaml:"ssl,omitempty"`
3435
VerifySSL *bool `yaml:"verify_ssl,omitempty"`
3536
}
3637

0 commit comments

Comments
 (0)