From 036f9b446f5370b61662b1de8b3865e9c28bd5a5 Mon Sep 17 00:00:00 2001 From: S96EA Date: Mon, 29 Jan 2024 15:39:44 +0800 Subject: [PATCH] feat: provide interactive support for project deletion (#1699) * feat: support create project by config file * feat: support delete project using interactive mode * Revert "feat: support create project by config file" This reverts commit 0ca8e8452048b03520cf2f06d322ac4df30848eb. * chore: make delete command interactive by default * chore: reuse project ref prompt * fix: handle non-interactive shell * chore: invert conditional --------- Co-authored-by: owen Co-authored-by: Qiao Han Co-authored-by: Han Qiao --- cmd/link.go | 17 +++++++++-------- cmd/projects.go | 19 ++++++++++++++++--- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/cmd/link.go b/cmd/link.go index 18d409ce5..b136b8152 100644 --- a/cmd/link.go +++ b/cmd/link.go @@ -31,9 +31,8 @@ var ( RunE: func(cmd *cobra.Command, args []string) error { ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt) if len(projectRef) == 0 { - if err := PromptProjectRef(ctx); err != nil { - return err - } + title := "Which project do you want to link?" + cobra.CheckErr(PromptProjectRef(ctx, title)) } fsys := afero.NewOsFs() if err := link.PreRun(projectRef, fsys); err != nil { @@ -59,8 +58,7 @@ func init() { rootCmd.AddCommand(linkCmd) } -func PromptProjectRef(ctx context.Context) error { - title := "Which project do you want to link?" +func PromptProjectRef(ctx context.Context, title string) error { resp, err := utils.GetSupabase().GetProjectsWithResponse(ctx) if err != nil { return err @@ -70,13 +68,16 @@ func PromptProjectRef(ctx context.Context) error { } items := make([]utils.PromptItem, len(*resp.JSON200)) for i, project := range *resp.JSON200 { - items[i] = utils.PromptItem{Summary: project.Name, Details: project.Id} + items[i] = utils.PromptItem{ + Summary: project.Id, + Details: fmt.Sprintf("name: %s, org: %s, region: %s", project.Name, project.OrganizationId, project.Region), + } } choice, err := utils.PromptChoice(ctx, title, items) if err != nil { return err } - projectRef = choice.Details - fmt.Fprintln(os.Stderr, "Selected project ref:", projectRef) + projectRef = choice.Summary + fmt.Fprintln(os.Stderr, "Selected project:", projectRef) return nil } diff --git a/cmd/projects.go b/cmd/projects.go index 0352502f4..400c00c3d 100644 --- a/cmd/projects.go +++ b/cmd/projects.go @@ -18,6 +18,7 @@ import ( "github.com/supabase/cli/internal/utils" "github.com/supabase/cli/internal/utils/flags" "github.com/supabase/cli/pkg/api" + "golang.org/x/term" ) var ( @@ -87,12 +88,24 @@ var ( projectsDeleteCmd = &cobra.Command{ Use: "delete ", Short: "Delete a Supabase project", - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), PreRunE: func(cmd *cobra.Command, args []string) error { - return delete.PreRun(args[0]) + if !term.IsTerminal(int(os.Stdin.Fd())) { + return cobra.ExactArgs(1)(cmd, args) + } + return nil }, RunE: func(cmd *cobra.Command, args []string) error { - return delete.Run(cmd.Context(), args[0], afero.NewOsFs()) + if len(args) == 0 { + title := "Which project do you want to delete?" + cobra.CheckErr(PromptProjectRef(cmd.Context(), title)) + } else { + projectRef = args[0] + } + if err := delete.PreRun(projectRef); err != nil { + return err + } + return delete.Run(cmd.Context(), projectRef, afero.NewOsFs()) }, } )