forked from lalluviamola/web-blog
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
231 lines (200 loc) · 5.5 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"flag"
"log"
_ "net/url"
"os"
"os/exec"
"runtime/pprof"
"strings"
"time"
"github.com/kjk/common/u"
"github.com/kjk/notionapi"
)
var (
dirWwwGenerated = "www_generated" // directory where we generate html files
httpPort = 9044
flgVerbose bool
flgNoCache bool
cacheDir = "notion_cache"
cachingPolicy = notionapi.PolicyDownloadNewer
)
type RequestCacheEntry struct {
Method string
URL string
Body []byte
BodyPP []byte // only if different than Body
Response []byte
}
type Cache struct {
Path string
Entries []*RequestCacheEntry
}
func main() {
var (
flgRunDev bool
flgRunProd bool
flgImportNotion bool
flgGen bool
flgDiff bool
flgCiDaily bool
flgGenMarkdown bool
flgImportNotionOne string
flgProfile string
)
{
// flag.BoolVar(&flgWc, "wc", false, "wc -l i.e. line count")
flag.BoolVar(&flgVerbose, "verbose", false, "if true, verbose logging")
flag.BoolVar(&flgNoCache, "no-cache", false, "if true, disables cache for downloading notion pages")
flag.BoolVar(&flgRunDev, "run-dev", false, "run server locally")
flag.BoolVar(&flgRunProd, "run-prod", false, "run server in production")
flag.BoolVar(&flgImportNotion, "import-notion", false, "re-download the content from Notion. use -no-cache to disable cache")
flag.BoolVar(&flgGen, "gen", false, "gen html in www_generated/ directory")
//flag.BoolVar(&flgDiff, "diff", false, "preview diff using winmerge")
flag.BoolVar(&flgCiDaily, "ci-update-from-notion", false, "incrementally update from notion")
flag.BoolVar(&flgGenMarkdown, "gen-markdown", false, "Generate markdown from notion cache")
//flag.StringVar(&flgProfile, "profile", "", "name of file to save cpu profiling info")
flag.Parse()
}
timeStart := time.Now()
defer func() {
logf(ctx(), "finished in %s\n", time.Since(timeStart))
}()
cdUpDir("blog")
if false {
dirToLF(".")
return
}
if false {
optimizeImages("notion_cache", "www")
return
}
if false {
flgImportNotionOne = "08e19004306b413aba6e0e86a10fec7a"
}
// for those commands we only want to use cache
if flgGen || flgRunDev {
cachingPolicy = notionapi.PolicyCacheOnly
}
if flgGenMarkdown {
genMarkdown()
return
}
if flgRunDev {
runServerDev()
return
}
if flgRunProd {
runServerProd()
return
}
if flgDiff {
u.WinmergeDiffPreview()
return
}
if flgProfile != "" {
logf(ctx(), "staring cpu profile in file '%s'\n", flgProfile)
f, err := os.Create(flgProfile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if flgCiDaily {
var cmd *exec.Cmd
// once a day re-download everything from Notion from scratch
// checkin if files changed
// and deploy to cloudflare if changed
ghToken := os.Getenv("GITHUB_TOKEN")
panicIf(ghToken == "", "GITHUB_TOKEN env variable missing")
panicIf(os.Getenv("NOTION_TOKEN") == "", "NOTION_TOKEN env variable missing")
{
// not sure if needed
cmd = exec.Command("git", "checkout", "master")
runCmdLoggedMust(cmd)
}
cachingPolicy = notionapi.PolicyDownloadNewer
cc := getNotionCachingClient()
_ = loadArticles(cc)
{
cmd = exec.Command("git", "status")
s := runCmdMust(cmd)
if strings.Contains(s, "nothing to commit, working tree clean") {
// nothing changed so nothing else to do
logf(ctx(), "Nothing changed, skipping deploy")
return
}
}
{
// not sure if this is needed on GitHub CI
cmd = exec.Command("git", "config", "--global", "user.email", "[email protected]")
runCmdLoggedMust(cmd)
cmd = exec.Command("git", "config", "--global", "user.name", "Krzysztof Kowalczyk")
runCmdLoggedMust(cmd)
/*
cmd = exec.Command("git", "config", "--global", "github.user", "kjk")
runCmdLoggedMust(cmd)
cmd = exec.Command("git", "config", "--global", "github.token", ghToken)
runCmdLoggedMust(cmd)
*/
cmd = exec.Command("git", "add", "notion_cache")
runCmdLoggedMust(cmd)
nowStr := time.Now().Format("2006-01-02")
commitMsg := "ci: update from notion on " + nowStr
cmd = exec.Command("git", "commit", "-am", commitMsg)
runCmdLoggedMust(cmd)
if false {
// TODO: do I need to be so specific or can I just do "git push"?
s := strings.Replace("https://${GITHUB_TOKEN}@github.com/kjk/blog.git", "${GITHUB_TOKEN}", ghToken, -1)
cmd = exec.Command("git", "push", s, "master")
runCmdLoggedMust(cmd)
} else {
cmd = exec.Command("git", "push")
runCmdLoggedMust(cmd)
}
}
return
}
if flgImportNotion {
cachingPolicy = notionapi.PolicyDownloadNewer
cc := getNotionCachingClient()
_ = loadArticles(cc)
return
}
if flgImportNotionOne != "" {
cachingPolicy = notionapi.PolicyDownloadAlways
cc := getNotionCachingClient()
_, err := cc.DownloadPage(flgImportNotionOne)
must(err)
return
}
if flgGen {
cachingPolicy = notionapi.PolicyCacheOnly
genHTMLServer(dirWwwGenerated)
return
}
flag.Usage()
}
func getNotionCachingClient() *notionapi.CachingClient {
if flgNoCache {
cachingPolicy = notionapi.PolicyDownloadAlways
}
token := os.Getenv("NOTION_TOKEN")
if token == "" && cachingPolicy != notionapi.PolicyCacheOnly {
logf(ctx(), "must set NOTION_TOKEN env variable\n")
os.Exit(1)
}
// TODO: verify token still valid, somehow
client := ¬ionapi.Client{
AuthToken: token,
}
if flgVerbose {
client.Logger = os.Stdout
client.DebugLog = true
}
d, err := notionapi.NewCachingClient(cacheDir, client)
must(err)
d.Policy = cachingPolicy
return d
}