-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.go
181 lines (164 loc) · 4.66 KB
/
gitlab.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
)
var (
host *string
token *string
key *string
)
type gitlabkeys struct {
Id int
Name string
Title string
}
func gitlab(url string, datastring map[string]interface{}, token string) []gitlabkeys {
var (
req *http.Request
resp *http.Response
err error
result []gitlabkeys
)
if datastring != nil {
var jsonStr, err = json.Marshal(datastring)
if err != nil {
log.Fatalln(err)
}
req, err = http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
} else {
var jsonStr = []byte(``)
req, err = http.NewRequest("GET", url, bytes.NewBuffer(jsonStr))
}
req.Header.Set("Private-Token", token)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err = client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal([]byte(body), &result)
if err != nil {
panic(err)
}
return result
}
func getarg() bool {
if len(os.Args) == 1 {
fmt.Println("usage: %s Command [<args>]", os.Args[0])
fmt.Printf("<<Command>>\n")
fmt.Printf("list:\n-host \"gitlab basic hostname\"\n-token \"Gitlab Access Token\"\n")
fmt.Printf("search:\n-host \"gitlab basic hostname\"\n-token \"Gitlab Access Token\"\n-key \"key for finding in project names\"\n")
fmt.Printf("-----\n")
fmt.Printf("Examples:\n\n./myapp list -host \"gitlab.com\" -token \"myaccesstoken\"\n")
fmt.Printf("./myapp search -key \"myprojectname\" -host \"gitlab.com\" -token \"myaccesstoken\"\n\n")
fmt.Println("Have Fun :)")
return true
}
switch os.Args[1] {
case "list":
Listprojects()
case "search":
Searchprojects()
case "deploykey":
Enable_deploy_key()
default:
fmt.Printf("%q is not valid command.\n", os.Args[1])
os.Exit(2)
}
return true
}
func Listprojects() bool {
var projects []gitlabkeys
listcmd := flag.NewFlagSet("list", flag.ExitOnError)
host = listcmd.String("host", "", "Enter Gitlab Host")
token = listcmd.String("token", "", "Enter Gitlab AccessToken")
err := listcmd.Parse(os.Args[2:])
if err != nil {
log.Fatal(err)
}
if *host != "" && *token != "" {
url := string(*host) + "/api/v4/projects"
projects = gitlab(url, nil, *token)
for value := range projects {
fmt.Printf("repository with id %d and name :%s\n", projects[value].Id, projects[value].Name)
}
} else {
listcmd.PrintDefaults()
}
return true
}
func Searchprojects() string {
var projects []gitlabkeys
searchcmd := flag.NewFlagSet("search", flag.ExitOnError)
host = searchcmd.String("host", "", "Enter Gitlab Host")
token = searchcmd.String("token", "", "Enter Gitlab AccessToken")
key = searchcmd.String("key", "", "Enter what you'r looking for ;)")
err := searchcmd.Parse(os.Args[2:])
if err != nil {
log.Fatal(err)
}
if *key != "" && *host != "" && *token != "" {
url := string(*host) + "/api/v4/projects"
projects = gitlab(url, nil, *token)
for value := range projects {
if strings.Contains(*key, projects[value].Name) == true {
fmt.Printf("we found this one : id:%d\tname:%s\n", projects[value].Id, projects[value].Name)
}
}
} else {
searchcmd.PrintDefaults()
}
return ""
}
func Enable_deploy_key() string {
var projects []gitlabkeys
var key_id int
deploycmd := flag.NewFlagSet("deploy", flag.ExitOnError)
host = deploycmd.String("host", "", "Enter Gitlab Host")
token = deploycmd.String("token", "", "Enter Gitlab AccessToken")
key = deploycmd.String("deploykey", "", "Enter DeployKey ;)")
group := deploycmd.String("group", "", "Enter group for enable ;)")
err := deploycmd.Parse(os.Args[2:])
if err != nil {
log.Fatal(err)
}
if *key != "" && *host != "" && *token != "" && *group != "" {
url := string(*host) + "/api/v4/groups/" + *group + "/projects?page=2&per_page=100"
deploykeysurl := string(*host) + "/api/v4/deploy_keys"
deploykeys := gitlab(deploykeysurl, nil, *token)
projects = gitlab(url, nil, *token)
for deploykey := range deploykeys {
if strings.Contains(*key, deploykeys[deploykey].Title) {
key_id = deploykeys[deploykey].Id
}
}
if key_id != 0 {
for value := range projects {
jsondata := map[string]interface{}{"key": strconv.Itoa(projects[value].Id), "key_id": strconv.Itoa(key_id)}
enable_key_url := string(*host) + "/api/v4/projects/" + strconv.Itoa(projects[value].Id) + "/deploy_keys/" + strconv.Itoa(key_id) + "/enable"
_ = gitlab(enable_key_url, jsondata, *token)
fmt.Printf("we found this one : id:%d\tname:%s\n", projects[value].Id, projects[value].Name)
}
}
} else {
deploycmd.PrintDefaults()
}
return ""
}
func main() {
getarg()
}