-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (151 loc) · 6.32 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/gookit/color"
"github.com/spf13/viper"
)
type UserConfig struct {
Token string
GithubUser string
GitlabID string
site string
}
type GHUser struct {
Login string `json:"login"`
ID int `json:"id"`
Name string `json:"name"`
Blog string `json:"blog"`
Location string `json:"location"`
Bio string `json:"bio"`
PublicRepos int `json:"public_repos"`
Followers int `json:"followers"`
Following int `json:"following"`
Message string `json:"message"`
}
type GLUser struct {
Login string `json:"username"`
Name string `json:"name"`
Blog string `json:"website_url"`
Pronouns string `json:"pronouns"`
Location string `json:"location"`
Bio string `json:"bio"`
PublicRepos int `json:"public_repos"`
Followers int `json:"followers"`
Following int `json:"following"`
Message string `json:"message"`
}
func GetUserConfig() UserConfig {
var viper = viper.New()
viper.AddConfigPath(string(os.Getenv("HOME")) + "/.config/gufetch/")
viper.SetConfigName("config")
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
return UserConfig{viper.GetString("token.gitlab"), viper.GetString("defualt.githubUsername"), viper.GetString("defualt.gitlabID"), viper.GetString("defualt.site")}
}
func ApiRequest(url string) []byte {
resp, err := http.Get(url)
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
return body
}
func EmptyCheckFMT(string string) string {
if string == "" {
return "N/A"
} else {
return strings.TrimSpace(string)
}
}
func main() {
isGithub := flag.Bool("github", false, "select Github as your platform")
isGitlab := flag.Bool("gitlab", false, "select Gitlab as your platform")
isMono := flag.Bool("mono", false, "display with no colours")
flag.Parse()
additionalCLA := flag.Args()
var userConfig UserConfig = GetUserConfig()
if *isMono {
color.Disable()
}
if *isGithub {
if len(additionalCLA) < 1 && userConfig.GithubUser == "" {
fmt.Println("Please enter a Github username or set it in the config file")
return
}
var result GHUser
if len(additionalCLA) > 0 {
var fetch = ApiRequest("https://api.github.com/users/" + additionalCLA[0])
if err := json.Unmarshal(fetch, &result); err != nil {
fmt.Println("Can not unmarshal Github JSON")
}
} else {
var fetch = ApiRequest("https://api.github.com/users/" + userConfig.GithubUser)
if err := json.Unmarshal(fetch, &result); err != nil {
fmt.Println("Can not unmarshal Github JSON")
}
}
if result.Message == "Not Found" {
fmt.Println("Github username not found")
return
}
fmt.Println(` @@@@@@@@@@@@@@@ gufetch - Github`)
fmt.Println(` @@@@@@@@@@@@@@@@@@@@@ ~~~~~~~~~~~~~~~~`)
fmt.Println(` @@@@ @@@@&@@@@ @@@@ Username: `, result.Login)
fmt.Println(` @@@@@ @@@@@ Name: `, EmptyCheckFMT(result.Name))
fmt.Println(` @@@@@ @@@@@ Bio: `, EmptyCheckFMT(result.Bio))
fmt.Println(` @@@@@ @@@@@ Website: `, EmptyCheckFMT(result.Blog))
fmt.Println(` @@@@@ @@@@@ Location: `, EmptyCheckFMT(result.Location))
fmt.Println(` @@@@@@@ @@@@@@@ Followers: `, result.Followers)
fmt.Println(` @@@@ @@@@@ @@@@@@@@@@ Following: `, result.Following)
fmt.Println(` @@@ @@@@@@@@ Repos: `, result.PublicRepos)
fmt.Println(` @@@@@ @@@@@ `)
} else if *isGitlab {
if len(additionalCLA) < 1 && userConfig.GitlabID == "" {
fmt.Println("Please enter a Gitlab ID or set it in the config file")
return
}
if userConfig.Token == "" {
print("You need a token to fetch a Gitlab Profile!")
} else {
var result GLUser
if len(additionalCLA) > 0 {
var fetch = ApiRequest("https://gitlab.com/api/v4/users/" + additionalCLA[0] + "?private_token=" + userConfig.Token)
if err := json.Unmarshal(fetch, &result); err != nil {
fmt.Println("Can not unmarshal Gitlab JSON")
}
} else {
var fetch = ApiRequest("https://gitlab.com/api/v4/users/" + userConfig.GitlabID + "?private_token=" + userConfig.Token)
if err := json.Unmarshal(fetch, &result); err != nil {
fmt.Println("Can not unmarshal Gitlab JSON")
}
}
if result.Message == "404 User Not Found" {
fmt.Println("Gitlab ID not found")
return
}
color.Println(`<fg=fca326> (( ((</> gufetch - Gitlab`)
color.Println(`<fg=fca326> (((( ((((</> ~~~~~~~~~~~~~~~~~`)
color.Println(`<fg=fca326> (((((( (((((</> Username: `, EmptyCheckFMT(result.Login))
color.Println(`<fg=fca326> (((((((( (((((((</> Name: `, EmptyCheckFMT(result.Name))
color.Println(`<fg=fca326> //</><fg=FA6C25>///////</><fg=E04228>(((((((((((</><fg=FA6C25>///////</><fg=fca326>//</> Bio: `, EmptyCheckFMT(result.Bio))
color.Println(`<fg=fca326> ////</><fg=FA6C25>//////</><fg=E04228>((((((((((</><fg=FA6C25>//////</><fg=fca326>////</> Pronouns: `, EmptyCheckFMT(result.Pronouns))
color.Println(`<fg=fca326>//////</><fg=FA6C25>//////</><fg=E04228>((((((((</><fg=FA6C25>/////</><fg=fca326>//////</> Website: `, EmptyCheckFMT(result.Blog))
color.Println(`<fg=fca326> /////</><fg=FA6C25>/////</><fg=E04228>((((((</><fg=FA6C25>////</><fg=fca326>/////</> Location: `, EmptyCheckFMT(result.Location))
color.Println(`<fg=fca326> ////</><fg=FA6C25>///</><fg=E04228>(((((</><fg=FA6C25>///</><fg=fca326>////</> Followers: `, result.Followers)
color.Println(`<fg=fca326> //</><fg=FA6C25>//</><fg=E04228>(((</><fg=FA6C25>//</><fg=fca326>//</> Following: `, result.Following)
color.Println(`<fg=fca326> /</><fg=FA6C25>/</><fg=E04228>(</><fg=FA6C25>/</><fg=fca326>/</>`)
}
}
}