Skip to content

Commit

Permalink
feat: Add saucectl configure list (#798)
Browse files Browse the repository at this point in the history
* feat: Add saucectl configure list

* fix unit test

* Update internal/cmd/configure/list.go

Co-authored-by: Alex Plischke <alex.plischke@saucelabs.com>

* renaming

---------

Co-authored-by: Alex Plischke <alex.plischke@saucelabs.com>
  • Loading branch information
tianfeng92 and alexplischke authored Jun 20, 2023
1 parent cd16f0f commit b23ddbc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
9 changes: 7 additions & 2 deletions internal/cmd/configure/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func Command() *cobra.Command {
}
cmd.Flags().StringVarP(&cliUsername, "username", "u", "", "username, available on your sauce labs account")
cmd.Flags().StringVarP(&cliAccessKey, "accessKey", "a", "", "accessKey, available on your sauce labs account")

cmd.AddCommand(ListCommand())
return cmd
}

Expand All @@ -59,8 +61,11 @@ func printCreds(creds iam.Credentials) {
valueStyle := color.New(color.FgBlue)

fmt.Println("Currently configured credentials:")
fmt.Println(labelStyle.Sprint(" Username:"), valueStyle.Sprint(creds.Username))
fmt.Println(labelStyle.Sprint(" Access key:"), valueStyle.Sprint(mask(creds.AccessKey)))
fmt.Println(labelStyle.Sprint("\t Username:"), valueStyle.Sprint(creds.Username))
fmt.Println(labelStyle.Sprint("\tAccess key:"), valueStyle.Sprint(mask(creds.AccessKey)))

println()
fmt.Printf("Collected from: %s", creds.Source)

println()
println()
Expand Down
22 changes: 22 additions & 0 deletions internal/cmd/configure/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package configure

import (
"github.com/saucelabs/saucectl/internal/credentials"
"github.com/spf13/cobra"
)

func ListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Aliases: []string{
"ls",
},
Short: "Shows the current credentials and their origin.",
Run: func(cmd *cobra.Command, args []string) {
creds := credentials.Get()
printCreds(creds)
},
}

return cmd
}
9 changes: 9 additions & 0 deletions internal/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func init() {
// It's under the user's home directory, if defined, otherwise under the current working directory.
var DefaultCredsPath = ""

// EnvSource indicates the credentials are retrieved from environment variables.
const EnvSource = "Environment variables"

// ConfigFileSource indicates the credentials are retrieved from configuration file.
const ConfigFileSource = "Configuration file"

// Get returns the configured credentials.
// Effectively a convenience wrapper around FromEnv, followed by a call to FromFile.
//
Expand All @@ -39,6 +45,7 @@ func FromEnv() iam.Credentials {
return iam.Credentials{
Username: os.Getenv("SAUCE_USERNAME"),
AccessKey: os.Getenv("SAUCE_ACCESS_KEY"),
Source: fmt.Sprintf("%s(%s)", EnvSource, "$SAUCE_USERNAME, $SAUCE_ACCESS_KEY"),
}
}

Expand Down Expand Up @@ -67,6 +74,8 @@ func fromFile(path string) iam.Credentials {
return iam.Credentials{}
}

c.Source = fmt.Sprintf("%s(%s)", ConfigFileSource, path)

return c
}

Expand Down
10 changes: 6 additions & 4 deletions internal/credentials/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package credentials
import (
"os"
"path/filepath"
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/saucelabs/saucectl/internal/iam"
)

Expand All @@ -24,6 +24,7 @@ func TestFromEnv(t *testing.T) {
want: iam.Credentials{
Username: "saucebot",
AccessKey: "123",
Source: "Environment variables($SAUCE_USERNAME, $SAUCE_ACCESS_KEY)",
},
},
{
Expand All @@ -32,13 +33,13 @@ func TestFromEnv(t *testing.T) {
_ = os.Unsetenv("SAUCE_USERNAME")
_ = os.Unsetenv("SAUCE_ACCESS_KEY")
},
want: iam.Credentials{},
want: iam.Credentials{Source: "Environment variables($SAUCE_USERNAME, $SAUCE_ACCESS_KEY)"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.beforeTest()
if got := FromEnv(); !reflect.DeepEqual(got, tt.want) {
if got := FromEnv(); !cmp.Equal(got, tt.want) {
t.Errorf("FromEnv() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -148,7 +149,8 @@ func TestFromFile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.beforeTest()
if got := fromFile(tt.args.path); !reflect.DeepEqual(got, tt.want) {
got := fromFile(tt.args.path)
if !cmp.Equal(got.Username, tt.want.Username) || !cmp.Equal(got.AccessKey, tt.want.AccessKey) {
t.Errorf("FromFile() = %v, want %v", got, tt.want)
}
})
Expand Down
1 change: 1 addition & 0 deletions internal/iam/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package iam
type Credentials struct {
Username string `yaml:"username"`
AccessKey string `yaml:"accessKey"`
Source string `yaml:"-"`
}

// IsSet checks whether the credentials, i.e. username and access key are not empty.
Expand Down

0 comments on commit b23ddbc

Please sign in to comment.