github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/service/unbind_service.go (about) 1 package service 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/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/flags" 10 . "code.cloudfoundry.org/cli/cf/i18n" 11 "code.cloudfoundry.org/cli/cf/requirements" 12 "code.cloudfoundry.org/cli/cf/terminal" 13 ) 14 15 type UnbindService struct { 16 ui terminal.UI 17 config coreconfig.Reader 18 serviceBindingRepo api.ServiceBindingRepository 19 appReq requirements.ApplicationRequirement 20 serviceInstanceReq requirements.ServiceInstanceRequirement 21 } 22 23 func init() { 24 commandregistry.Register(&UnbindService{}) 25 } 26 27 func (cmd *UnbindService) MetaData() commandregistry.CommandMetadata { 28 return commandregistry.CommandMetadata{ 29 Name: "unbind-service", 30 ShortName: "us", 31 Description: T("Unbind a service instance from an app"), 32 Usage: []string{ 33 T("CF_NAME unbind-service APP_NAME SERVICE_INSTANCE"), 34 }, 35 } 36 } 37 38 func (cmd *UnbindService) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 39 if len(fc.Args()) != 2 { 40 cmd.ui.Failed(T("Incorrect Usage. Requires APP SERVICE_INSTANCE as arguments\n\n") + commandregistry.Commands.CommandUsage("unbind-service")) 41 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2) 42 } 43 44 serviceName := fc.Args()[1] 45 46 cmd.appReq = requirementsFactory.NewApplicationRequirement(fc.Args()[0]) 47 cmd.serviceInstanceReq = requirementsFactory.NewServiceInstanceRequirement(serviceName) 48 49 reqs := []requirements.Requirement{ 50 requirementsFactory.NewLoginRequirement(), 51 cmd.appReq, 52 cmd.serviceInstanceReq, 53 } 54 return reqs, nil 55 } 56 57 func (cmd *UnbindService) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 58 cmd.ui = deps.UI 59 cmd.config = deps.Config 60 cmd.serviceBindingRepo = deps.RepoLocator.GetServiceBindingRepository() 61 return cmd 62 } 63 64 func (cmd *UnbindService) Execute(c flags.FlagContext) error { 65 app := cmd.appReq.GetApplication() 66 instance := cmd.serviceInstanceReq.GetServiceInstance() 67 68 cmd.ui.Say(T("Unbinding app {{.AppName}} from service {{.ServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", 69 map[string]interface{}{ 70 "AppName": terminal.EntityNameColor(app.Name), 71 "ServiceName": terminal.EntityNameColor(instance.Name), 72 "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), 73 "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), 74 "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), 75 })) 76 77 found, err := cmd.serviceBindingRepo.Delete(instance, app.GUID) 78 if err != nil { 79 return err 80 } 81 82 cmd.ui.Ok() 83 84 if !found { 85 cmd.ui.Warn(T("Binding between {{.InstanceName}} and {{.AppName}} did not exist", 86 map[string]interface{}{"InstanceName": instance.Name, "AppName": app.Name})) 87 } 88 return nil 89 }