-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
405 lines (313 loc) · 9.02 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/olebedev/config"
"github.com/senorprogrammer/til/pages"
"github.com/senorprogrammer/til/src"
)
const (
defaultCommitMsg = "build, save, push"
defaultEditor = "open"
/* -------------------- Messages -------------------- */
errConfigValueRead = "could not read a required configuration value"
errNoTitle = "title must not be blank"
statusDone = "done"
statusIdxBuild = "building index page"
statusRepoPush = "pushing to remote"
statusRepoSave = "saving uncommitted files"
statusTagBuild = "building tag pages"
)
var (
buildFlag bool
listFlag bool
saveFlag bool
targetDirFlag string
)
func init() {
src.LL = log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile)
flag.BoolVar(&buildFlag, "b", false, "builds the index and tag pages (short-hand)")
flag.BoolVar(&buildFlag, "build", false, "builds the index and tag pages")
flag.BoolVar(&listFlag, "l", false, "lists the configured target directories (short-hand)")
flag.BoolVar(&listFlag, "list", false, "lists the configured target directories")
flag.BoolVar(&saveFlag, "s", false, "builds, saves, and pushes (short-hand)")
flag.BoolVar(&saveFlag, "save", false, "builds, saves, and pushes")
flag.StringVar(&targetDirFlag, "t", "", "specifies the target directory key (short-hand)")
flag.StringVar(&targetDirFlag, "target", "", "specifies the target directory key")
}
/* -------------------- Main -------------------- */
func main() {
flag.Parse()
cnf := &src.Config{}
cnf.Load()
/* Flaghandling */
/* I personally think "flag handling" should be spelled flag-handling
but precedence has been set and we will defer to it.
According to wiktionary.org, "stick-handling" is correctly spelled
"stickhandling", so here we are, abomination enshrined */
if listFlag {
listTargetDirectories(src.GlobalConfig)
src.Victory(statusDone)
}
if buildFlag {
buildContent()
src.Victory(statusDone)
}
if saveFlag {
commitMsg := determineCommitMessage(src.GlobalConfig, os.Args)
buildContent()
save(commitMsg)
push()
src.Victory(statusDone)
}
src.BuildTargetDirectory()
/* Page creation */
title := parseTitle(targetDirFlag, os.Args)
if title == "" {
// Every non-dash argument is considered a part of the title. If there are no arguments, we have no title
// Can't have a page without a title
src.Defeat(errors.New(errNoTitle))
}
createNewPage(title)
src.Victory(statusDone)
}
/* -------------------- Helper functions -------------------- */
func buildContent() {
pages := loadPages()
tagMap := buildTagPages(pages)
buildIndexPage(pages, tagMap)
}
// buildIndexPage creates the main index.md page that is the root of the site
func buildIndexPage(pageSet []*pages.Page, tagMap *pages.TagMap) {
src.Info(statusIdxBuild)
content := ""
// Write the tag list into the top of the index
tagLinks := []string{}
for _, tagName := range tagMap.SortedTagNames() {
tags := tagMap.Get(tagName)
if len(tags) > 0 {
tagLinks = append(tagLinks, tags[0].Link())
}
}
content += strings.Join(tagLinks, ", ")
content += "\n"
// Write the page list into the middle of the page
content += pagesToHTMLUnorderedList(pageSet)
content += "\n"
// Write the footer content into the bottom of the index
content += "\n"
content += src.Footer()
// And write the file to disk
tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, true)
if err != nil {
src.Defeat(err)
}
filePath := fmt.Sprintf(
"%s/index.%s",
tDir,
pages.FileExtension,
)
err = ioutil.WriteFile(filePath, []byte(content), 0644)
if err != nil {
src.Defeat(err)
}
src.Progress(filePath)
}
// buildTagPages creates the tag pages, with links to posts tagged with those names
func buildTagPages(pageSet []*pages.Page) *pages.TagMap {
src.Info(statusTagBuild)
tagMap := pages.NewTagMap(pageSet)
var wGroup sync.WaitGroup
for _, tagName := range tagMap.SortedTagNames() {
wGroup.Add(1)
go func(tagName string) {
defer wGroup.Done()
content := fmt.Sprintf("## %s\n\n", tagName)
// Write the page list into the middle of the page
content += pagesToHTMLUnorderedList(tagMap.PagesFor(tagName))
// Write the footer content into the bottom of the page
content += "\n"
content += src.Footer()
// And write the file to disk
tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, true)
if err != nil {
src.Defeat(err)
}
filePath := fmt.Sprintf(
"%s/%s.%s",
tDir,
tagName,
pages.FileExtension,
)
err = ioutil.WriteFile(filePath, []byte(content), 0644)
if err != nil {
src.Defeat(err)
}
src.Progress(filePath)
}(tagName)
}
wGroup.Wait()
return tagMap
}
func createNewPage(title string) {
tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, true)
if err != nil {
src.Defeat(err)
}
page := pages.NewPage(title, tDir)
err = page.Open(defaultEditor)
if err != nil {
src.Defeat(err)
}
// Write the page path to the console. This makes it easy to know which file we just created
src.Info(page.FilePath)
}
// determineCommitMessage figures out which commit message to save the repo with
// The order of precedence is:
// * message passed in via the -s flag
// * message defined in config.yml for the commitMessage key
// * message as a hard-coded constant, at top, in defaultCommitMsg
// Example:
// > til -t b -s this is message
func determineCommitMessage(cfg *config.Config, args []string) string {
if flag.NArg() == 0 {
return cfg.UString("commitMessage", defaultCommitMsg)
}
msgOffset := len(args) - flag.NArg()
msg := strings.Join(args[msgOffset:], " ")
return msg
}
// listTargetDirectories writes the list of target directories in the configuration
// out to the terminal
func listTargetDirectories(cfg *config.Config) {
dirMap, err := cfg.Map("targetDirectories")
if err != nil {
src.Defeat(err)
}
for key, dir := range dirMap {
src.Info(fmt.Sprintf("%6s\t%s\n", key, dir.(string)))
}
}
// loadPages reads the page files from disk (in reverse chronological order) and
// creates Page instances from them
func loadPages() []*pages.Page {
pageSet := []*pages.Page{}
tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, true)
if err != nil {
src.Defeat(err)
}
filePaths, _ := filepath.Glob(
fmt.Sprintf(
"%s/*.%s",
tDir,
pages.FileExtension,
),
)
for i := len(filePaths) - 1; i >= 0; i-- {
page := pages.PageFromFilePath(filePaths[i])
pageSet = append(pageSet, page)
}
return pageSet
}
// // open tll the OS to open the newly-created page in the editor (as specified in the config)
// // If there's no editor explicitly defined by the user, tell the OS to try and open it
// func open(page *src.Page) error {
// editor := src.GlobalConfig.UString("editor", defaultEditor)
// if editor == "" {
// editor = defaultEditor
// }
// cmd := exec.Command(editor, page.FilePath)
// err := cmd.Run()
// return err
// }
// pagesToHTMLUnorderedList creates the unordered list of page links that appear
// on the index and tag pages
func pagesToHTMLUnorderedList(pageSet []*pages.Page) string {
content := ""
prevPage := &pages.Page{}
for _, page := range pageSet {
if !page.IsContentPage() {
continue
}
// This breaks the page list up by month
if prevPage.CreatedMonth() != page.CreatedMonth() {
content += "\n"
}
content += fmt.Sprintf("* %s\n", page.Link())
prevPage = page
}
return content
}
func parseTitle(targetFlag string, args []string) string {
titleOffset := 3
if targetFlag == "" {
titleOffset = 1
}
return strings.Title(strings.Join(args[titleOffset:], " "))
}
// push pushes up to the remote git repo
func push() {
src.Info(statusRepoPush)
tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, false)
if err != nil {
src.Defeat(err)
}
r, err := git.PlainOpen(tDir)
if err != nil {
src.Defeat(err)
}
err = r.Push(&git.PushOptions{})
if err != nil {
src.Defeat(err)
}
}
// https://github.com/go-git/go-git/blob/master/_examples/commit/main.go
func save(commitMsg string) {
src.Info(statusRepoSave)
tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, false)
if err != nil {
src.Defeat(err)
}
r, err := git.PlainOpen(tDir)
if err != nil {
src.Defeat(err)
}
w, err := r.Worktree()
if err != nil {
src.Defeat(err)
}
_, err = w.Add(".")
if err != nil {
src.Defeat(err)
}
defaultCommitEmail, err2 := src.GlobalConfig.String("committerEmail")
defaultCommitName, err3 := src.GlobalConfig.String("committerName")
if err2 != nil || err3 != nil {
src.Defeat(errors.New(errConfigValueRead))
}
commit, err := w.Commit(commitMsg, &git.CommitOptions{
Author: &object.Signature{
Name: defaultCommitName,
Email: defaultCommitEmail,
When: time.Now(),
},
})
if err != nil {
src.Defeat(err)
}
obj, err := r.CommitObject(commit)
if err != nil {
src.Defeat(err)
}
src.Info(fmt.Sprintf("committed with '%s' (%.7s)", obj.Message, obj.Hash.String()))
}