-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.go
110 lines (97 loc) · 2.4 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
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"text/template"
"time"
"github.com/KyleBanks/goodreads"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
var (
gitHubClient *githubv4.Client
goodReadsClient *goodreads.Client
goodReadsID string
username string
write = flag.String("write", "", "write output to")
)
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
fmt.Println("Usage: markscribe [template]")
os.Exit(1)
}
tplIn, err := ioutil.ReadFile(flag.Args()[0])
if err != nil {
fmt.Println("Can't read file:", err)
os.Exit(1)
}
tpl, err := template.New("tpl").Funcs(template.FuncMap{
/* GitHub */
"recentContributions": recentContributions,
"recentPullRequests": recentPullRequests,
"recentRepos": recentRepos,
"recentForks": recentForks,
"recentReleases": recentReleases,
"followers": recentFollowers,
"recentStars": recentStars,
"gists": gists,
"sponsors": sponsors,
"repo": repo,
/* RSS */
"rss": rssFeed,
/* GoodReads */
"goodReadsReviews": goodReadsReviews,
"goodReadsCurrentlyReading": goodReadsCurrentlyReading,
/* Literal.club */
"literalClubCurrentlyReading": literalClubCurrentlyReading,
/* Utils */
"humanize": humanized,
"reverse": reverse,
"now": time.Now,
"contains": strings.Contains,
"toLower": strings.ToLower,
}).Parse(string(tplIn))
if err != nil {
fmt.Println("Can't parse template:", err)
os.Exit(1)
}
var httpClient *http.Client
gitHubToken := os.Getenv("GITHUB_TOKEN")
goodReadsToken := os.Getenv("GOODREADS_TOKEN")
goodReadsID = os.Getenv("GOODREADS_USER_ID")
if len(gitHubToken) > 0 {
httpClient = oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: gitHubToken},
))
}
gitHubClient = githubv4.NewClient(httpClient)
goodReadsClient = goodreads.NewClient(goodReadsToken)
if len(gitHubToken) > 0 {
username, err = getUsername()
if err != nil {
fmt.Println("Can't retrieve GitHub profile:", err)
os.Exit(1)
}
}
w := os.Stdout
if len(*write) > 0 {
f, err := os.Create(*write)
if err != nil {
fmt.Println("Can't create:", err)
os.Exit(1)
}
defer f.Close() //nolint: errcheck
w = f
}
err = tpl.Execute(w, nil)
if err != nil {
fmt.Println("Can't render template:", err)
os.Exit(1)
}
}