user_create.go 1.0 KB
Newer Older
1 2 3
package cmd

import (
4
	"net/http"
5

6
	"github.com/jenkins-zh/jenkins-cli/client"
7 8 9
	"github.com/spf13/cobra"
)

LinuxSuRen's avatar
LinuxSuRen 已提交
10
// UserCreateOption is user create cmd option
11
type UserCreateOption struct {
12
	RoundTripper http.RoundTripper
13 14 15 16 17 18 19 20 21
}

var userCreateOption UserCreateOption

func init() {
	userCmd.AddCommand(userCreateCmd)
}

var userCreateCmd = &cobra.Command{
LinuxSuRen's avatar
LinuxSuRen 已提交
22
	Use:   "create <username> [password]",
23 24 25 26 27 28 29 30 31 32
	Short: "Create a user for your Jenkins",
	Long:  `Create a user for your Jenkins`,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			cmd.Help()
			return
		}

		username := args[0]

LinuxSuRen's avatar
LinuxSuRen 已提交
33 34 35 36 37
		var password string
		if len(args) >= 2 {
			password = args[1]
		}

38 39 40 41 42 43 44
		jclient := &client.UserClient{
			JenkinsCore: client.JenkinsCore{
				RoundTripper: userCreateOption.RoundTripper,
				Debug:        rootOptions.Debug,
			},
		}
		getCurrentJenkinsAndClient(&(jclient.JenkinsCore))
45

LinuxSuRen's avatar
LinuxSuRen 已提交
46
		if user, err := jclient.Create(username, password); err == nil {
47
			cmd.Println("create user success. Password is:", user.Password1)
48
		} else {
49
			cmd.PrintErrln(err)
50 51 52
		}
	},
}