github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/cf/commands/application/disable_ssh.go (about)

     1  package application
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/applications"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/models"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type DisableSSH 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(&DisableSSH{})
    26  }
    27  
    28  func (cmd *DisableSSH) MetaData() commandregistry.CommandMetadata {
    29  	return commandregistry.CommandMetadata{
    30  		Name:        "disable-ssh",
    31  		Description: T("Disable ssh for the application"),
    32  		Usage: []string{
    33  			T("CF_NAME disable-ssh APP_NAME"),
    34  		},
    35  	}
    36  }
    37  
    38  func (cmd *DisableSSH) 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("disable-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 *DisableSSH) 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 *DisableSSH) Execute(fc flags.FlagContext) error {
    63  	app := cmd.appReq.GetApplication()
    64  
    65  	if !app.EnableSSH {
    66  		cmd.ui.Say(fmt.Sprintf(T("ssh support is already disabled")+" for '%s'", app.Name))
    67  		return nil
    68  	}
    69  
    70  	cmd.ui.Say(T("Disabling ssh support for '{{.AppName}}'...",
    71  		map[string]interface{}{
    72  			"AppName": app.Name,
    73  		},
    74  	))
    75  	cmd.ui.Say("")
    76  
    77  	enable := false
    78  	updatedApp, err := cmd.appRepo.Update(app.GUID, models.AppParams{EnableSSH: &enable})
    79  	if err != nil {
    80  		return errors.New(T("Error disabling 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 disabled for ") + app.Name)
    87  	}
    88  	return nil
    89  }