Repository navigation
Expand file tree
/
Copy pathrest.go
More file actions
100 lines (82 loc) · 1.95 KB
/
Copy pathrest.go
File metadata and controls
100 lines (82 loc) · 1.95 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"io/ioutil"
"log"
"net/http"
"strings"
)
type grtCmd struct {
digest string
method string
protocol string
domain string
endpoint string
getVars map[string]string
rawForm string
body string
}
func NewGrtCmd(method, endpoint string) *grtCmd {
cmd := &grtCmd{
method: method,
protocol: "http",
domain: "gerrit.dev.returnpath.net",
endpoint: endpoint,
}
cmd.SetDigest()
return cmd
}
func (this *grtCmd) GetDigest() string {
var client http.Client
resp, err := client.Get(this.protocol + "://" + this.domain + this.endpoint)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest(this.method, this.protocol+"://"+this.domain+this.endpoint, nil)
if err != nil {
log.Fatal(err)
}
user := GetConfigValue("gerrit.user")
pass := GetConfigValue("gerrit.pass")
auth := GetAuthorization(user, pass, resp)
return GetAuthString(auth, req.URL, req.Method, 1)
}
func (this *grtCmd) SetDigest() {
this.digest = this.GetDigest()
}
func (this *grtCmd) Call() string {
var form string
switch {
case this.rawForm != "":
form = "?" + this.rawForm
case len(this.getVars) > 0:
form = "?"
for k, v := range this.getVars {
form += k + "=" + v + "&"
}
form = form[:len(form)-1] // Trim trailing ampersand
}
var client http.Client
resp, err := client.Get(this.protocol + "://" + this.domain + this.endpoint + form)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest(this.method, this.protocol+"://"+this.domain+this.endpoint+form, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add("Authorization", this.digest)
resp, err = client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// TODO: Need to check the return status.
// If, for example, you get Unauthorized the slice below will be out of bounds.
val := string(body)
val = val[strings.Index(string(val), "\n"):]
return val
}