From d70e833144b87a27d83f58070413c4a51c7b58bf Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Sat, 17 Aug 2024 02:48:56 +0800 Subject: [PATCH] feat: support size param when creating project --- cmd/branches.go | 18 ++---------------- cmd/projects.go | 43 +++++++++++++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/cmd/branches.go b/cmd/branches.go index 71aad9203..df2b48a8f 100644 --- a/cmd/branches.go +++ b/cmd/branches.go @@ -31,20 +31,6 @@ var ( branchRegion = utils.EnumFlag{ Allowed: flyRegions(), } - branchSize = utils.EnumFlag{ - Allowed: []string{ - string(api.Micro), - string(api.Small), - string(api.Medium), - string(api.Large), - string(api.Xlarge), - string(api.N2xlarge), - string(api.N4xlarge), - string(api.N8xlarge), - string(api.N12xlarge), - string(api.N16xlarge), - }, - } persistent bool branchCreateCmd = &cobra.Command{ @@ -62,7 +48,7 @@ var ( body.Region = &branchRegion.Value } if cmdFlags.Changed("size") { - body.DesiredInstanceSize = (*api.DesiredInstanceSize)(&branchSize.Value) + body.DesiredInstanceSize = (*api.DesiredInstanceSize)(&size.Value) } if cmdFlags.Changed("persistent") { body.Persistent = &persistent @@ -182,7 +168,7 @@ func init() { branchFlags.StringVar(&flags.ProjectRef, "project-ref", "", "Project ref of the Supabase project.") createFlags := branchCreateCmd.Flags() createFlags.Var(&branchRegion, "region", "Select a region to deploy the branch database.") - createFlags.Var(&branchSize, "size", "Select a desired instance size for the branch database.") + createFlags.Var(&size, "size", "Select a desired instance size for the branch database.") createFlags.BoolVar(&persistent, "persistent", false, "Whether to create a persistent branch.") branchesCmd.AddCommand(branchCreateCmd) branchesCmd.AddCommand(branchListCmd) diff --git a/cmd/projects.go b/cmd/projects.go index e2c1ddb45..08ec10dd8 100644 --- a/cmd/projects.go +++ b/cmd/projects.go @@ -30,12 +30,26 @@ var ( dbPassword string region = utils.EnumFlag{ - Allowed: make([]string, len(utils.RegionMap)), + Allowed: awsRegions(), } plan = utils.EnumFlag{ Allowed: []string{string(api.V1CreateProjectBodyPlanFree), string(api.V1CreateProjectBodyPlanPro)}, Value: string(api.V1CreateProjectBodyPlanFree), } + size = utils.EnumFlag{ + Allowed: []string{ + string(api.Micro), + string(api.Small), + string(api.Medium), + string(api.Large), + string(api.Xlarge), + string(api.N2xlarge), + string(api.N4xlarge), + string(api.N8xlarge), + string(api.N12xlarge), + string(api.N16xlarge), + }, + } projectsCreateCmd = &cobra.Command{ Use: "create [project name]", @@ -55,12 +69,16 @@ var ( if len(args) > 0 { projectName = args[0] } - return create.Run(cmd.Context(), api.V1CreateProjectBody{ + body := api.V1CreateProjectBody{ Name: projectName, OrganizationId: orgId, DbPass: dbPassword, Region: api.V1CreateProjectBodyRegion(region.Value), - }, afero.NewOsFs()) + } + if cmd.Flags().Changed("size") { + body.DesiredInstanceSize = (*api.DesiredInstanceSize)(&size.Value) + } + return create.Run(cmd.Context(), body, afero.NewOsFs()) }, } @@ -108,13 +126,6 @@ var ( ) func init() { - // Setup enum flags - i := 0 - for k := range utils.RegionMap { - region.Allowed[i] = k - i++ - } - sort.Strings(region.Allowed) // Add flags to cobra command createFlags := projectsCreateCmd.Flags() createFlags.BoolVarP(&interactive, "interactive", "i", true, "Enables interactive mode.") @@ -124,6 +135,7 @@ func init() { createFlags.Var(®ion, "region", "Select a region close to you for the best performance.") createFlags.Var(&plan, "plan", "Select a plan that suits your needs.") cobra.CheckErr(createFlags.MarkHidden("plan")) + createFlags.Var(&size, "size", "Select a desired instance size for your project.") cobra.CheckErr(viper.BindPFlag("DB_PASSWORD", createFlags.Lookup("db-password"))) apiKeysFlags := projectsApiKeysCmd.Flags() @@ -136,3 +148,14 @@ func init() { projectsCmd.AddCommand(projectsApiKeysCmd) rootCmd.AddCommand(projectsCmd) } + +func awsRegions() []string { + result := make([]string, len(utils.RegionMap)) + i := 0 + for k := range utils.RegionMap { + result[i] = k + i++ + } + sort.Strings(result) + return result +}