github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/create/create_git_user.go (about)

     1  package create
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/olli-ai/jx/v2/pkg/cmd/create/options"
     7  
     8  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
     9  
    10  	"time"
    11  
    12  	"github.com/jenkins-x/jx-logging/pkg/log"
    13  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    14  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    15  	"github.com/olli-ai/jx/v2/pkg/kube"
    16  	"github.com/olli-ai/jx/v2/pkg/util"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  var (
    21  	create_git_user_long = templates.LongDesc(`
    22  		Creates a new user for a Git Server. Only supported for Gitea so far
    23  `)
    24  
    25  	create_git_user_example = templates.Examples(`
    26  		# Creates a new user in the local Gitea server
    27  		jx create git user -n local someUserName -p somepassword -e foo@bar.com
    28  	`)
    29  )
    30  
    31  // CreateGitUserOptions the command line options for the command
    32  type CreateGitUserOptions struct {
    33  	options.CreateOptions
    34  
    35  	ServerFlags opts.ServerFlags
    36  	Username    string
    37  	Password    string
    38  	ApiToken    string
    39  	Email       string
    40  	IsAdmin     bool
    41  }
    42  
    43  // NewCmdCreateGitUser creates a command
    44  func NewCmdCreateGitUser(commonOpts *opts.CommonOptions) *cobra.Command {
    45  	options := &CreateGitUserOptions{
    46  		CreateOptions: options.CreateOptions{
    47  			CommonOptions: commonOpts,
    48  		},
    49  	}
    50  
    51  	cmd := &cobra.Command{
    52  		Use:     "user [username]",
    53  		Short:   "Adds a new user to the Git server",
    54  		Long:    create_git_user_long,
    55  		Example: create_git_user_example,
    56  		Run: func(cmd *cobra.Command, args []string) {
    57  			options.Cmd = cmd
    58  			options.Args = args
    59  			err := options.Run()
    60  			helper.CheckErr(err)
    61  		},
    62  	}
    63  	options.ServerFlags.AddGitServerFlags(cmd)
    64  	cmd.Flags().StringVarP(&options.ApiToken, "api-token", "t", "", "The API Token for the user")
    65  	cmd.Flags().StringVarP(&options.Password, "password", "p", "", "The User password to try automatically create a new API Token")
    66  	cmd.Flags().StringVarP(&options.Email, "email", "e", "", "The User email address")
    67  	cmd.Flags().BoolVarP(&options.IsAdmin, "admin", "a", false, "Whether the user is an admin user")
    68  
    69  	return cmd
    70  }
    71  
    72  // Run implements the command
    73  func (o *CreateGitUserOptions) Run() error {
    74  	args := o.Args
    75  	if len(args) > 0 {
    76  		o.Username = args[0]
    77  	}
    78  	if len(args) > 1 {
    79  		o.ApiToken = args[1]
    80  	}
    81  	authConfigSvc, err := o.GitAuthConfigService()
    82  	if err != nil {
    83  		return err
    84  	}
    85  	config := authConfigSvc.Config()
    86  
    87  	server, err := o.FindGitServer(config, &o.ServerFlags)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	// TODO add the API thingy...
    93  	if o.Username == "" {
    94  		return fmt.Errorf("No Username specified")
    95  	}
    96  	if o.Password == "" && o.ApiToken == "" {
    97  		return fmt.Errorf("No password or APIToken specified")
    98  	}
    99  
   100  	client, ns, err := o.KubeClientAndNamespace()
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	deploymentName := "gitea-gitea"
   106  	log.Logger().Infof("Waiting for pods to be ready for deployment %s", deploymentName)
   107  
   108  	err = kube.WaitForDeploymentToBeReady(client, deploymentName, ns, 5*time.Minute)
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	pods, err := kube.GetDeploymentPods(client, deploymentName, ns)
   114  	if pods == nil || len(pods) == 0 {
   115  		return fmt.Errorf("No pod found for namespace %s with name %s", ns, deploymentName)
   116  	}
   117  
   118  	command := "/app/gitea/gitea admin create-user --admin --name " + o.Username + " --password " + o.Password
   119  	if o.Email != "" {
   120  		command += " --email " + o.Email
   121  	}
   122  	if o.IsAdmin {
   123  		command += " --admin"
   124  	}
   125  	// default to using the first pods found if more than one exists for the deployment
   126  	err = o.RunCommand("kubectl", "exec", "-t", pods[0].Name, "--", "/bin/sh", "-c", command)
   127  	if err != nil {
   128  		return nil
   129  	}
   130  
   131  	log.Logger().Infof("Created user %s API Token for Git server %s at %s",
   132  		util.ColorInfo(o.Username), util.ColorInfo(server.Name), util.ColorInfo(server.URL))
   133  	return nil
   134  }