Skip to content

Commit

Permalink
Move CLI utilities into internal/util/cli from internal/util to balan…
Browse files Browse the repository at this point in the history
…ce tests (#5386)

* Move CLI utilities into internal/util/cli from internal/util to balance tests

* Fix lint complaint

* Remove remote server connection from tests -- sometimes, the connection dial seems to stall
  • Loading branch information
evankanderson authored Jan 31, 2025
1 parent fc18f0f commit 07e670c
Show file tree
Hide file tree
Showing 12 changed files with 774 additions and 773 deletions.
5 changes: 2 additions & 3 deletions cmd/cli/app/auth/auth_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/spf13/viper"
"google.golang.org/grpc"

"github.com/mindersec/minder/internal/util"
"github.com/mindersec/minder/internal/util/cli"
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
)
Expand All @@ -38,7 +37,7 @@ func deleteCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *gr
}

// We read name and email from the JWT. We don't need to validate it here.
creds, err := util.LoadCredentials()
creds, err := cli.LoadCredentials()
if err != nil {
return cli.MessageAndError("Error loading credentials from file", err)
}
Expand Down Expand Up @@ -78,7 +77,7 @@ func deleteCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *gr

// This step is added to avoid confusing the users by seeing their credentials locally, however it is not
// directly related to user deletion because the token will expire after 5 minutes and cannot be refreshed
err = util.RemoveCredentials()
err = cli.RemoveCredentials()
if err != nil {
cmd.Println(cli.WarningBanner.Render("Failed to remove locally stored credentials."))
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/cli/app/auth/auth_logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/mindersec/minder/internal/util"
"github.com/mindersec/minder/internal/util/cli"
"github.com/mindersec/minder/pkg/config"
clientconfig "github.com/mindersec/minder/pkg/config/client"
Expand All @@ -21,7 +20,7 @@ var logoutCmd = &cobra.Command{
Short: "Logout from minder control plane.",
Long: `Logout from minder control plane. Credentials will be removed from $XDG_CONFIG_HOME/minder/credentials.json`,
RunE: func(cmd *cobra.Command, _ []string) error {
if err := util.RemoveCredentials(); err != nil {
if err := cli.RemoveCredentials(); err != nil {
return cli.MessageAndError("Error removing credentials", err)
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/cli/app/auth/auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/mindersec/minder/internal/util"
"github.com/mindersec/minder/internal/util/cli"
"github.com/mindersec/minder/pkg/config"
clientconfig "github.com/mindersec/minder/pkg/config/client"
Expand Down Expand Up @@ -43,10 +42,10 @@ func TokenCommand(cmd *cobra.Command, _ []string) error {
// save credentials
issuerUrl := clientConfig.Identity.CLI.IssuerUrl
clientId := clientConfig.Identity.CLI.ClientId
creds, err := util.GetToken(issuerUrl, clientId)
creds, err := cli.GetToken(issuerUrl, clientId)
if err != nil {
cmd.Printf("Error getting token: %v\n", err)
if errors.Is(err, os.ErrNotExist) || errors.Is(err, util.ErrGettingRefreshToken) {
if errors.Is(err, os.ErrNotExist) || errors.Is(err, cli.ErrGettingRefreshToken) {
// wait for the token to be received
token, err := cli.Login(ctx, cmd, clientConfig, []string{}, skipBrowser)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/cli/app/auth/offline_token/offline_revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/spf13/viper"
"google.golang.org/grpc"

"github.com/mindersec/minder/internal/util"
"github.com/mindersec/minder/internal/util/cli"
"github.com/mindersec/minder/pkg/config"
clientconfig "github.com/mindersec/minder/pkg/config/client"
Expand Down Expand Up @@ -60,7 +59,7 @@ func offlineRevokeCommand(_ context.Context, cmd *cobra.Command, _ []string, _ *
issuerUrlStr := clientConfig.Identity.CLI.IssuerUrl
clientID := clientConfig.Identity.CLI.ClientId

if err := util.RevokeOfflineToken(tok, issuerUrlStr, clientID); err != nil {
if err := cli.RevokeOfflineToken(tok, issuerUrlStr, clientID); err != nil {
return fmt.Errorf("couldn't revoke token: %v", err)
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/cli/app/auth/offline_token/offline_use.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/spf13/viper"
"google.golang.org/grpc"

"github.com/mindersec/minder/internal/util"
"github.com/mindersec/minder/internal/util/cli"
"github.com/mindersec/minder/pkg/config"
clientconfig "github.com/mindersec/minder/pkg/config/client"
Expand Down Expand Up @@ -59,13 +58,13 @@ func offlineUseCommand(_ context.Context, cmd *cobra.Command, _ []string, _ *grp
issuerUrlStr := clientConfig.Identity.CLI.IssuerUrl
clientID := clientConfig.Identity.CLI.ClientId

creds, err := util.RefreshCredentials(tok, issuerUrlStr, clientID)
creds, err := cli.RefreshCredentials(tok, issuerUrlStr, clientID)
if err != nil {
return fmt.Errorf("couldn't fetch credentials: %v", err)
}

// save credentials
filePath, err := util.SaveCredentials(util.OpenIdCredentials{
filePath, err := cli.SaveCredentials(cli.OpenIdCredentials{
AccessToken: creds.AccessToken,
RefreshToken: creds.RefreshToken,
AccessTokenExpiresAt: creds.AccessTokenExpiresAt,
Expand Down
20 changes: 19 additions & 1 deletion internal/util/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,29 @@ func ConcatenateAndWrap(input string, maxLen int) string {
return result
}

// GetConfigDirPath returns the path to the config directory
func GetConfigDirPath() (string, error) {
// Get the XDG_CONFIG_HOME environment variable
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")

// If XDG_CONFIG_HOME is not set or empty, use $HOME/.config as the base directory
if xdgConfigHome == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("error getting home directory: %v", err)
}
xdgConfigHome = filepath.Join(homeDir, ".config")
}

filePath := filepath.Join(xdgConfigHome, "minder")
return filePath, nil
}

// GetDefaultCLIConfigPath returns the default path for the CLI config file
// Returns an empty string if the path cannot be determined
func GetDefaultCLIConfigPath() string {
//nolint:errcheck // ignore error as we are just checking if the file exists
cfgDirPath, _ := util.GetConfigDirPath()
cfgDirPath, _ := GetConfigDirPath()

var xdgConfigPath string
if cfgDirPath != "" {
Expand Down
50 changes: 50 additions & 0 deletions internal/util/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: Copyright 2025 The Minder Authors
// SPDX-License-Identifier: Apache-2.0

package cli_test

import (
"os"
"path/filepath"
"testing"

"github.com/mindersec/minder/internal/util/cli"
)

// TestGetConfigDirPath tests the GetConfigDirPath function
func TestGetConfigDirPath(t *testing.T) {
t.Parallel()
tests := []struct {
name string
envVar string
expectedPath string
expectingError bool
}{
{
name: "XDG_CONFIG_HOME set",
envVar: "/custom/config",
expectedPath: "/custom/config/minder",
expectingError: false,
},
{
name: "XDG_CONFIG_HOME is not set",
envVar: "",
expectedPath: filepath.Join(os.Getenv("HOME"), ".config", "minder"),
expectingError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
setEnvVar(t, XdgConfigHomeEnvVar, tt.envVar)
path, err := cli.GetConfigDirPath()
if (err != nil) != tt.expectingError {
t.Errorf("expected error: %v, got: %v", tt.expectingError, err)
}
if path != tt.expectedPath {
t.Errorf("expected path: %s, got: %s", tt.expectedPath, path)
}
})
}
}
Loading

0 comments on commit 07e670c

Please sign in to comment.