-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (182 loc) · 5.22 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"encoding/json"
"flag"
"fmt"
"golang.org/x/net/context"
"golang.org/x/net/proxy"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"log"
"net/http"
"net/url"
"os"
"os/user"
"strconv"
"strings"
)
var tokFile string
var ctx context.Context
var px string
// todo copy your clientSecret here
const clientSecret = `{"installed":{"client_id":"-.apps.googleusercontent.com","project_id":"decisive-clover-","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"-","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}`
func getClient(config *oauth2.Config) *http.Client {
tok, err := tokenFromFile(tokFile)
if err != nil {
tok = getTokenFromWeb(config)
saveToken(tokFile, tok)
}
urlI := url.URL{}
urlProxy, _ := urlI.Parse(px)
transport := http.Transport{}
if strings.HasPrefix(px, "socks") {
l := strings.Index(px, "://")
dialSocksProxy, err := proxy.SOCKS5("tcp", px[l+3:], nil, proxy.Direct)
if err != nil {
fmt.Println("Error connecting to proxy:", err)
}
transport.Dial = dialSocksProxy.Dial
} else {
transport.Proxy = http.ProxyURL(urlProxy)
}
ctx = context.WithValue(context.Background(), oauth2.HTTPClient, &http.Client{
Transport: &transport,
})
return config.Client(ctx, tok)
}
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser then type the "+
"authorization code: \n%v\n", authURL)
var authCode string
if _, err := fmt.Scan(&authCode); err != nil {
log.Fatalf("Unable to read authorization code: %v", err)
}
tok, err := config.Exchange(context.TODO(), authCode)
if err != nil {
log.Fatalf("Unable to retrieve token from web: %v", err)
}
return tok
}
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return tok, err
}
func saveToken(path string, token *oauth2.Token) {
fmt.Printf("Saving credential file to: %s\n", path)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("Unable to cache oauth token: %v", err)
}
defer f.Close()
_ = json.NewEncoder(f).Encode(token)
}
func CheckIfError(err error) {
if err == nil {
return
}
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
os.Exit(1)
}
func Info(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
func LatestCommit(cIter object.CommitIter, name string, before *object.Commit) (*object.Commit, *object.Commit) {
c, err := cIter.Next()
if err != nil {
return before, nil
}
if c.Author.Name != name {
return LatestCommit(cIter, name, before)
} else if before != nil {
return before, c
} else {
return LatestCommit(cIter, name, c)
}
}
func GetRange(i int) string {
v := strconv.Itoa(i)
return "A" + v + ":F" + v
}
func GoogleSheetWrite(a, b, c, d string) {
config, err := google.ConfigFromJSON([]byte(clientSecret), sheets.SpreadsheetsScope)
CheckIfError(err)
client := getClient(config)
CheckIfError(err)
srv, err := sheets.NewService(
ctx,
option.WithHTTPClient(client),
option.WithScopes(sheets.SpreadsheetsScope),
)
CheckIfError(err)
spreadsheetId := "your Id aaaaRCcccUX5OvBVAWEA16tgVTowKI2QV0G"
ss := srv.Spreadsheets.Values
cells, err := ss.Get(spreadsheetId, "A1:F9999").Do()
CheckIfError(err)
if len(cells.Values) == 0 {
Info("No data found.")
} else {
Info("Sheet Found.")
}
l := len(cells.Values)
vr := &sheets.ValueRange{}
vr.Values = append(vr.Values, []interface{}{b, "git committed", c, 0, a, d})
r := ss.Append(
spreadsheetId,
GetRange(l+1),
vr,
)
_, err = r.ValueInputOption("USER_ENTERED").Do()
CheckIfError(err)
Info("%s\n%s\n%s\n%s", a, b, c, d)
}
func ReadGit(path, name string) (string, string, string) {
r, err := git.PlainOpen(path)
CheckIfError(err)
ref, err := r.Head()
CheckIfError(err)
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
CheckIfError(err)
c, cc := LatestCommit(cIter, name, nil)
t, _ := c.Tree()
tt, _ := cc.Tree()
ch, _ := t.Diff(tt)
a := fmt.Sprintf("Hash:%s; %d files committed.", c.Hash.String()[:8], ch.Len())
CheckIfError(err)
return strings.ReplaceAll(c.Message, "\n", ""), c.Author.When.Format("2006-01-02 15:04:05"), a
}
func usage() {
_, _ = fmt.Fprintf(os.Stderr, `dailyReport version: 1.0.0
Usage: dailyReport [-h help] [-D git_project_dir] [-U userName] [-P Proxy]
Options:
`)
flag.PrintDefaults()
}
func main() {
u, _ := user.Current()
tokFile = u.HomeDir + "/token.json"
Info("Auto Work report...")
var pa, usr string
h := false
flag.StringVar(&px, "P", "socks://127.0.0.1:8082", "set local http proxy addr")
flag.StringVar(&pa, "D", "D:/sd", "your git local path")
flag.StringVar(&usr, "U", "tom", "filter git user name")
flag.BoolVar(&h, "h", false, "show help")
flag.Parse()
if h {
usage()
} else {
a, b, c := ReadGit(pa, usr)
GoogleSheetWrite(usr, a, b, c)
}
}