Skip to content

Commit

Permalink
Merge pull request #155 from planetscale/fatih/show-user-friendly-aut…
Browse files Browse the repository at this point in the history
…h-message

cmd: return a user friendly message if the user is not authenticated
  • Loading branch information
fatih authored Apr 9, 2021
2 parents 595c8bb + 744cc84 commit 5a7caea
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 45 deletions.
6 changes: 3 additions & 3 deletions internal/cmd/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (

// BackupCmd handles branch backups.
func BackupCmd(ch *cmdutil.Helper) *cobra.Command {

cmd := &cobra.Command{
Use: "backup <command>",
Short: "Create, read, destroy, and update branch backups",
Use: "backup <command>",
Short: "Create, read, destroy, and update branch backups",
PersistentPreRunE: cmdutil.CheckAuthentication(ch.Config),
}

cmd.PersistentFlags().StringVar(&ch.Config.Organization, "org", ch.Config.Organization, "The organization for the current user")
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/branch/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
// BranchCmd handles the branching of a database.
func BranchCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "branch <command>",
Short: "Create, delete, and manage branches",
Use: "branch <command>",
Short: "Create, delete, and manage branches",
PersistentPreRunE: cmdutil.CheckAuthentication(ch.Config),
}

cmd.PersistentFlags().StringVar(&ch.Config.Organization, "org", ch.Config.Organization,
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ choose one. To connect to a specific branch, pass the branch as a second
argument:
pscale connect mydatabase mybranch`,
PersistentPreRunE: cmdutil.CheckAuthentication(ch.Config),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
database := args[0]
Expand Down
7 changes: 4 additions & 3 deletions internal/cmd/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
// DatabaseCmd encapsulates the commands for creating a database
func DatabaseCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "database <command>",
Short: "Create, read, destroy, and update databases",
Aliases: []string{"db"},
Use: "database <command>",
Short: "Create, read, destroy, and update databases",
Aliases: []string{"db"},
PersistentPreRunE: cmdutil.CheckAuthentication(ch.Config),
}

cmd.PersistentFlags().StringVar(&ch.Config.Organization, "org", ch.Config.Organization,
Expand Down
7 changes: 4 additions & 3 deletions internal/cmd/deployrequest/dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
// Requests.
func DeployRequestCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "deploy-request <command>",
Short: "Create, approve, diff, and manage deploy requests",
Aliases: []string{"dr"},
Use: "deploy-request <command>",
Short: "Create, approve, diff, and manage deploy requests",
Aliases: []string{"dr"},
PersistentPreRunE: cmdutil.CheckAuthentication(ch.Config),
}

cmd.PersistentFlags().StringVar(&ch.Config.Organization, "org", ch.Config.Organization, "The organization for the current user")
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

func OrgCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "org <command>",
Short: "Modify and manage organization options",
Use: "org <command>",
Short: "Modify and manage organization options",
PersistentPreRunE: cmdutil.CheckAuthentication(ch.Config),
}

cmd.AddCommand(SwitchCmd(ch))
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ choose one. To open a shell instance to a specific branch, pass the branch as a
second argument:
pscale shell mydatabase mybranch`,
PersistentPreRunE: cmdutil.CheckAuthentication(ch.Config),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
database := args[0]
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
// SnapshotCmd encapsulates the command for running snapshots.
func SnapshotCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "snapshot <action>",
Short: "Create, get, and list schema snapshots",
Use: "snapshot <action>",
Short: "Create, get, and list schema snapshots",
PersistentPreRunE: cmdutil.CheckAuthentication(ch.Config),
}

cmd.PersistentFlags().StringVar(&ch.Config.Organization, "org", ch.Config.Organization, "The organization for the current user")
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
// TokenCmd encapsulates the command for running snapshots.
func TokenCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "service-token <action>",
Short: "Create, get, and list service tokens",
Use: "service-token <action>",
Short: "Create, get, and list service tokens",
PersistentPreRunE: cmdutil.CheckAuthentication(ch.Config),
}

cmd.PersistentFlags().StringVar(&ch.Config.Organization, "org", ch.Config.Organization, "The organization for the current user")
Expand Down
28 changes: 0 additions & 28 deletions internal/cmdutil/args.go

This file was deleted.

37 changes: 37 additions & 0 deletions internal/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package cmdutil

import (
"errors"
"fmt"
"strings"

"github.com/planetscale/cli/internal/config"
"github.com/planetscale/cli/internal/printer"
ps "github.com/planetscale/planetscale-go/planetscale"
"github.com/spf13/cobra"
)

// Helper is passed to every single command and is used by individual
Expand All @@ -18,3 +23,35 @@ type Helper struct {
// Printer is used to print output of a command to stdout.
Printer *printer.Printer
}

// RequiredArgs returns a short and actionable error message if the given
// required arguments are not available.
func RequiredArgs(reqArgs ...string) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
n := len(reqArgs)
if len(args) >= n {
return nil
}

missing := reqArgs[len(args):]

a := fmt.Sprintf("arguments <%s>", strings.Join(missing, ", "))
if len(missing) == 1 {
a = fmt.Sprintf("argument <%s>", missing[0])
}

return fmt.Errorf("missing %s \n\n%s", a, cmd.UsageString())
}
}

// CheckAuthentication checks whether the user is authenticated and returns a
// actionable error message.
func CheckAuthentication(cfg *config.Config) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
if cfg.IsAuthenticated() {
return nil
}

return errors.New("not authenticated yet. Please run 'pscale auth login'")
}
}

0 comments on commit 5a7caea

Please sign in to comment.