Skip to content

Commit

Permalink
wip: start work autoapply command for enabling/disabling gated deploy…
Browse files Browse the repository at this point in the history
… requests
  • Loading branch information
no-itsbackpack committed Sep 1, 2022
1 parent 9747876 commit 0672633
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
62 changes: 62 additions & 0 deletions internal/cmd/deployrequest/autoapply.go
@@ -0,0 +1,62 @@
package deployrequest

import (
"fmt"
"strconv"

"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/printer"
"github.com/planetscale/planetscale-go/planetscale"

"github.com/spf13/cobra"
)

// AutoApplyCmd is the command for enabling/disabling auto applying a gated deploy requests.
func AutoApplyCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "auto-apply <database> <number> <option>",
Short: "Auto apply changes to a gated deploy request",
Args: cmdutil.RequiredArgs("database", "number", "option"),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
database := args[0]
number := args[1]

client, err := ch.Client()
if err != nil {
return err
}

n, err := strconv.ParseUint(number, 10, 64)
if err != nil {
return fmt.Errorf("the argument <number> is invalid: %s", err)
}

dr, err := client.DeployRequests.CancelDeploy(ctx, &planetscale.CancelDeployRequestRequest{
Organization: ch.Config.Organization,
Database: database,
Number: n,
})
if err != nil {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("deploy request '%s/%s' does not exist in organization %s",
printer.BoldBlue(database), printer.BoldBlue(number), printer.BoldBlue(ch.Config.Organization))
default:
return cmdutil.HandleError(err)
}
}

if ch.Printer.Format() == printer.Human {
ch.Printer.Printf("Successfully enabled auto-apply changes for '%s/%s'.\n",
printer.BoldBlue(database),
printer.BoldBlue(dr.Number))
return nil
}

return ch.Printer.PrintResource(toDeployRequest(dr))
},
}

return cmd
}
Empty file.
2 changes: 2 additions & 0 deletions internal/cmd/deployrequest/dr.go
Expand Up @@ -20,6 +20,8 @@ func DeployRequestCmd(ch *cmdutil.Helper) *cobra.Command {
cmd.PersistentFlags().StringVar(&ch.Config.Organization, "org", ch.Config.Organization, "The organization for the current user")
cmd.MarkPersistentFlagRequired("org") // nolint:errcheck

cmd.AddCommand(ApplyCmd(ch))
cmd.AddCommand(AutoApplyCmd(ch))
cmd.AddCommand(CancelCmd(ch))
cmd.AddCommand(CloseCmd(ch))
cmd.AddCommand(CreateCmd(ch))
Expand Down
8 changes: 8 additions & 0 deletions internal/mock/dr.go
Expand Up @@ -10,6 +10,9 @@ type DeployRequestsService struct {
ApplyFn func(context.Context, *ps.ApplyDeployRequestRequest) (*ps.DeployRequest, error)
ApplyFnInvoked bool

AutoApplyFn func(context.Context, *ps.AutoApplyDeployRequestRequest) (*ps.DeployRequest, error)
AutoApplyFnInvoked bool

CancelFn func(context.Context, *ps.CancelDeployRequestRequest) (*ps.DeployRequest, error)
CancelFnInvoked bool

Expand Down Expand Up @@ -46,6 +49,11 @@ func (d *DeployRequestsService) ApplyDeploy(ctx context.Context, req *ps.ApplyDe
return d.ApplyFn(ctx, req)
}

func (d *DeployRequestsService) AutoApplyDeploy(ctx context.Context, req *ps.AutoApplyDeployRequestRequest) (*ps.DeployRequest, error) {
d.AutoApplyFnInvoked = true
return d.AutoApplyFn(ctx, req)
}

func (d *DeployRequestsService) CancelDeploy(ctx context.Context, req *ps.CancelDeployRequestRequest) (*ps.DeployRequest, error) {
d.CancelFnInvoked = true
return d.CancelFn(ctx, req)
Expand Down

0 comments on commit 0672633

Please sign in to comment.