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

     1  package domain
     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 CreateDomain struct {
    16  	ui         terminal.UI
    17  	config     coreconfig.Reader
    18  	domainRepo api.DomainRepository
    19  	orgReq     requirements.OrganizationRequirement
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&CreateDomain{})
    24  }
    25  
    26  func (cmd *CreateDomain) MetaData() commandregistry.CommandMetadata {
    27  	return commandregistry.CommandMetadata{
    28  		Name:        "create-domain",
    29  		Description: T("Create a domain in an org for later use"),
    30  		Usage: []string{
    31  			T("CF_NAME create-domain ORG DOMAIN"),
    32  		},
    33  	}
    34  }
    35  
    36  func (cmd *CreateDomain) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    37  	if len(fc.Args()) != 2 {
    38  		cmd.ui.Failed(T("Incorrect Usage. Requires org_name, domain_name as arguments\n\n") + commandregistry.Commands.CommandUsage("create-domain"))
    39  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    40  	}
    41  
    42  	cmd.orgReq = requirementsFactory.NewOrganizationRequirement(fc.Args()[0])
    43  
    44  	reqs := []requirements.Requirement{
    45  		requirementsFactory.NewLoginRequirement(),
    46  		cmd.orgReq,
    47  	}
    48  
    49  	return reqs, nil
    50  }
    51  
    52  func (cmd *CreateDomain) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    53  	cmd.ui = deps.UI
    54  	cmd.config = deps.Config
    55  	cmd.domainRepo = deps.RepoLocator.GetDomainRepository()
    56  	return cmd
    57  }
    58  
    59  func (cmd *CreateDomain) Execute(c flags.FlagContext) error {
    60  	domainName := c.Args()[1]
    61  	owningOrg := cmd.orgReq.GetOrganization()
    62  
    63  	cmd.ui.Say(T("Creating domain {{.DomainName}} for org {{.OrgName}} as {{.Username}}...",
    64  		map[string]interface{}{
    65  			"DomainName": terminal.EntityNameColor(domainName),
    66  			"OrgName":    terminal.EntityNameColor(owningOrg.Name),
    67  			"Username":   terminal.EntityNameColor(cmd.config.Username())}))
    68  
    69  	_, err := cmd.domainRepo.Create(domainName, owningOrg.GUID)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	cmd.ui.Ok()
    75  	return nil
    76  }