github.com/loafoe/cli@v7.1.0+incompatible/cf/requirements/user.go (about)

     1  package requirements
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api"
     5  	"code.cloudfoundry.org/cli/cf/models"
     6  )
     7  
     8  //go:generate counterfeiter . UserRequirement
     9  
    10  type UserRequirement interface {
    11  	Requirement
    12  	GetUser() models.UserFields
    13  }
    14  
    15  type userAPIRequirement struct {
    16  	username   string
    17  	userRepo   api.UserRepository
    18  	clientRepo api.ClientRepository
    19  	wantGUID   bool
    20  	clientID   string
    21  
    22  	user models.UserFields
    23  }
    24  
    25  func NewClientRequirement(clientID string, clientRepo api.ClientRepository) *userAPIRequirement {
    26  	req := new(userAPIRequirement)
    27  	req.clientID = clientID
    28  	req.clientRepo = clientRepo
    29  	return req
    30  }
    31  
    32  func NewUserRequirement(
    33  	username string,
    34  	userRepo api.UserRepository,
    35  	wantGUID bool,
    36  ) *userAPIRequirement {
    37  	req := new(userAPIRequirement)
    38  	req.username = username
    39  	req.userRepo = userRepo
    40  	req.wantGUID = wantGUID
    41  
    42  	return req
    43  }
    44  
    45  func (req *userAPIRequirement) Execute() error {
    46  	if req.wantGUID {
    47  		var err error
    48  		req.user, err = req.userRepo.FindByUsername(req.username)
    49  		if err != nil {
    50  			return err
    51  		}
    52  	} else if req.clientID != "" {
    53  		var err error
    54  		_, err = req.clientRepo.ClientExists(req.clientID)
    55  		if err != nil {
    56  			return err
    57  		}
    58  		req.user = models.UserFields{GUID: req.clientID, Username: req.clientID}
    59  	} else {
    60  		req.user = models.UserFields{Username: req.username}
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  func (req *userAPIRequirement) GetUser() models.UserFields {
    67  	return req.user
    68  }