-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
user_token.go
62 lines (53 loc) · 1.52 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
57
58
59
60
61
62
package cmd
import (
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"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
TargetUser string
RoundTripper http.RoundTripper
}
var userTokenOption UserTokenOption
func init() {
userCmd.AddCommand(userTokenCmd)
userTokenCmd.Flags().BoolVarP(&userTokenOption.Generate, "generate", "g", false,
i18n.T("Generate the token"))
userTokenCmd.Flags().StringVarP(&userTokenOption.Name, "name", "n", "",
i18n.T("Name of the token"))
userTokenCmd.Flags().StringVarP(&userTokenOption.TargetUser, "target-user", "", "",
i18n.T("The target user of the new token"))
}
var userTokenCmd = &cobra.Command{
Use: "token",
Short: i18n.T("Token the user of your Jenkins"),
Long: i18n.T("Token the user of your Jenkins"),
Example: `jcli user token -g`,
RunE: func(cmd *cobra.Command, _ []string) (err error) {
if !userTokenOption.Generate {
cmd.Help()
return
}
jclient := &client.UserClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: userTokenOption.RoundTripper,
Debug: rootOptions.Debug,
},
}
getCurrentJenkinsAndClientOrDie(&(jclient.JenkinsCore))
var token *client.Token
token, err = jclient.CreateToken(userTokenOption.TargetUser, userTokenOption.Name)
if err == nil {
var data []byte
data, err = userOption.Output(token)
if err == nil {
cmd.Println(string(data))
}
}
return
},
}