github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/cf/commands/application/enable_ssh.go (about) 1 package application 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/liamawhite/cli-with-i18n/cf/api/applications" 8 "github.com/liamawhite/cli-with-i18n/cf/commandregistry" 9 "github.com/liamawhite/cli-with-i18n/cf/configuration/coreconfig" 10 "github.com/liamawhite/cli-with-i18n/cf/flags" 11 . "github.com/liamawhite/cli-with-i18n/cf/i18n" 12 "github.com/liamawhite/cli-with-i18n/cf/models" 13 "github.com/liamawhite/cli-with-i18n/cf/requirements" 14 "github.com/liamawhite/cli-with-i18n/cf/terminal" 15 ) 16 17 type EnableSSH struct { 18 ui terminal.UI 19 config coreconfig.Reader 20 appReq requirements.ApplicationRequirement 21 appRepo applications.Repository 22 } 23 24 func init() { 25 commandregistry.Register(&EnableSSH{}) 26 } 27 28 func (cmd *EnableSSH) MetaData() commandregistry.CommandMetadata { 29 return commandregistry.CommandMetadata{ 30 Name: "enable-ssh", 31 Description: T("Enable ssh for the application"), 32 Usage: []string{ 33 T("CF_NAME enable-ssh APP_NAME"), 34 }, 35 } 36 } 37 38 func (cmd *EnableSSH) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 39 if len(fc.Args()) != 1 { 40 cmd.ui.Failed(T("Incorrect Usage. Requires APP_NAME as argument\n\n") + commandregistry.Commands.CommandUsage("enable-ssh")) 41 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 42 } 43 44 cmd.appReq = requirementsFactory.NewApplicationRequirement(fc.Args()[0]) 45 46 reqs := []requirements.Requirement{ 47 requirementsFactory.NewLoginRequirement(), 48 requirementsFactory.NewTargetedSpaceRequirement(), 49 cmd.appReq, 50 } 51 52 return reqs, nil 53 } 54 55 func (cmd *EnableSSH) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 56 cmd.ui = deps.UI 57 cmd.config = deps.Config 58 cmd.appRepo = deps.RepoLocator.GetApplicationRepository() 59 return cmd 60 } 61 62 func (cmd *EnableSSH) Execute(fc flags.FlagContext) error { 63 app := cmd.appReq.GetApplication() 64 65 if app.EnableSSH { 66 cmd.ui.Say(T("ssh support is already enabled for '{{.AppName}}'", map[string]interface{}{ 67 "AppName": app.Name, 68 })) 69 return nil 70 } 71 72 cmd.ui.Say(T("Enabling ssh support for '{{.AppName}}'...", map[string]interface{}{ 73 "AppName": app.Name, 74 })) 75 cmd.ui.Say("") 76 77 enable := true 78 updatedApp, err := cmd.appRepo.Update(app.GUID, models.AppParams{EnableSSH: &enable}) 79 if err != nil { 80 return errors.New(T("Error enabling ssh support for ") + app.Name + ": " + err.Error()) 81 } 82 83 if updatedApp.EnableSSH { 84 cmd.ui.Ok() 85 } else { 86 return errors.New(T("ssh support is not enabled for ") + app.Name) 87 } 88 return nil 89 }