-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_buildpack.go
120 lines (100 loc) · 3.43 KB
/
delete_buildpack.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package buildpack
import (
"fmt"
"code.cloudfoundry.org/cli/cf/api"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/cli/cf/flags"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/terminal"
)
type DeleteBuildpack struct {
ui terminal.UI
buildpackRepo api.BuildpackRepository
}
func init() {
commandregistry.Register(&DeleteBuildpack{})
}
func (cmd *DeleteBuildpack) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.buildpackRepo = deps.RepoLocator.GetBuildpackRepository()
return cmd
}
func (cmd *DeleteBuildpack) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
fs["s"] = &flags.StringFlag{ShortName: "s", Usage: T("Specify stack to disambiguate buildpacks with the same name")}
return commandregistry.CommandMetadata{
Name: "delete-buildpack",
Description: T("Delete a buildpack"),
Usage: []string{
T("CF_NAME delete-buildpack BUILDPACK [-f] [-s STACK]"),
},
Flags: fs,
}
}
func (cmd *DeleteBuildpack) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd), "",
func() bool {
return len(fc.Args()) != 1
},
)
loginReq := requirementsFactory.NewLoginRequirement()
reqs := []requirements.Requirement{
usageReq,
loginReq,
}
return reqs, nil
}
func (cmd *DeleteBuildpack) Execute(c flags.FlagContext) error {
var (
err error
buildpack models.Buildpack
)
buildpackName := c.Args()[0]
stack := c.String("s")
force := c.Bool("f")
if !force {
answer := cmd.ui.ConfirmDelete("buildpack", buildpackName)
if !answer {
return nil
}
}
if stack == "" {
buildpack, err = cmd.buildpackRepo.FindByName(buildpackName)
cmd.ui.Say(T("Deleting buildpack {{.BuildpackName}}...", map[string]interface{}{"BuildpackName": terminal.EntityNameColor(buildpackName)}))
} else {
buildpack, err = cmd.buildpackRepo.FindByNameAndStack(buildpackName, stack)
cmd.ui.Say(T("Deleting buildpack {{.BuildpackName}} with stack {{.Stack}}...", map[string]interface{}{"BuildpackName": terminal.EntityNameColor(buildpackName), "Stack": terminal.EntityNameColor(stack)}))
}
switch err.(type) {
case nil: //do nothing
case *errors.ModelNotFoundError:
cmd.ui.Ok()
if stack == "" {
cmd.ui.Warn(T("Buildpack {{.BuildpackName}} does not exist.", map[string]interface{}{"BuildpackName": buildpackName}))
} else {
cmd.ui.Warn(T("Buildpack {{.BuildpackName}} with stack {{.Stack}} not found.", map[string]interface{}{"BuildpackName": buildpackName, "Stack": stack}))
}
return nil
case *errors.AmbiguousModelError:
var err2 error
buildpack, err2 = cmd.buildpackRepo.FindByNameWithNilStack(buildpackName)
if err2 != nil {
return fmt.Errorf("%s Specify the stack (using -s) to disambiguate.", err.Error())
}
default:
return err
}
err = cmd.buildpackRepo.Delete(buildpack.GUID)
if err != nil {
return errors.New(T("Error deleting buildpack {{.Name}}\n{{.Error}}", map[string]interface{}{
"Name": terminal.EntityNameColor(buildpack.Name),
"Error": err.Error(),
}))
}
cmd.ui.Ok()
return nil
}