github.com/jfrog/jfrog-cli-core@v1.12.1/artifactory/commands/usersmanagement/userscreate.go (about)

     1  package usersmanagement
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/jfrog/jfrog-cli-core/artifactory/utils"
     7  	"github.com/jfrog/jfrog-cli-core/utils/config"
     8  	"github.com/jfrog/jfrog-client-go/artifactory/services"
     9  	"github.com/jfrog/jfrog-client-go/utils/log"
    10  )
    11  
    12  type UsersCreateCommand struct {
    13  	serverDetails   *config.ServerDetails
    14  	users           []services.User
    15  	usersGroups     []string
    16  	replaceIfExists bool
    17  }
    18  
    19  func NewUsersCreateCommand() *UsersCreateCommand {
    20  	return &UsersCreateCommand{}
    21  }
    22  
    23  func (ucc *UsersCreateCommand) SetUsers(users []services.User) *UsersCreateCommand {
    24  	ucc.users = users
    25  	return ucc
    26  }
    27  
    28  func (ucc *UsersCreateCommand) Users() []services.User {
    29  	return ucc.users
    30  }
    31  
    32  func (ucc *UsersCreateCommand) ServerDetails() (*config.ServerDetails, error) {
    33  	return ucc.serverDetails, nil
    34  }
    35  
    36  func (ucc *UsersCreateCommand) SetServerDetails(serverDetails *config.ServerDetails) *UsersCreateCommand {
    37  	ucc.serverDetails = serverDetails
    38  	return ucc
    39  }
    40  
    41  func (ucc *UsersCreateCommand) SetUsersGroups(usersGroups []string) *UsersCreateCommand {
    42  	ucc.usersGroups = usersGroups
    43  	return ucc
    44  }
    45  
    46  func (ucc *UsersCreateCommand) UsersGroups() []string {
    47  	return ucc.usersGroups
    48  }
    49  
    50  func (ucc *UsersCreateCommand) SetReplaceIfExists(replaceIfExists bool) *UsersCreateCommand {
    51  	ucc.replaceIfExists = replaceIfExists
    52  	return ucc
    53  }
    54  
    55  func (ucc *UsersCreateCommand) ReplaceIfExists() bool {
    56  	return ucc.replaceIfExists
    57  }
    58  
    59  func (ucc *UsersCreateCommand) CommandName() string {
    60  	return "rt_users_create"
    61  }
    62  
    63  func (ucc *UsersCreateCommand) Run() error {
    64  	servicesManager, err := utils.CreateServiceManager(ucc.serverDetails, -1, false)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	for _, user := range ucc.users {
    70  		log.Info(fmt.Sprintf("Creating user %s...", user.Name))
    71  		user.Groups = ucc.usersGroups
    72  		params := new(services.UserParams)
    73  		params.UserDetails = user
    74  		params.ReplaceIfExists = ucc.ReplaceIfExists()
    75  		err = servicesManager.CreateUser(*params)
    76  		if err != nil {
    77  			break
    78  		}
    79  	}
    80  	return err
    81  }