Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ var profileAddCmd = &cobra.Command{
return fmt.Errorf("profile %q already exists", name)
}

var ssl *bool
if flag := cmd.Flags().Lookup("ssl"); flag != nil && flag.Changed {
val, _ := cmd.Flags().GetBool("ssl")
ssl = &val
}

var verifySSL *bool
if flag := cmd.Flags().Lookup("verify-ssl"); flag != nil && flag.Changed {
val, _ := cmd.Flags().GetBool("verify-ssl")
Expand All @@ -106,6 +112,7 @@ var profileAddCmd = &cobra.Command{
ClientSecret: profClientSecret,
AccessToken: profToken,
RefreshToken: profRefreshToken,
SSL: ssl,
VerifySSL: verifySSL,
}

Expand Down
82 changes: 80 additions & 2 deletions internal/cmd/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,20 @@ func TestProfileCommands(t *testing.T) {
t.Errorf("expected '* test-prof (test.looker.com:19999)', got %q", out)
}

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

cfgCheck, err := config.Load()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if cfgCheck.Profiles["test-prof2"].SSL == nil || *cfgCheck.Profiles["test-prof2"].SSL != false {
t.Errorf("expected test-prof2 SSL to be false, got %v", cfgCheck.Profiles["test-prof2"].SSL)
}

// 5. List profiles (check default marker)
out, err = executeCommand("profile", "ls")
if err != nil {
Expand Down Expand Up @@ -176,11 +184,13 @@ func TestInitClient_Profile(t *testing.T) {
_ = RootCmd.PersistentFlags().Set("port", "19999")
_ = RootCmd.PersistentFlags().Set("client-id", "")
_ = RootCmd.PersistentFlags().Set("client-secret", "")
_ = RootCmd.PersistentFlags().Set("ssl", "true")
_ = RootCmd.PersistentFlags().Set("verify-ssl", "true")
RootCmd.PersistentFlags().Lookup("host").Changed = false
RootCmd.PersistentFlags().Lookup("port").Changed = false
RootCmd.PersistentFlags().Lookup("client-id").Changed = false
RootCmd.PersistentFlags().Lookup("client-secret").Changed = false
RootCmd.PersistentFlags().Lookup("ssl").Changed = false
RootCmd.PersistentFlags().Lookup("verify-ssl").Changed = false
cfgProfile = ""

Expand Down Expand Up @@ -307,4 +317,72 @@ func TestInitClient_Profile(t *testing.T) {
t.Errorf("expected verifySSL to be true from flag, got false")
}
})

t.Run("Profile ssl is used when flag is omitted", func(t *testing.T) {
cfg, _ := config.Load()
falseVal := false
cfg.Profiles["prof-no-https"] = config.Profile{
Host: "profile-host.com",
Port: "1234",
ClientID: "prof-id",
ClientSecret: "prof-sec",
SSL: &falseVal,
}
_ = cfg.Save()
_ = RootCmd.PersistentFlags().Set("profile", "prof-no-https")
_ = RootCmd.PersistentFlags().Set("ssl", "true")
RootCmd.PersistentFlags().Lookup("ssl").Changed = false
defer func() {
_ = RootCmd.PersistentFlags().Set("profile", "")
cfgProfile = ""
_ = RootCmd.PersistentFlags().Set("ssl", "true")
if flag := RootCmd.PersistentFlags().Lookup("ssl"); flag != nil {
flag.Changed = false
}
}()
Comment thread
drstrangelooker marked this conversation as resolved.

wrapper, err := initClient(context.Background(), false)
if err != nil {
t.Fatalf("initClient failed: %v", err)
}

if wrapper.Session.Config.BaseUrl != "http://profile-host.com:1234" {
t.Errorf("expected base URL 'http://profile-host.com:1234', got '%s'", wrapper.Session.Config.BaseUrl)
}
})

t.Run("Explicit ssl flag overrides profile value", func(t *testing.T) {
cfg, _ := config.Load()
falseVal := false
cfg.Profiles["prof-no-https"] = config.Profile{
Host: "profile-host.com",
Port: "1234",
ClientID: "prof-id",
ClientSecret: "prof-sec",
SSL: &falseVal,
}
_ = cfg.Save()
_ = RootCmd.PersistentFlags().Set("profile", "prof-no-https")
_ = RootCmd.PersistentFlags().Set("ssl", "true")
RootCmd.PersistentFlags().Lookup("ssl").Changed = true
defer func() {
_ = RootCmd.PersistentFlags().Set("profile", "")
cfgProfile = ""
_ = RootCmd.PersistentFlags().Set("ssl", "true")
RootCmd.PersistentFlags().Lookup("ssl").Changed = false
_ = RootCmd.PersistentFlags().Set("ssl", "true")
if flag := RootCmd.PersistentFlags().Lookup("ssl"); flag != nil {
flag.Changed = false
}
}()
Comment thread
drstrangelooker marked this conversation as resolved.

wrapper, err := initClient(context.Background(), false)
if err != nil {
t.Fatalf("initClient failed: %v", err)
}

if wrapper.Session.Config.BaseUrl != "https://profile-host.com:1234" {
t.Errorf("expected base URL 'https://profile-host.com:1234', got '%s'", wrapper.Session.Config.BaseUrl)
}
})
}
13 changes: 10 additions & 3 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"net/url"
"os"

"github.com/spf13/cobra"
v4 "github.com/looker-open-source/sdk-codegen/go/sdk/v4"
"github.com/looker-open-source/looker-cli/internal/client"
"github.com/looker-open-source/looker-cli/internal/config"
v4 "github.com/looker-open-source/sdk-codegen/go/sdk/v4"
"github.com/spf13/cobra"
)

var (
Expand Down Expand Up @@ -140,6 +140,13 @@ func initClient(ctx context.Context, oauth bool) (*client.ClientWrapper, error)
token = cfgToken
}

ssl := cfgSSL
if flag := RootCmd.PersistentFlags().Lookup("ssl"); flag != nil && !flag.Changed {
if prof.SSL != nil {
ssl = *prof.SSL
}
}

verifySSL := cfgVerifySSL
if !RootCmd.PersistentFlags().Lookup("verify-ssl").Changed {
if prof.VerifySSL != nil {
Expand All @@ -157,7 +164,7 @@ func initClient(ctx context.Context, oauth bool) (*client.ClientWrapper, error)
clientSecret,
token,
cfgSuUser,
cfgSSL,
ssl,
verifySSL,
oauth,
cfgTokenFile,
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Profile struct {
AccessToken string `yaml:"access_token,omitempty"`
RefreshToken string `yaml:"refresh_token,omitempty"`
Expiration string `yaml:"expiration,omitempty"`
SSL *bool `yaml:"ssl,omitempty"`
VerifySSL *bool `yaml:"verify_ssl,omitempty"`
}

Expand Down
Loading