-
Notifications
You must be signed in to change notification settings - Fork 2
/
hosts.go
161 lines (148 loc) · 3.76 KB
/
hosts.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
package main
import (
"encoding/json"
"fmt"
"os"
)
// Hostcode
type HostCode int
const (
github HostCode = iota + 1
gitlab
gitea
)
// Host struct which will mapped accordingly
type Host struct {
Hosts map[string]HostCode
}
// MapHosts maps hosts to their respective hostcode
func (host *Host) MapHosts() {
host.Hosts = map[string]HostCode{
"github.com": github,
"gitlab.com": gitlab,
"gitea.com": gitea,
"github": github,
"gitlab": gitlab,
"gitea": gitea,
"gh": github,
"gl": gitlab,
"gt": gitea,
}
}
var (
hostcode HostCode
choice string
)
// Returns url according to host and host info
func (host *Host) generateURL(site string, arg []string) string {
var url string
if len(arg) == 1 {
switch host.Hosts[site] {
case github:
url = fmt.Sprintf("https://api.github.com/users/%s", arg[0])
case gitlab, gitea:
// url = "gitlab.com/api/users"
fmt.Fprintln(os.Stderr, "SupportError: gitfetch doesn't support gitlab and codeberg for now")
os.Exit(0)
}
choice = "user"
} else {
switch host.Hosts[site] {
case github:
url = fmt.Sprintf("https://api.github.com/repos/%s/%s", arg[0], arg[1])
case gitlab:
url = fmt.Sprintf("https://gitlab.com/api/v4/projects/%s%s%s?license=1", arg[0], "%2F", arg[1])
case gitea:
fmt.Fprintln(os.Stderr, "SupportError: gitfetch doesn't support codeberg for now")
os.Exit(0)
}
choice = "repo"
}
return url
}
// User struct to store the user json values
type User struct {
Github struct {
Username string `json:"login"`
Company string
Blog string
Location string
Email string
Bio string
Repositories int `json:"public_repos"`
Gists int `json:"public_gists"`
Followers int
Following int
Created string `json:"created_at"`
}
Gitlab struct {
Username string `json:"login"`
Company string
Blog string
Location string
Email string
Bio string
Repositories int `json:"public_repos"`
Gists int `json:"public_gists"`
Followers int
Following int
Created string `json:"created_at"`
}
Gitea struct{}
}
// Repository struct to store the repository json values
type Repository struct {
Github struct {
Name string `json:"full_name"`
Repository string `json:"html_url"`
Description string `json:"description"`
Created string `json:"created_at"`
Modified string `json:"updated_at"`
DefaultBranch string `json:"default_branch"`
Stars int `json:"stargazers_count"`
Forks int `json:"forks_count"`
Language string `json:"language"`
License struct {
Name string `json:"name"`
} `json:"license"`
}
Gitlab struct {
Name string `json:"path_with_namespace"`
Repository string `json:"web_url"`
Description string `json:"description"`
Created string `json:"created_at"`
Modified string `json:"last_activity_at"`
DefaultBranch string `json:"default_branch"`
Stars int `json:"star_count"`
Forks int `json:"forks_count"`
Language string `json:"language"`
License struct {
Name string `json:"name"`
} `json:"license"`
}
Gitea struct{}
}
// Request user info
func (user *User) Request(url string) {
jsonDump := Request(url)
switch hostcode {
case github:
json.Unmarshal(jsonDump, &user.Github)
case gitlab:
json.Unmarshal(jsonDump, &user.Gitlab)
case gitea:
json.Unmarshal(jsonDump, &user.Gitea)
}
}
// Request repository info
func (repo *Repository) Request(url string) {
jsonDump := Request(url)
switch hostcode {
case github:
json.Unmarshal(jsonDump, &repo.Github)
case gitlab:
json.Unmarshal(jsonDump, &repo.Gitlab)
case gitea:
json.Unmarshal(jsonDump, &repo.Gitea)
}
}