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

     1  package organization
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/organizations"
     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/models"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  	"code.cloudfoundry.org/cli/plugin/models"
    15  )
    16  
    17  const orgLimit = 0
    18  
    19  type ListOrgs struct {
    20  	ui              terminal.UI
    21  	config          coreconfig.Reader
    22  	orgRepo         organizations.OrganizationRepository
    23  	pluginOrgsModel *[]plugin_models.GetOrgs_Model
    24  	pluginCall      bool
    25  }
    26  
    27  func init() {
    28  	commandregistry.Register(&ListOrgs{})
    29  }
    30  
    31  func (cmd *ListOrgs) MetaData() commandregistry.CommandMetadata {
    32  	return commandregistry.CommandMetadata{
    33  		Name:        "orgs",
    34  		ShortName:   "o",
    35  		Description: T("List all orgs"),
    36  		Usage: []string{
    37  			"CF_NAME orgs",
    38  		},
    39  	}
    40  }
    41  
    42  func (cmd *ListOrgs) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    43  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    44  		T("No argument required"),
    45  		func() bool {
    46  			return len(fc.Args()) != 0
    47  		},
    48  	)
    49  
    50  	reqs := []requirements.Requirement{
    51  		usageReq,
    52  		requirementsFactory.NewLoginRequirement(),
    53  	}
    54  
    55  	return reqs, nil
    56  }
    57  
    58  func (cmd *ListOrgs) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    59  	cmd.ui = deps.UI
    60  	cmd.config = deps.Config
    61  	cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository()
    62  	cmd.pluginOrgsModel = deps.PluginModels.Organizations
    63  	cmd.pluginCall = pluginCall
    64  	return cmd
    65  }
    66  
    67  func (cmd ListOrgs) Execute(fc flags.FlagContext) error {
    68  	cmd.ui.Say(T("Getting orgs as {{.Username}}...\n",
    69  		map[string]interface{}{"Username": terminal.EntityNameColor(cmd.config.Username())}))
    70  
    71  	noOrgs := true
    72  	table := cmd.ui.Table([]string{T("name")})
    73  
    74  	orgs, err := cmd.orgRepo.ListOrgs(orgLimit)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	for _, org := range orgs {
    79  		table.Add(org.Name)
    80  		noOrgs = false
    81  	}
    82  
    83  	err = table.Print()
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	if err != nil {
    89  		return errors.New(T("Failed fetching orgs.\n{{.APIErr}}",
    90  			map[string]interface{}{"APIErr": err}))
    91  	}
    92  
    93  	if noOrgs {
    94  		cmd.ui.Say(T("No orgs found"))
    95  	}
    96  
    97  	if cmd.pluginCall {
    98  		cmd.populatePluginModel(orgs)
    99  	}
   100  	return nil
   101  }
   102  
   103  func (cmd *ListOrgs) populatePluginModel(orgs []models.Organization) {
   104  	for _, org := range orgs {
   105  		orgModel := plugin_models.GetOrgs_Model{}
   106  		orgModel.Name = org.Name
   107  		orgModel.Guid = org.GUID
   108  		*(cmd.pluginOrgsModel) = append(*(cmd.pluginOrgsModel), orgModel)
   109  	}
   110  }