diff --git a/internal/cmd/backup/backup.go b/internal/cmd/backup/backup.go index 1ad6f62e..dda4dfbc 100644 --- a/internal/cmd/backup/backup.go +++ b/internal/cmd/backup/backup.go @@ -16,10 +16,10 @@ import ( // BackupCmd handles branch backups. func BackupCmd(ch *cmdutil.Helper) *cobra.Command { - cmd := &cobra.Command{ - Use: "backup ", - Short: "Create, read, destroy, and update branch backups", + Use: "backup ", + 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") diff --git a/internal/cmd/branch/branch.go b/internal/cmd/branch/branch.go index c12a9bc1..ec5ee32c 100644 --- a/internal/cmd/branch/branch.go +++ b/internal/cmd/branch/branch.go @@ -12,8 +12,9 @@ import ( // BranchCmd handles the branching of a database. func BranchCmd(ch *cmdutil.Helper) *cobra.Command { cmd := &cobra.Command{ - Use: "branch ", - Short: "Create, delete, and manage branches", + Use: "branch ", + Short: "Create, delete, and manage branches", + PersistentPreRunE: cmdutil.CheckAuthentication(ch.Config), } cmd.PersistentFlags().StringVar(&ch.Config.Organization, "org", ch.Config.Organization, diff --git a/internal/cmd/connect/connect.go b/internal/cmd/connect/connect.go index 5445b361..805fd804 100644 --- a/internal/cmd/connect/connect.go +++ b/internal/cmd/connect/connect.go @@ -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] diff --git a/internal/cmd/database/database.go b/internal/cmd/database/database.go index 18978e84..5722ebef 100644 --- a/internal/cmd/database/database.go +++ b/internal/cmd/database/database.go @@ -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 ", - Short: "Create, read, destroy, and update databases", - Aliases: []string{"db"}, + Use: "database ", + 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, diff --git a/internal/cmd/deployrequest/dr.go b/internal/cmd/deployrequest/dr.go index abb4a9dc..a050264f 100644 --- a/internal/cmd/deployrequest/dr.go +++ b/internal/cmd/deployrequest/dr.go @@ -11,9 +11,10 @@ import ( // Requests. func DeployRequestCmd(ch *cmdutil.Helper) *cobra.Command { cmd := &cobra.Command{ - Use: "deploy-request ", - Short: "Create, approve, diff, and manage deploy requests", - Aliases: []string{"dr"}, + Use: "deploy-request ", + 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") diff --git a/internal/cmd/org/org.go b/internal/cmd/org/org.go index ca29408c..05113b29 100644 --- a/internal/cmd/org/org.go +++ b/internal/cmd/org/org.go @@ -10,8 +10,9 @@ import ( func OrgCmd(ch *cmdutil.Helper) *cobra.Command { cmd := &cobra.Command{ - Use: "org ", - Short: "Modify and manage organization options", + Use: "org ", + Short: "Modify and manage organization options", + PersistentPreRunE: cmdutil.CheckAuthentication(ch.Config), } cmd.AddCommand(SwitchCmd(ch)) diff --git a/internal/cmd/shell/shell.go b/internal/cmd/shell/shell.go index 6729f09b..d3af9da7 100644 --- a/internal/cmd/shell/shell.go +++ b/internal/cmd/shell/shell.go @@ -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] diff --git a/internal/cmd/snapshot/snapshot.go b/internal/cmd/snapshot/snapshot.go index 9f2f0046..5da99147 100644 --- a/internal/cmd/snapshot/snapshot.go +++ b/internal/cmd/snapshot/snapshot.go @@ -12,8 +12,9 @@ import ( // SnapshotCmd encapsulates the command for running snapshots. func SnapshotCmd(ch *cmdutil.Helper) *cobra.Command { cmd := &cobra.Command{ - Use: "snapshot ", - Short: "Create, get, and list schema snapshots", + Use: "snapshot ", + 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") diff --git a/internal/cmd/token/token.go b/internal/cmd/token/token.go index c687bfca..2eb33d7b 100644 --- a/internal/cmd/token/token.go +++ b/internal/cmd/token/token.go @@ -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 ", - Short: "Create, get, and list service tokens", + Use: "service-token ", + 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") diff --git a/internal/cmdutil/args.go b/internal/cmdutil/args.go deleted file mode 100644 index 7e87eba4..00000000 --- a/internal/cmdutil/args.go +++ /dev/null @@ -1,28 +0,0 @@ -package cmdutil - -import ( - "fmt" - "strings" - - "github.com/spf13/cobra" -) - -// 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()) - } -} diff --git a/internal/cmdutil/cmdutil.go b/internal/cmdutil/cmdutil.go index 24547ed4..a1832af2 100644 --- a/internal/cmdutil/cmdutil.go +++ b/internal/cmdutil/cmdutil.go @@ -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 @@ -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'") + } +}