github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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  	wantGUID bool
    19  
    20  	user models.UserFields
    21  }
    22  
    23  func NewUserRequirement(
    24  	username string,
    25  	userRepo api.UserRepository,
    26  	wantGUID bool,
    27  ) *userAPIRequirement {
    28  	req := new(userAPIRequirement)
    29  	req.username = username
    30  	req.userRepo = userRepo
    31  	req.wantGUID = wantGUID
    32  
    33  	return req
    34  }
    35  
    36  func (req *userAPIRequirement) Execute() error {
    37  	if req.wantGUID {
    38  		var err error
    39  		req.user, err = req.userRepo.FindByUsername(req.username)
    40  		if err != nil {
    41  			return err
    42  		}
    43  	} else {
    44  		req.user = models.UserFields{Username: req.username}
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func (req *userAPIRequirement) GetUser() models.UserFields {
    51  	return req.user
    52  }