From 9e7002d3d855171e47c1c4ad4d1390644b7409c2 Mon Sep 17 00:00:00 2001 From: Iheanyi Ekechukwu Date: Thu, 1 Apr 2021 17:01:38 -0400 Subject: [PATCH 1/4] Respect TTY settings for terminal styling. --- internal/cmdutil/terminal.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/cmdutil/terminal.go b/internal/cmdutil/terminal.go index 9b814667..9d0e89fa 100644 --- a/internal/cmdutil/terminal.go +++ b/internal/cmdutil/terminal.go @@ -39,12 +39,18 @@ func PrintProgress(message string) func() { // BoldBlue returns a string formatted with blue and bold. func BoldBlue(msg string) string { + if !IsTTY { + return msg + } // the 'color' package already handles IsTTY gracefully return color.New(color.FgBlue).Add(color.Bold).Sprint(msg) } // Bold returns a string formatted with bold. func Bold(msg string) string { + if !IsTTY { + return msg + } // the 'color' package already handles IsTTY gracefully return color.New(color.Bold).Sprint(msg) } From d909a2f9d0be397a98878afd58e54714b10f44f7 Mon Sep 17 00:00:00 2001 From: Iheanyi Ekechukwu Date: Thu, 1 Apr 2021 17:01:49 -0400 Subject: [PATCH 2/4] Add error checker for not found errors. --- internal/cmdutil/terminal.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/cmdutil/terminal.go b/internal/cmdutil/terminal.go index 9d0e89fa..6447c8b6 100644 --- a/internal/cmdutil/terminal.go +++ b/internal/cmdutil/terminal.go @@ -8,6 +8,7 @@ import ( "github.com/briandowns/spinner" "github.com/fatih/color" "github.com/mattn/go-isatty" + "github.com/planetscale/planetscale-go/planetscale" ) var IsTTY = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) @@ -54,3 +55,12 @@ func Bold(msg string) string { // the 'color' package already handles IsTTY gracefully return color.New(color.Bold).Sprint(msg) } + +func IsNotFoundError(ogErr error) bool { + err, ok := ogErr.(*planetscale.ErrorResponse) + if ok { + return err.Code == "not_found" + } + + return false +} From f06dd225735eb05ea8ecb15050383b85fa5d5e68 Mon Sep 17 00:00:00 2001 From: Iheanyi Ekechukwu Date: Thu, 1 Apr 2021 17:02:49 -0400 Subject: [PATCH 3/4] Implement not found error styling for database. --- internal/cmd/database/delete.go | 3 +++ internal/cmd/database/get.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/internal/cmd/database/delete.go b/internal/cmd/database/delete.go index 69fc194b..c097066d 100644 --- a/internal/cmd/database/delete.go +++ b/internal/cmd/database/delete.go @@ -68,6 +68,9 @@ func DeleteCmd(cfg *config.Config) *cobra.Command { Database: name, }) if err != nil { + if cmdutil.IsNotFoundError(err) { + return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(name), cmdutil.BoldBlue(cfg.Organization)) + } return err } diff --git a/internal/cmd/database/get.go b/internal/cmd/database/get.go index b97463cd..c5882ab3 100644 --- a/internal/cmd/database/get.go +++ b/internal/cmd/database/get.go @@ -49,6 +49,9 @@ func GetCmd(cfg *config.Config) *cobra.Command { Database: name, }) if err != nil { + if cmdutil.IsNotFoundError(err) { + return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(name), cmdutil.BoldBlue(cfg.Organization)) + } return err } From 6e3d8fcf4000d6e4dbc9e46741d895b09cf9e4d3 Mon Sep 17 00:00:00 2001 From: Iheanyi Ekechukwu Date: Thu, 1 Apr 2021 17:28:31 -0400 Subject: [PATCH 4/4] Start on improving not found errors. --- internal/cmd/backup/create.go | 3 +++ internal/cmd/backup/delete.go | 3 +++ internal/cmd/backup/get.go | 3 +++ internal/cmd/backup/list.go | 3 +++ internal/cmd/branch/create.go | 3 +++ internal/cmd/branch/delete.go | 3 +++ internal/cmd/branch/get.go | 3 +++ internal/cmd/branch/list.go | 3 +++ internal/cmd/branch/status.go | 3 +++ internal/cmd/database/delete.go | 2 +- internal/cmd/database/get.go | 2 +- 11 files changed, 29 insertions(+), 2 deletions(-) diff --git a/internal/cmd/backup/create.go b/internal/cmd/backup/create.go index 4a5827f4..50a4caba 100644 --- a/internal/cmd/backup/create.go +++ b/internal/cmd/backup/create.go @@ -37,6 +37,9 @@ func CreateCmd(cfg *config.Config) *cobra.Command { defer end() backup, err := client.Backups.Create(ctx, createReq) if err != nil { + if cmdutil.IsNotFoundError(err) { + return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(database)) + } return err } diff --git a/internal/cmd/backup/delete.go b/internal/cmd/backup/delete.go index 057d0e3c..2875287b 100644 --- a/internal/cmd/backup/delete.go +++ b/internal/cmd/backup/delete.go @@ -72,6 +72,9 @@ func DeleteCmd(cfg *config.Config) *cobra.Command { Backup: backup, }) if err != nil { + if cmdutil.IsNotFoundError(err) { + return fmt.Errorf("%s does not exist in branch %s of %s\n", cmdutil.BoldBlue(backup), cmdutil.BoldBlue(branch), cmdutil.BoldBlue(database)) + } return err } diff --git a/internal/cmd/backup/get.go b/internal/cmd/backup/get.go index c179962d..a4a5d29e 100644 --- a/internal/cmd/backup/get.go +++ b/internal/cmd/backup/get.go @@ -59,6 +59,9 @@ func GetCmd(cfg *config.Config) *cobra.Command { end() err = printer.PrintOutput(cfg.OutputJSON, printer.NewBackupPrinter(b)) if err != nil { + if cmdutil.IsNotFoundError(err) { + return fmt.Errorf("%s does not exist in branch %s of %s\n", cmdutil.BoldBlue(backup), cmdutil.BoldBlue(branch), cmdutil.BoldBlue(database)) + } return err } diff --git a/internal/cmd/backup/list.go b/internal/cmd/backup/list.go index 028e0e7c..0ae8e801 100644 --- a/internal/cmd/backup/list.go +++ b/internal/cmd/backup/list.go @@ -53,6 +53,9 @@ func ListCmd(cfg *config.Config) *cobra.Command { Branch: branch, }) if err != nil { + if cmdutil.IsNotFoundError(err) { + return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(database)) + } return errors.Wrap(err, "error listing backups") } end() diff --git a/internal/cmd/branch/create.go b/internal/cmd/branch/create.go index cc83b25d..7ba201b0 100644 --- a/internal/cmd/branch/create.go +++ b/internal/cmd/branch/create.go @@ -63,6 +63,9 @@ func CreateCmd(cfg *config.Config) *cobra.Command { defer end() dbBranch, err := client.DatabaseBranches.Create(ctx, createReq) if err != nil { + if cmdutil.IsNotFoundError(err) { + return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(source), cmdutil.BoldBlue(cfg.Organization)) + } return err } diff --git a/internal/cmd/branch/delete.go b/internal/cmd/branch/delete.go index 6487abe6..47ffb37b 100644 --- a/internal/cmd/branch/delete.go +++ b/internal/cmd/branch/delete.go @@ -70,6 +70,9 @@ func DeleteCmd(cfg *config.Config) *cobra.Command { Branch: branch, }) if err != nil { + if cmdutil.IsNotFoundError(err) { + return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(source)) + } return err } diff --git a/internal/cmd/branch/get.go b/internal/cmd/branch/get.go index ca097bd5..9e636b3e 100644 --- a/internal/cmd/branch/get.go +++ b/internal/cmd/branch/get.go @@ -51,6 +51,9 @@ func GetCmd(cfg *config.Config) *cobra.Command { Branch: branch, }) if err != nil { + if cmdutil.IsNotFoundError(err) { + return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(source)) + } return err } diff --git a/internal/cmd/branch/list.go b/internal/cmd/branch/list.go index c010c272..956fcf90 100644 --- a/internal/cmd/branch/list.go +++ b/internal/cmd/branch/list.go @@ -51,6 +51,9 @@ func ListCmd(cfg *config.Config) *cobra.Command { Database: database, }) if err != nil { + if cmdutil.IsNotFoundError(err) { + return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(database), cmdutil.BoldBlue(cfg.Organization)) + } return errors.Wrap(err, "error listing branches") } end() diff --git a/internal/cmd/branch/status.go b/internal/cmd/branch/status.go index f078711b..d04f6c76 100644 --- a/internal/cmd/branch/status.go +++ b/internal/cmd/branch/status.go @@ -37,6 +37,9 @@ func StatusCmd(cfg *config.Config) *cobra.Command { Branch: branch, }) if err != nil { + if cmdutil.IsNotFoundError(err) { + return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(source)) + } return err } diff --git a/internal/cmd/database/delete.go b/internal/cmd/database/delete.go index c097066d..c0572d19 100644 --- a/internal/cmd/database/delete.go +++ b/internal/cmd/database/delete.go @@ -69,7 +69,7 @@ func DeleteCmd(cfg *config.Config) *cobra.Command { }) if err != nil { if cmdutil.IsNotFoundError(err) { - return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(name), cmdutil.BoldBlue(cfg.Organization)) + return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(name), cmdutil.BoldBlue(cfg.Organization)) } return err } diff --git a/internal/cmd/database/get.go b/internal/cmd/database/get.go index c5882ab3..1c21c7d9 100644 --- a/internal/cmd/database/get.go +++ b/internal/cmd/database/get.go @@ -50,7 +50,7 @@ func GetCmd(cfg *config.Config) *cobra.Command { }) if err != nil { if cmdutil.IsNotFoundError(err) { - return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(name), cmdutil.BoldBlue(cfg.Organization)) + return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(name), cmdutil.BoldBlue(cfg.Organization)) } return err }