Skip to content

Commit

Permalink
feat: worker delete (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
benPearce1 authored Nov 18, 2022
1 parent c251e1c commit 11c838c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pkg/cmd/target/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/spf13/cobra"
)

const resourceDescription = "deployment target"

type DeleteOptions struct {
*cmd.Dependencies
*shared.GetTargetsOptions
Expand Down Expand Up @@ -66,7 +68,7 @@ func NewCmdDelete(f factory.Factory) *cobra.Command {
}

if !skipConfirmation { // TODO NO_PROMPT env var or whatever we do there
return question.DeleteWithConfirmation(f.Ask, "deployment target", itemToDelete.Name, itemToDelete.GetID(), func() error {
return question.DeleteWithConfirmation(f.Ask, resourceDescription, itemToDelete.Name, itemToDelete.GetID(), func() error {
return delete(opts.Client, itemToDelete)
})
}
Expand All @@ -75,7 +77,7 @@ func NewCmdDelete(f factory.Factory) *cobra.Command {
},
}

question.RegisterConfirmDeletionFlag(cmd, &skipConfirmation, "deployment target")
question.RegisterConfirmDeletionFlag(cmd, &skipConfirmation, resourceDescription)

return cmd
}
Expand All @@ -86,7 +88,7 @@ func deleteRun(opts *DeleteOptions) error {
return err
}

return question.DeleteWithConfirmation(opts.Ask, "deployment target", itemToDelete.Name, itemToDelete.GetID(), func() error {
return question.DeleteWithConfirmation(opts.Ask, resourceDescription, itemToDelete.Name, itemToDelete.GetID(), func() error {
return delete(opts.Client, itemToDelete)
})
}
Expand Down
91 changes: 91 additions & 0 deletions pkg/cmd/worker/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package delete

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/cmd/worker/shared"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/question"
"github.com/OctopusDeploy/cli/pkg/question/selectors"
"github.com/OctopusDeploy/cli/pkg/util"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/spf13/cobra"
)

const resourceDescription = "worker"

type DeleteOptions struct {
*cmd.Dependencies
*shared.GetWorkersOptions
}

func NewDeleteOptions(dependencies *cmd.Dependencies) *DeleteOptions {
return &DeleteOptions{
Dependencies: dependencies,
GetWorkersOptions: shared.NewGetWorkersOptions(dependencies, nil),
}
}

func NewCmdDelete(f factory.Factory) *cobra.Command {
var skipConfirmation bool

cmd := &cobra.Command{
Use: "delete {<name> | <id>}",
Short: "Delete a worker in an instance of Octopus Deploy",
Long: "Delete a worker in an instance of Octopus Deploy",
Aliases: []string{"del", "rm", "remove"},
Example: fmt.Sprintf(heredoc.Doc(`
$ %s worker delete
$ %s worker rm
`), constants.ExecutableName, constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
deps := cmd.NewDependencies(f, c)

if util.Empty(args) {
opts := NewDeleteOptions(deps)
return deleteRun(opts)
}

idOrName := args[0]
opts := NewDeleteOptions(deps)
worker, err := opts.GetWorkerCallback(idOrName)
if err != nil {
return err
}

if worker == nil {
return fmt.Errorf("cannot find a worker with name or ID of '%s'", idOrName)
}

if !skipConfirmation { // TODO NO_PROMPT env var or whatever we do there
return question.DeleteWithConfirmation(f.Ask, resourceDescription, worker.Name, worker.GetID(), func() error {
return delete(opts.Client, worker)
})
}

return delete(opts.Client, worker)
},
}

question.RegisterConfirmDeletionFlag(cmd, &skipConfirmation, resourceDescription)

return cmd
}

func deleteRun(opts *DeleteOptions) error {
worker, err := selectors.Select(opts.Ask, "Select the worker you wish to delete:", opts.GetWorkersCallback, func(worker *machines.Worker) string { return worker.Name })
if err != nil {
return err
}

return question.DeleteWithConfirmation(opts.Ask, resourceDescription, worker.Name, worker.GetID(), func() error {
return delete(opts.Client, worker)
})
}

func delete(client *client.Client, itemToDelete *machines.Worker) error {
return client.Workers.DeleteByID(itemToDelete.GetID())
}
14 changes: 14 additions & 0 deletions pkg/cmd/worker/shared/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ import (
)

type GetWorkersCallback func() ([]*machines.Worker, error)
type GetWorkerCallback func(identifer string) (*machines.Worker, error)

type GetWorkersOptions struct {
GetWorkersCallback
GetWorkerCallback
}

func NewGetWorkersOptions(dependencies *cmd.Dependencies, filter func(*machines.Worker) bool) *GetWorkersOptions {
return &GetWorkersOptions{
GetWorkersCallback: func() ([]*machines.Worker, error) {
return GetWorkers(*dependencies.Client, filter)
},
GetWorkerCallback: func(identifier string) (*machines.Worker, error) {
return GetWorker(*dependencies.Client, identifier)
},
}
}

Expand Down Expand Up @@ -48,3 +53,12 @@ func GetWorkers(client client.Client, filter func(*machines.Worker) bool) ([]*ma

return workers, nil
}

func GetWorker(client client.Client, identifier string) (*machines.Worker, error) {
worker, err := client.Workers.GetByIdentifier(identifier)
if err != nil {
return nil, err
}

return worker, nil
}
2 changes: 2 additions & 0 deletions pkg/cmd/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package worker
import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
cmdDelete "github.com/OctopusDeploy/cli/pkg/cmd/worker/delete"
cmdList "github.com/OctopusDeploy/cli/pkg/cmd/worker/list"
listeningTentacle "github.com/OctopusDeploy/cli/pkg/cmd/worker/listening-tentacle"
pollingTentacle "github.com/OctopusDeploy/cli/pkg/cmd/worker/polling-tentacle"
Expand Down Expand Up @@ -32,6 +33,7 @@ func NewCmdWorker(f factory.Factory) *cobra.Command {
cmd.AddCommand(pollingTentacle.NewCmdPollingTentacle(f))
cmd.AddCommand(ssh.NewCmdSsh(f))
cmd.AddCommand(cmdList.NewCmdList(f))
cmd.AddCommand(cmdDelete.NewCmdDelete(f))
cmd.AddCommand(cmdView.NewCmdView(f))

return cmd
Expand Down

0 comments on commit 11c838c

Please sign in to comment.