Skip to content

Commit

Permalink
Adds check to ensure config file exists. Fixes: #4513
Browse files Browse the repository at this point in the history
  • Loading branch information
mesembria committed Feb 3, 2025
1 parent f5f00ed commit 835ac83
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions cmd/cli/app/auth/auth_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package auth

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -27,14 +29,24 @@ $XDG_CONFIG_HOME/minder/credentials.json`,

// LoginCommand is the login subcommand
func LoginCommand(ctx context.Context, cmd *cobra.Command, _ []string, _ *grpc.ClientConn) error {
clientConfig, err := config.ReadConfigFromViper[clientconfig.Config](viper.GetViper())
if err != nil {
return cli.MessageAndError("Unable to read config", err)
}
v := viper.GetViper()

// No longer print usage on returned error, since we've parsed our inputs
// See https://github.com/spf13/cobra/issues/340#issuecomment-374617413
cmd.SilenceUsage = true

// If config file is specified but doesn't exist, that's an error
if configFile := v.GetString("config"); configFile != "" {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
return cli.MessageAndError("Config file does not exist", fmt.Errorf("file %s not found", configFile))
}
}

clientConfig, err := config.ReadConfigFromViper[clientconfig.Config](v)
if err != nil {
return cli.MessageAndError("Unable to read config", err)
}

filePath, err := cli.LoginAndSaveCreds(ctx, cmd, clientConfig)
if err != nil {
return cli.MessageAndError("Error ensuring credentials", err)
Expand Down

0 comments on commit 835ac83

Please sign in to comment.