Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
fix auth status tests
Browse files Browse the repository at this point in the history
  • Loading branch information
profclems committed May 14, 2021
1 parent 7ed4fdb commit 65a7d41
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 20 deletions.
6 changes: 3 additions & 3 deletions commands/auth/login/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
const tokenUser = "oauth2"

type configExt interface {
GetWithSource(string, string) (string, string, error)
GetWithSource(string, string, bool) (string, string, error)
}

type CredentialOptions struct {
Expand Down Expand Up @@ -102,11 +102,11 @@ func helperRun(opts *CredentialOptions) error {
}

var gotUser string
gotToken, source, _ := cfg.GetWithSource(expectedParams["host"], "token")
gotToken, source, _ := cfg.GetWithSource(expectedParams["host"], "token", true)
if strings.HasSuffix(source, "_TOKEN") {
gotUser = tokenUser
} else {
gotUser, _, _ = cfg.GetWithSource(expectedParams["host"], "user")
gotUser, _, _ = cfg.GetWithSource(expectedParams["host"], "user", true)
}

if gotUser == "" || gotToken == "" {
Expand Down
2 changes: 1 addition & 1 deletion commands/auth/login/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type tinyConfig map[string]string

func (c tinyConfig) GetWithSource(host, key string) (string, string, error) {
func (c tinyConfig) GetWithSource(host, key string, searchENVVars bool) (string, string, error) {
return c[fmt.Sprintf("%s:%s", host, key)], c["_source"], nil
}

Expand Down
2 changes: 1 addition & 1 deletion commands/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func loginRun() error {
if token := config.GetFromEnv("token"); token != "" {
fmt.Fprintf(opts.IO.StdErr, "%s you have GITLAB_TOKEN or OAUTH_TOKEN environment variable set. Unset if you don't want to use it for glab\n", c.Yellow("!WARNING:"))
}
existingToken, _, _ := cfg.GetWithSource(hostname, "token")
existingToken, _, _ := cfg.GetWithSource(hostname, "token", false)

if existingToken != "" && opts.Interactive {
apiClient, err := cmdutils.LabClientFunc(hostname, cfg, false)
Expand Down
2 changes: 1 addition & 1 deletion commands/auth/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func statusRun(opts *StatusOpts) error {
statusInfo[instance] = append(statusInfo[instance], fmt.Sprintf(x, ys...))
}

token, tokenSource, _ := cfg.GetWithSource(instance, "token")
token, tokenSource, _ := cfg.GetWithSource(instance, "token", false)
apiClient, err := api.NewClientWithCfg(instance, cfg, false)
if opts.HttpClientOverride != nil {
apiClient, _ = opts.HttpClientOverride(token, instance)
Expand Down
4 changes: 2 additions & 2 deletions commands/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func genKey(host, key string) string {
}

func (c configStub) Get(host, key string) (string, error) {
val, _, err := c.GetWithSource(host, key)
val, _, err := c.GetWithSource(host, key, true)
return val, err
}

func (c configStub) GetWithSource(host, key string) (string, string, error) {
func (c configStub) GetWithSource(host, key string, searchENVVars bool) (string, string, error) {
if v, found := c[genKey(host, key)]; found {
return v, "(memory)", nil
}
Expand Down
19 changes: 7 additions & 12 deletions internal/config/config_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
// This interface describes interacting with some persistent configuration for glab.
type Config interface {
Get(string, string) (string, error)
GetWithSource(string, string) (string, string, error)
GetWithSource(string, string, bool) (string, string, error)
Set(string, string, string) error
UnsetHost(string)
Hosts() ([]string, error)
Expand Down Expand Up @@ -289,32 +289,27 @@ func NewBlankRoot() *yaml.Node {
type fileConfig struct {
ConfigMap
documentRoot *yaml.Node
envSearched bool
}

func (c *fileConfig) Root() *yaml.Node {
return c.ConfigMap.Root
}

func (c *fileConfig) Get(hostname, key string) (string, error) {
env := GetFromEnv(key)
c.envSearched = true // set to avoid searching env again
if env != "" {
return env, nil
}
key = ConfigKeyEquivalence(key)
val, _, err := c.GetWithSource(hostname, key)
c.envSearched = false // reset
val, _, err := c.GetWithSource(hostname, key, true)
return val, err
}

func (c *fileConfig) GetWithSource(hostname, key string) (string, string, error) {
if !c.envSearched {
func (c *fileConfig) GetWithSource(hostname, key string, searchENVVars bool) (string, string, error) {
if searchENVVars {
v, s := GetFromEnvWithSource(key)
if v != "" {
return v, s, nil
}
}

key = ConfigKeyEquivalence(key)

var cfgError error

if hostname != "" {
Expand Down

0 comments on commit 65a7d41

Please sign in to comment.