-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgct.go
More file actions
110 lines (93 loc) · 1.99 KB
/
gct.go
File metadata and controls
110 lines (93 loc) · 1.99 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
101
102
103
104
105
106
107
108
109
110
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
)
type User struct {
Login string
Id int
Avatar_url string
Name string
Email string
}
const fileName = "users.json"
var users []User
func main() {
load()
login := os.Args[1]
user := findUser(login)
if user == nil {
fmt.Println("Bad user")
return
}
setGitConfig("user.name", user.Name)
setGitConfig("user.email", user.Email)
}
func load() {
file, _ := os.Open(fileName)
defer file.Close()
decoder := json.NewDecoder(file)
decoder.Decode(&users)
}
func findUser(login string) *User {
user := lUser(login)
if user == nil {
fmt.Println("Can't find user on local data.")
user = dUser(login)
}
return user
}
func lUser(login string) *User {
for i := 0; i < len(users); i++ {
if strings.EqualFold(login, users[i].Login) {
return &users[i]
}
}
return nil
}
func dUser(login string) (user *User) {
request := "https://api.github.com/users/" + login
fmt.Println("Request " + request)
resp, err := http.Get(request)
if err != nil {
fmt.Println("Can't request the user. Maybe without internet.")
return nil
}
// calls 'resp.Body.Close()' after the ending of this funcion
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println(string(body))
err = json.Unmarshal(body, &user)
if err != nil {
fmt.Println("Can't read the json")
return nil
}
go save(user)
return user
}
func save(user *User) {
users = append(users, *user)
file, _ := os.Create(fileName)
defer file.Close()
enc := json.NewEncoder(file)
enc.Encode(&users)
}
func setGitConfig(config string, value string) {
if len(value) > 0 {
fmt.Println(value)
err := exec.Command("git", "config", config, strconv.Quote(value)).Run()
if err != nil {
fmt.Printf("Can't change property %s\n", config)
fmt.Println(err)
}
} else {
exec.Command("git", "config", "--unset", config).Run()
fmt.Printf("Can't set %s with a empty value.\n", config)
}
}