github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/securitygroup/running_security_groups.go (about)

     1  package securitygroup
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/securitygroups/defaults/running"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/flags"
     8  	. "code.cloudfoundry.org/cli/cf/i18n"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/terminal"
    11  )
    12  
    13  type listRunningSecurityGroups struct {
    14  	ui                       terminal.UI
    15  	runningSecurityGroupRepo running.SecurityGroupsRepo
    16  	configRepo               coreconfig.Reader
    17  }
    18  
    19  func init() {
    20  	commandregistry.Register(&listRunningSecurityGroups{})
    21  }
    22  
    23  func (cmd *listRunningSecurityGroups) MetaData() commandregistry.CommandMetadata {
    24  	return commandregistry.CommandMetadata{
    25  		Name:        "running-security-groups",
    26  		Description: T("List security groups in the set of security groups for running applications"),
    27  		Usage: []string{
    28  			"CF_NAME running-security-groups",
    29  		},
    30  	}
    31  }
    32  
    33  func (cmd *listRunningSecurityGroups) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    34  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    35  		T("No argument required"),
    36  		func() bool {
    37  			return len(fc.Args()) != 0
    38  		},
    39  	)
    40  
    41  	reqs := []requirements.Requirement{
    42  		usageReq,
    43  		requirementsFactory.NewLoginRequirement(),
    44  	}
    45  	return reqs, nil
    46  }
    47  
    48  func (cmd *listRunningSecurityGroups) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    49  	cmd.ui = deps.UI
    50  	cmd.configRepo = deps.Config
    51  	cmd.runningSecurityGroupRepo = deps.RepoLocator.GetRunningSecurityGroupsRepository()
    52  	return cmd
    53  }
    54  
    55  func (cmd *listRunningSecurityGroups) Execute(context flags.FlagContext) error {
    56  	cmd.ui.Say(T("Acquiring running security groups as '{{.username}}'", map[string]interface{}{
    57  		"username": terminal.EntityNameColor(cmd.configRepo.Username()),
    58  	}))
    59  
    60  	defaultSecurityGroupsFields, err := cmd.runningSecurityGroupRepo.List()
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	cmd.ui.Ok()
    66  	cmd.ui.Say("")
    67  
    68  	if len(defaultSecurityGroupsFields) > 0 {
    69  		for _, value := range defaultSecurityGroupsFields {
    70  			cmd.ui.Say(value.Name)
    71  		}
    72  	} else {
    73  		cmd.ui.Say(T("No running security groups set"))
    74  	}
    75  	return nil
    76  }