Improve error message when client is not logged in (#1867)
This commit is contained in:
+14
-1
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func clientFromCLI(c *cli.Context) (*service.Client, error) {
|
||||
func unauthenticatedClientFromCLI(c *cli.Context) (*service.Client, error) {
|
||||
if err := makeConfigIfNotExists(c.String("config")); err != nil {
|
||||
return nil, errors.Wrapf(err, "error verifying that config exists at %s", c.String("config"))
|
||||
}
|
||||
@@ -32,12 +32,25 @@ func clientFromCLI(c *cli.Context) (*service.Client, error) {
|
||||
return nil, errors.Wrap(err, "error creating Fleet API client handler")
|
||||
}
|
||||
|
||||
return fleet, nil
|
||||
}
|
||||
|
||||
func clientFromCLI(c *cli.Context) (*service.Client, error) {
|
||||
fleet, err := unauthenticatedClientFromCLI(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add authentication token
|
||||
t, err := getConfigValue(c, "token")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error getting token from the config")
|
||||
}
|
||||
|
||||
if token, ok := t.(string); ok {
|
||||
if token == "" {
|
||||
return nil, errors.New("Please log in with: fleetctl login")
|
||||
}
|
||||
fleet.SetToken(token)
|
||||
} else {
|
||||
return nil, errors.Errorf("token config value was not a string: %+v", t)
|
||||
|
||||
@@ -42,7 +42,7 @@ Interactively prompts for email and password if not specified in the flags or en
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
fleet, err := clientFromCLI(c)
|
||||
fleet, err := unauthenticatedClientFromCLI(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ func setupCommand() cli.Command {
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
fleet, err := clientFromCLI(c)
|
||||
fleet, err := unauthenticatedClientFromCLI(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user