forked from muesli/markscribe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
109 lines (95 loc) · 2.58 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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"text/template"
"github.com/KyleBanks/goodreads"
"github.com/go-sprout/sprout"
"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 := os.ReadFile(flag.Args()[0])
if err != nil {
fmt.Println("Can't read file:", err)
os.Exit(1)
}
funcMap := sprout.FuncMap(sprout.WithAlias("lower", "toLower"))
/* Github */
funcMap["recentContributions"] = recentContributions
funcMap["recentPullRequests"] = recentPullRequests
funcMap["popularRepos"] = popularRepos
funcMap["recentCreatedRepos"] = recentCreatedRepos
funcMap["recentPushedRepos"] = recentPushedRepos
funcMap["recentForkedRepos"] = recentForkedRepos
funcMap["latestReleasedRepos"] = latestReleasedRepos
funcMap["recentReleases"] = recentReleases
funcMap["followers"] = recentFollowers
funcMap["recentStars"] = recentStars
funcMap["gists"] = gists
funcMap["sponsors"] = sponsors
funcMap["repo"] = repo
funcMap["repoRecentReleases"] = repoRecentReleases
/* RSS */
funcMap["rss"] = rssFeed
/* GoodReads */
funcMap["goodReadsReviews"] = goodReadsReviews
funcMap["goodReadsCurrentlyReading"] = goodReadsCurrentlyReading
/* Literal.club */
funcMap["literalClubCurrentlyReading"] = literalClubCurrentlyReading
/* Utils */
funcMap["humanize"] = humanized
tpl, err := template.New("tpl").Funcs(funcMap).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)
}
}