github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/buildpack/delete_buildpack.go (about) 1 package buildpack 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/cf/api" 7 "code.cloudfoundry.org/cli/cf/commandregistry" 8 "code.cloudfoundry.org/cli/cf/errors" 9 "code.cloudfoundry.org/cli/cf/flags" 10 . "code.cloudfoundry.org/cli/cf/i18n" 11 "code.cloudfoundry.org/cli/cf/models" 12 "code.cloudfoundry.org/cli/cf/requirements" 13 "code.cloudfoundry.org/cli/cf/terminal" 14 ) 15 16 type DeleteBuildpack struct { 17 ui terminal.UI 18 buildpackRepo api.BuildpackRepository 19 } 20 21 func init() { 22 commandregistry.Register(&DeleteBuildpack{}) 23 } 24 25 func (cmd *DeleteBuildpack) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 26 cmd.ui = deps.UI 27 cmd.buildpackRepo = deps.RepoLocator.GetBuildpackRepository() 28 return cmd 29 } 30 31 func (cmd *DeleteBuildpack) MetaData() commandregistry.CommandMetadata { 32 fs := make(map[string]flags.FlagSet) 33 fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")} 34 fs["s"] = &flags.StringFlag{ShortName: "s", Usage: T("Specify stack to disambiguate buildpacks with the same name")} 35 36 return commandregistry.CommandMetadata{ 37 Name: "delete-buildpack", 38 Description: T("Delete a buildpack"), 39 Usage: []string{ 40 T("CF_NAME delete-buildpack BUILDPACK [-f] [-s STACK]"), 41 }, 42 Flags: fs, 43 } 44 } 45 46 func (cmd *DeleteBuildpack) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 47 usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd), "", 48 func() bool { 49 return len(fc.Args()) != 1 50 }, 51 ) 52 53 loginReq := requirementsFactory.NewLoginRequirement() 54 55 reqs := []requirements.Requirement{ 56 usageReq, 57 loginReq, 58 } 59 60 return reqs, nil 61 } 62 63 func (cmd *DeleteBuildpack) Execute(c flags.FlagContext) error { 64 var ( 65 err error 66 buildpack models.Buildpack 67 ) 68 69 buildpackName := c.Args()[0] 70 stack := c.String("s") 71 72 force := c.Bool("f") 73 74 if !force { 75 answer := cmd.ui.ConfirmDelete("buildpack", buildpackName) 76 if !answer { 77 return nil 78 } 79 } 80 81 if stack == "" { 82 buildpack, err = cmd.buildpackRepo.FindByName(buildpackName) 83 cmd.ui.Say(T("Deleting buildpack {{.BuildpackName}}...", map[string]interface{}{"BuildpackName": terminal.EntityNameColor(buildpackName)})) 84 } else { 85 buildpack, err = cmd.buildpackRepo.FindByNameAndStack(buildpackName, stack) 86 cmd.ui.Say(T("Deleting buildpack {{.BuildpackName}} with stack {{.Stack}}...", map[string]interface{}{"BuildpackName": terminal.EntityNameColor(buildpackName), "Stack": terminal.EntityNameColor(stack)})) 87 } 88 89 switch err.(type) { 90 case nil: //do nothing 91 case *errors.ModelNotFoundError: 92 cmd.ui.Ok() 93 if stack == "" { 94 cmd.ui.Warn(T("Buildpack {{.BuildpackName}} does not exist.", map[string]interface{}{"BuildpackName": buildpackName})) 95 } else { 96 cmd.ui.Warn(T("Buildpack '{{.BuildpackName}}' with stack '{{.Stack}}' not found.", map[string]interface{}{"BuildpackName": buildpackName, "Stack": stack})) 97 } 98 return nil 99 case *errors.AmbiguousModelError: 100 var err2 error 101 buildpack, err2 = cmd.buildpackRepo.FindByNameWithNilStack(buildpackName) 102 if err2 != nil { 103 return fmt.Errorf("%s Specify the stack (using -s) to disambiguate.", err.Error()) 104 } 105 default: 106 return err 107 108 } 109 110 err = cmd.buildpackRepo.Delete(buildpack.GUID) 111 if err != nil { 112 return errors.New(T("Error deleting buildpack {{.Name}}\n{{.Error}}", map[string]interface{}{ 113 "Name": terminal.EntityNameColor(buildpack.Name), 114 "Error": err.Error(), 115 })) 116 } 117 118 cmd.ui.Ok() 119 return nil 120 }