(f factory.Factory)
| 15 | ) |
| 16 | |
| 17 | func NewCmdDelete(f factory.Factory) *cobra.Command { |
| 18 | var skipConfirmation bool |
| 19 | cmd := &cobra.Command{ |
| 20 | Use: "delete {<name> | <id>}", |
| 21 | Short: "Delete an environment", |
| 22 | Long: "Delete an environment in Octopus Deploy", |
| 23 | Aliases: []string{"del", "rm", "remove"}, |
| 24 | Example: heredoc.Docf(` |
| 25 | %[1]s environment delete |
| 26 | %[1]s environment rm |
| 27 | `, constants.ExecutableName), |
| 28 | RunE: func(cmd *cobra.Command, args []string) error { |
| 29 | if len(args) == 0 { |
| 30 | return deleteRun(f, cmd) |
| 31 | } |
| 32 | |
| 33 | itemIDOrName := args[0] |
| 34 | |
| 35 | client, err := f.GetSpacedClient(apiclient.NewRequester(cmd)) |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | |
| 40 | // SDK doesn't have accounts.GetByIDOrName so we emulate it here |
| 41 | foundEnvironments, err := client.Environments.Get(environments.EnvironmentsQuery{ |
| 42 | // TODO we can't lookup by ID here because the server will AND it with the ItemName and produce no results |
| 43 | PartialName: itemIDOrName, |
| 44 | }) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | // need exact match |
| 49 | var itemToDelete *environments.Environment |
| 50 | for _, item := range foundEnvironments.Items { |
| 51 | if item.Name == itemIDOrName { |
| 52 | itemToDelete = item |
| 53 | break |
| 54 | } |
| 55 | } |
| 56 | if itemToDelete == nil { |
| 57 | return fmt.Errorf("cannot find an environment with name or ID of '%s'", itemIDOrName) |
| 58 | } |
| 59 | |
| 60 | if !skipConfirmation { // TODO NO_PROMPT env var or whatever we do there |
| 61 | return question.DeleteWithConfirmation(f.Ask, "environment", itemToDelete.Name, itemToDelete.GetID(), func() error { |
| 62 | return delete(client, itemToDelete) |
| 63 | }) |
| 64 | } |
| 65 | |
| 66 | return delete(client, itemToDelete) |
| 67 | }, |
| 68 | } |
| 69 | |
| 70 | question.RegisterConfirmDeletionFlag(cmd, &skipConfirmation, "environment") |
| 71 | |
| 72 | return cmd |
| 73 | } |
| 74 |
nothing calls this directly
no test coverage detected