-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
user_token.go
56 lines (47 loc) · 1.27 KB
/
user_token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package cmd
import (
"net/http"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra"
)
// UserTokenOption represents a user token cmd option
type UserTokenOption struct {
Generate bool
Name string
RoundTripper http.RoundTripper
}
var userTokenOption UserTokenOption
func init() {
userCmd.AddCommand(userTokenCmd)
userTokenCmd.Flags().BoolVarP(&userTokenOption.Generate, "generate", "g", false, "Generate the token")
userTokenCmd.Flags().StringVarP(&userTokenOption.Name, "name", "n", "", "Name of the token")
}
var userTokenCmd = &cobra.Command{
Use: "token -g",
Short: "Token the user of your Jenkins",
Long: `Token the user of your Jenkins`,
Run: func(cmd *cobra.Command, _ []string) {
if !userTokenOption.Generate {
cmd.Help()
return
}
jclient := &client.UserClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: userTokenOption.RoundTripper,
Debug: rootOptions.Debug,
},
}
getCurrentJenkinsAndClient(&(jclient.JenkinsCore))
tokenName := userTokenOption.Name
if status, err := jclient.CreateToken(tokenName); err == nil {
var data []byte
if data, err = userOption.Output(status); err == nil {
cmd.Println(string(data))
} else {
cmd.PrintErrln(err)
}
} else {
cmd.PrintErrln(err)
}
},
}