Skip to content

Commit

Permalink
feat: project delete (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
benPearce1 authored Oct 14, 2022
1 parent fa0d546 commit 38039c3
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/AlecAivazis/survey/v2 v2.3.5
github.com/MakeNowJust/heredoc/v2 v2.0.1
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.8.1
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.9.1
github.com/briandowns/spinner v1.19.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-multierror v1.1.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63n
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.8.1 h1:jaMlQAeI93w/BlwUf5LNXXlm0aRneroFKuV1BH0D89s=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.8.1/go.mod h1:XWqxyDUVElUlTaPqyCBblukpsHSnPcAKkAHgJgbsIAs=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.9.1 h1:rnG8B0t759J3en86aTSi9nZ/dv8VcNIz3s+iKWQxVrI=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.9.1/go.mod h1:XWqxyDUVElUlTaPqyCBblukpsHSnPcAKkAHgJgbsIAs=
github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh/a8E=
github.com/briandowns/spinner v1.19.0/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
10 changes: 3 additions & 7 deletions pkg/cmd/account/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

func NewCmdDelete(f factory.Factory) *cobra.Command {
var skipConfirmation bool
cmd := &cobra.Command{
Use: "delete {<name> | <id>}",
Short: "Delete an account in an instance of Octopus Deploy",
Expand All @@ -31,11 +32,6 @@ func NewCmdDelete(f factory.Factory) *cobra.Command {

itemIDOrName := args[0]

skipConfirmation, err := cmd.Flags().GetBool("confirm")
if err != nil {
return err
}

client, err := f.GetSpacedClient()
if err != nil {
return err
Expand Down Expand Up @@ -70,8 +66,8 @@ func NewCmdDelete(f factory.Factory) *cobra.Command {
return delete(client, itemToDelete)
},
}
// TODO confirm might want to be a global flag?
cmd.Flags().BoolP("confirm", "y", false, "Don't ask for confirmation before deleting the account.")

question.RegisterDeleteFlag(cmd, &skipConfirmation, "account")

return cmd
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/cmd/environment/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

func NewCmdDelete(f factory.Factory) *cobra.Command {
var skipConfirmation bool
cmd := &cobra.Command{
Use: "delete {<name> | <id>}",
Short: "Delete an environment in an instance of Octopus Deploy",
Expand All @@ -31,11 +32,6 @@ func NewCmdDelete(f factory.Factory) *cobra.Command {

itemIDOrName := args[0]

skipConfirmation, err := cmd.Flags().GetBool("confirm")
if err != nil {
return err
}

client, err := f.GetSpacedClient()
if err != nil {
return err
Expand Down Expand Up @@ -70,8 +66,8 @@ func NewCmdDelete(f factory.Factory) *cobra.Command {
return delete(client, itemToDelete)
},
}
// TODO confirm might want to be a global flag?
cmd.Flags().BoolP("confirm", "y", false, "Don't ask for confirmation before deleting the space.")

question.RegisterDeleteFlag(cmd, &skipConfirmation, "environment")

return cmd
}
Expand Down
94 changes: 94 additions & 0 deletions pkg/cmd/project/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package delete

import (
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/question"
"github.com/OctopusDeploy/cli/pkg/question/selectors"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects"
"github.com/spf13/cobra"
)

type DeleteOptions struct {
Client *client.Client
Ask question.Asker
NoPrompt bool
IdOrName string
*question.DeleteFlags
}

func NewCmdList(f factory.Factory) *cobra.Command {
deleteFlags := question.NewDeleteFlags()
cmd := &cobra.Command{
Use: "delete {<name> | <id> | <slug>}",
Short: "Delete projects in Octopus Deploy",
Long: "Delete projects in Octopus Deploy",
Aliases: []string{"del", "rm", "remove"},
Example: heredoc.Doc(`
$ octopus project delete
$ octopus project rm
`),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := f.GetSpacedClient()
if err != nil {
return err
}

opts := &DeleteOptions{
Client: client,
Ask: f.Ask,
NoPrompt: !f.IsPromptEnabled(),
IdOrName: args[0],
DeleteFlags: deleteFlags,
}

return deleteRun(opts)
},
}

question.RegisterDeleteFlag(cmd, &deleteFlags.Confirm.Value, "project")

return cmd
}

func deleteRun(opts *DeleteOptions) error {
if !opts.NoPrompt {
if err := PromptMissing(opts); err != nil {
return err
}
}

itemToDelete, err := opts.Client.Projects.GetByIdOrName(opts.IdOrName)
if err != nil {
return err
}

if opts.DeleteFlags.Confirm.Value {
return delete(opts.Client, itemToDelete)
} else {
return question.DeleteWithConfirmation(opts.Ask, "project", itemToDelete.Name, itemToDelete.ID, func() error {
return delete(opts.Client, itemToDelete)
})
}
}

func PromptMissing(opts *DeleteOptions) error {
if opts.IdOrName == "" {
existingProjects, err := opts.Client.Projects.GetAll()
if err != nil {
return err
}
itemToDelete, err := selectors.ByNameOrID(opts.Ask, existingProjects, "Select the project you wish to delete:")
if err != nil {
return err
}
opts.IdOrName = itemToDelete.GetID()
}

return nil
}

func delete(client *client.Client, project *projects.Project) error {
return client.Projects.DeleteByID(project.GetID())
}
2 changes: 2 additions & 0 deletions pkg/cmd/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package project
import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
cmdDelete "github.com/OctopusDeploy/cli/pkg/cmd/project/delete"
cmdList "github.com/OctopusDeploy/cli/pkg/cmd/project/list"
cmdView "github.com/OctopusDeploy/cli/pkg/cmd/project/view"
"github.com/OctopusDeploy/cli/pkg/constants"
Expand All @@ -28,6 +29,7 @@ func NewCmdProject(f factory.Factory) *cobra.Command {

cmd.AddCommand(cmdList.NewCmdList(f))
cmd.AddCommand(cmdView.NewCmdView(f))
cmd.AddCommand(cmdDelete.NewCmdList(f))

return cmd
}
10 changes: 3 additions & 7 deletions pkg/cmd/space/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

func NewCmdDelete(f factory.Factory) *cobra.Command {
var alreadyConfirmed bool
cmd := &cobra.Command{
Use: "delete {<name> | <id>}",
Short: "Delete a space in an instance of Octopus Deploy",
Expand All @@ -32,11 +33,6 @@ func NewCmdDelete(f factory.Factory) *cobra.Command {

itemIDOrName := args[0]

alreadyConfirmed, err := cmd.Flags().GetBool("confirm")
if err != nil {
return err
}

client, err := f.GetSystemClient()
if err != nil {
return err
Expand All @@ -59,8 +55,8 @@ func NewCmdDelete(f factory.Factory) *cobra.Command {
return nil
},
}
// TODO confirm might want to be a global flag?
cmd.Flags().BoolP("confirm", "y", false, "Don't ask for confirmation before deleting the space.")

question.RegisterDeleteFlag(cmd, &alreadyConfirmed, "space")

return cmd
}
Expand Down
19 changes: 18 additions & 1 deletion pkg/question/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@ package question

import (
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/OctopusDeploy/cli/pkg/output"
"github.com/OctopusDeploy/cli/pkg/util/flag"
"github.com/spf13/cobra"
)

const FlagConfirm = "confirm"

type DeleteFlags struct {
Confirm *flag.Flag[bool]
}

func NewDeleteFlags() *DeleteFlags {
return &DeleteFlags{
Confirm: flag.New[bool](FlagConfirm, false),
}
}

func RegisterDeleteFlag(cmd *cobra.Command, value *bool, resourceDescription string) {
cmd.Flags().BoolVarP(value, FlagConfirm, "y", false, fmt.Sprintf("Don't ask for confirmation before deleting the %s.", resourceDescription))
}

func DeleteWithConfirmation(ask Asker, itemType string, itemName string, itemID string, doDelete func() error) error {
var enteredName string
if err := ask(&survey.Input{
Expand Down

0 comments on commit 38039c3

Please sign in to comment.