-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdelete.go
More file actions
84 lines (69 loc) · 2.39 KB
/
delete.go
File metadata and controls
84 lines (69 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package delete
import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/apiclient"
"github.com/OctopusDeploy/cli/pkg/cmd/environment/helper"
"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/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments"
"github.com/spf13/cobra"
)
func NewCmdDelete(f factory.Factory) *cobra.Command {
var skipConfirmation bool
cmd := &cobra.Command{
Use: "delete {<name> | <id>}",
Short: "Delete an environment",
Long: "Delete an environment in Octopus Deploy",
Aliases: []string{"del", "rm", "remove"},
Example: heredoc.Docf(`
$ %[1]s environment delete
$ %[1]s environment rm
`, constants.ExecutableName),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return deleteRun(f, cmd)
}
itemIDOrName := args[0]
client, err := f.GetSpacedClient(apiclient.NewRequester(cmd))
if err != nil {
return err
}
itemToDelete, err := helper.GetByIDOrName(client.Environments, itemIDOrName)
if itemToDelete == nil {
return fmt.Errorf("cannot find an environment with name or ID of '%s'", itemIDOrName)
}
if !skipConfirmation { // TODO NO_PROMPT env var or whatever we do there
return question.DeleteWithConfirmation(f.Ask, "environment", itemToDelete.Name, itemToDelete.GetID(), func() error {
return delete(client, itemToDelete)
})
}
return delete(client, itemToDelete)
},
}
question.RegisterConfirmDeletionFlag(cmd, &skipConfirmation, "environment")
return cmd
}
func deleteRun(f factory.Factory, cmd *cobra.Command) error {
client, err := f.GetSpacedClient(apiclient.NewRequester(cmd))
if err != nil {
return err
}
existingItems, err := client.Environments.GetAll()
if err != nil {
return err
}
itemToDelete, err := selectors.ByName(f.Ask, existingItems, "Select the environment you wish to delete:")
if err != nil {
return err
}
return question.DeleteWithConfirmation(f.Ask, "environment", itemToDelete.Name, itemToDelete.GetID(), func() error {
return delete(client, itemToDelete)
})
}
func delete(client *client.Client, itemToDelete *environments.Environment) error {
return client.Environments.DeleteByID(itemToDelete.GetID())
}