Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: worker delete #147

Merged
merged 13 commits into from
Nov 18, 2022
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.16.0
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.16.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 @@ -50,6 +50,8 @@ github.com/OctopusDeploy/go-octopusdeploy/v2 v2.15.1 h1:AJ/UhXqHpFZOK2EtdSBvqTse
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.15.1/go.mod h1:2j9rwRfb5qUs9PEJ3W331W84kRaNge5bed4D7JR1ruU=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.16.0 h1:Ndr4YhSshyQT9rkOZ+o3xNAEz77/AY9klVOwULGFli4=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.16.0/go.mod h1:2j9rwRfb5qUs9PEJ3W331W84kRaNge5bed4D7JR1ruU=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.16.1 h1:F5LgixmKnsYBduOAScsmLyWtZpNuKkqJkafspzeYg6w=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.16.1/go.mod h1:2j9rwRfb5qUs9PEJ3W331W84kRaNge5bed4D7JR1ruU=
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
88 changes: 88 additions & 0 deletions pkg/cmd/worker/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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"
)

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, "deployment target", worker.Name, worker.GetID(), func() error {
benPearce1 marked this conversation as resolved.
Show resolved Hide resolved
return delete(opts.Client, worker)
})
}

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

question.RegisterConfirmDeletionFlag(cmd, &skipConfirmation, "deployment target")
benPearce1 marked this conversation as resolved.
Show resolved Hide resolved

return cmd
}

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

return question.DeleteWithConfirmation(opts.Ask, "worker", 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 @@ -31,6 +32,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))

return cmd
}