Skip to content

Commit 668fd52

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

4 files changed

Lines changed: 98 additions & 5 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: 80 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,72 @@ 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+
_ = RootCmd.PersistentFlags().Set("ssl", "true")
339+
if flag := RootCmd.PersistentFlags().Lookup("ssl"); flag != nil {
340+
flag.Changed = false
341+
}
342+
}()
343+
344+
wrapper, err := initClient(context.Background(), false)
345+
if err != nil {
346+
t.Fatalf("initClient failed: %v", err)
347+
}
348+
349+
if wrapper.Session.Config.BaseUrl != "http://profile-host.com:1234" {
350+
t.Errorf("expected base URL 'http://profile-host.com:1234', got '%s'", wrapper.Session.Config.BaseUrl)
351+
}
352+
})
353+
354+
t.Run("Explicit ssl flag overrides profile value", func(t *testing.T) {
355+
cfg, _ := config.Load()
356+
falseVal := false
357+
cfg.Profiles["prof-no-https"] = config.Profile{
358+
Host: "profile-host.com",
359+
Port: "1234",
360+
ClientID: "prof-id",
361+
ClientSecret: "prof-sec",
362+
SSL: &falseVal,
363+
}
364+
_ = cfg.Save()
365+
_ = RootCmd.PersistentFlags().Set("profile", "prof-no-https")
366+
_ = RootCmd.PersistentFlags().Set("ssl", "true")
367+
RootCmd.PersistentFlags().Lookup("ssl").Changed = true
368+
defer func() {
369+
_ = RootCmd.PersistentFlags().Set("profile", "")
370+
cfgProfile = ""
371+
_ = RootCmd.PersistentFlags().Set("ssl", "true")
372+
RootCmd.PersistentFlags().Lookup("ssl").Changed = false
373+
_ = RootCmd.PersistentFlags().Set("ssl", "true")
374+
if flag := RootCmd.PersistentFlags().Lookup("ssl"); flag != nil {
375+
flag.Changed = false
376+
}
377+
}()
378+
379+
wrapper, err := initClient(context.Background(), false)
380+
if err != nil {
381+
t.Fatalf("initClient failed: %v", err)
382+
}
383+
384+
if wrapper.Session.Config.BaseUrl != "https://profile-host.com:1234" {
385+
t.Errorf("expected base URL 'https://profile-host.com:1234', got '%s'", wrapper.Session.Config.BaseUrl)
386+
}
387+
})
310388
}

internal/cmd/root.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
"net/url"
2121
"os"
2222

23-
"github.com/spf13/cobra"
24-
v4 "github.com/looker-open-source/sdk-codegen/go/sdk/v4"
2523
"github.com/looker-open-source/looker-cli/internal/client"
2624
"github.com/looker-open-source/looker-cli/internal/config"
25+
v4 "github.com/looker-open-source/sdk-codegen/go/sdk/v4"
26+
"github.com/spf13/cobra"
2727
)
2828

2929
var (
@@ -140,6 +140,13 @@ func initClient(ctx context.Context, oauth bool) (*client.ClientWrapper, error)
140140
token = cfgToken
141141
}
142142

143+
ssl := cfgSSL
144+
if flag := RootCmd.PersistentFlags().Lookup("ssl"); flag != nil && !flag.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)