From fbbeef120e9bc3d7841448aba8146fbc89af83b8 Mon Sep 17 00:00:00 2001 From: mesembria Date: Thu, 30 Jan 2025 17:24:48 -0500 Subject: [PATCH] Adds check to ensure config file exists. Fixes: #4513 --- cmd/cli/app/auth/auth_login.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/cli/app/auth/auth_login.go b/cmd/cli/app/auth/auth_login.go index c5e6ab8464..24bd9556b2 100644 --- a/cmd/cli/app/auth/auth_login.go +++ b/cmd/cli/app/auth/auth_login.go @@ -5,6 +5,8 @@ package auth import ( "context" + "fmt" + "os" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -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)