-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
147 lines (131 loc) · 3.03 KB
/
helper.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bytes"
"encoding/json"
"fmt"
// "github.com/davecgh/go-spew/spew"
"log"
"net/http"
"strconv"
"unicode/utf8"
)
//return "" when fail, return gist id when upload success
func pushGist(gist Gist) string {
const gistURL string = "https://api.github.com/gists"
result := pushRequest(gist, gistURL)
return result
}
// https://gist.githubusercontent.com/anonymous/04fd2287b1912e26ec46/raw/my-redis-rc.yaml
func Short(s string, i int) string {
if len(s) < i {
return s
}
if utf8.ValidString(s[:i]) {
return s[:i]
}
return s[:i-1]
}
func getFileRequest(tag string, kube *Kube) {
var url string = "http://api.log4security.com:31819/kube/" + tag
response, err := http.Get(url)
if err != nil {
log.Fatal("post error: ", err)
}
defer response.Body.Close()
err = json.NewDecoder(response.Body).Decode(&kube)
//spew.Dump(kube)
if err != nil {
log.Fatal("Response json error: ", err)
}
}
func getRequestResponse(url string) {
_, err1 := http.Get(url)
if err1 != nil {
log.Fatal("post error: ", err1)
}
response, err := http.Get(url)
if err != nil {
log.Fatal("post error: ", err)
}
defer response.Body.Close()
//spew.Dump(response.Body)
dec := json.NewDecoder(response.Body)
// read open bracket
_, err = dec.Token()
if err != nil {
log.Fatal(err)
}
var buffer bytes.Buffer
for i := 0; i < 105; i++ {
if i == 0 {
buffer.WriteString("NAME")
} else if i == 47 {
buffer.WriteString("DESCRIPTION")
} else if i == 100 {
buffer.WriteString("STARS")
} else {
buffer.WriteString(" ")
}
}
fmt.Println(buffer.String())
var kube Kube
// while the array contains values
for dec.More() {
// decode an array value (Message)
err := dec.Decode(&kube)
if err != nil {
log.Fatal(err)
}
var buffer bytes.Buffer
for i := 0; i < 105; i++ {
if i == 0 {
s1 := Short(kube.Tag, 45)
i = i + len(s1) - 4
buffer.WriteString(s1)
} else if i == 47 {
s2 := Short(kube.Description, 51)
i = i + len(s2) - 11
buffer.WriteString(s2)
} else if i == 100 {
buffer.WriteString(strconv.Itoa(kube.Like))
} else {
buffer.WriteString(" ")
}
}
fmt.Println(buffer.String())
}
}
func getRequest(url string) {
response, err := http.Get(url)
if err != nil {
log.Fatal("post error: ", err)
}
defer response.Body.Close()
}
func pushRequest(gist interface{}, url string) string {
var obj map[string]interface{}
jsonData, err := json.Marshal(gist)
if err != nil {
log.Fatal("push gist marshel error: ", err)
}
payload := bytes.NewBuffer(jsonData)
respond, err := http.Post(url, "application/json", payload)
if err != nil {
log.Fatal("post error: ", err)
}
err = json.NewDecoder(respond.Body).Decode(&obj)
if err != nil {
log.Fatal("Response json error: ", err)
}
if str, ok := obj["id"].(string); ok {
return str
} else {
// log.Fatal("Response id is not a string error: ", ok)
return ""
}
}
func pushKube(kube Kube) string {
const kubeURL string = "http://api.log4security.com:31819/kube"
result := pushRequest(kube, kubeURL)
return result
}