forked from jenkins-zh/jenkins-cli
/
crumbIssuer.go
73 lines (61 loc) · 1.45 KB
/
crumbIssuer.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
63
64
65
66
67
68
69
70
71
72
73
package cmd
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/spf13/cobra"
)
//curl -k -u $JENKINS_USER:$JENKINS_TOKEN $JENKINS_URL/crumbIssuer/api/json -s
// Start contains the command line options
type CrumbIssuerOptions struct {
Upload bool
}
func init() {
rootCmd.AddCommand(versionCmd)
}
var versionCmd = &cobra.Command{
Use: "crumb",
Short: "Print crumbIssuer of Jenkins",
Long: `Print crumbIssuer of Jenkins`,
Run: func(cmd *cobra.Command, args []string) {
},
}
type CrumbIssuer struct {
Crumb string `json:"crumb"`
CrumbRequestField string `json:"crumbRequestField"`
}
func getCrumb() (CrumbIssuer, *JenkinsServer) {
config := getCurrentJenkins()
jenkinsRoot := config.URL
api := fmt.Sprintf("%s/crumbIssuer/api/json", jenkinsRoot)
req, err := http.NewRequest("GET", api, nil)
if err != nil {
log.Fatal(err)
}
req.SetBasicAuth(config.UserName, config.Token)
var crumbIssuer CrumbIssuer
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
if response, err := client.Do(req); err == nil {
if data, err := ioutil.ReadAll(response.Body); err == nil {
if response.StatusCode == 200 {
json.Unmarshal(data, &crumbIssuer)
} else {
fmt.Println("get curmb error")
log.Fatal(string(data))
}
} else {
log.Fatal(err)
}
} else {
log.Fatal(err)
}
return crumbIssuer, config
}
func init() {
}