Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds check to ensure config file exists. Fixes: #4513 #5387

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
Comment on lines +38 to +43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this only on auth login and not on other commands (i.e. in ReadConfigFromViper)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I'll move the logic up to that function and update the unit test as well.


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
Loading