forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_buildpack.go
95 lines (77 loc) · 2.45 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
package buildpack
import (
"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/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")}
return commandregistry.CommandMetadata{
Name: "delete-buildpack",
Description: T("Delete a buildpack"),
Usage: []string{
T("CF_NAME delete-buildpack BUILDPACK [-f]"),
},
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 {
buildpackName := c.Args()[0]
force := c.Bool("f")
if !force {
answer := cmd.ui.ConfirmDelete("buildpack", buildpackName)
if !answer {
return nil
}
}
cmd.ui.Say(T("Deleting buildpack {{.BuildpackName}}...", map[string]interface{}{"BuildpackName": terminal.EntityNameColor(buildpackName)}))
buildpack, err := cmd.buildpackRepo.FindByName(buildpackName)
switch err.(type) {
case nil: //do nothing
case *errors.ModelNotFoundError:
cmd.ui.Ok()
cmd.ui.Warn(T("Buildpack {{.BuildpackName}} does not exist.", map[string]interface{}{"BuildpackName": buildpackName}))
return nil
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
}