-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (118 loc) · 3.29 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
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/google/go-github/v68/github"
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"
"github.com/make-go-great/netrc-go"
)
const (
postFilesPath = "posts"
templatePostPath = "templates/post.html"
generatedPath = "docs"
extHTML = ".html"
netrcPath = "~/.netrc"
netrcMachineGitHub = "github.com"
)
type templatePostData struct {
Index string
Body string
}
func main() {
ctx := context.Background()
// Cleanup generated path
if err := os.RemoveAll(generatedPath); err != nil {
log.Fatalln("Failed to remove all", generatedPath, err)
}
if err := os.MkdirAll(generatedPath, 0o777); err != nil {
log.Fatalln("Failed to mkdir all", generatedPath)
}
// Read post files directory
postFiles, err := os.ReadDir(postFilesPath)
if err != nil {
log.Fatalln("Failed to read dir", postFilesPath)
}
// Prepare template
templatePostBytes, err := os.ReadFile(templatePostPath)
if err != nil {
log.Fatalln("Failed to read file", templatePostPath, err)
}
templatePost, err := template.New("post").Parse(string(templatePostBytes))
if err != nil {
log.Fatalln("Failed to parse template", err)
}
// Prepare GitHub
netrcData, err := netrc.ParseFile(netrcPath)
if err != nil {
log.Fatalln("Failed to read file netrc", err)
}
var ghAccessToken string
for _, machine := range netrcData.Machines {
if machine.Name == netrcMachineGitHub {
ghAccessToken = machine.Password
break
}
}
if ghAccessToken == "" {
log.Fatalln("Empty GitHub token in netrc")
}
ghTokenSrc := oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: strings.TrimSpace(ghAccessToken),
},
)
ghHTTPClient := oauth2.NewClient(ctx, ghTokenSrc)
ghClient := github.NewClient(ghHTTPClient)
eg := new(errgroup.Group)
// Generate post files
for _, postFile := range postFiles {
if postFile.IsDir() {
continue
}
postFilename := postFile.Name()
eg.Go(func() error {
// Prepare post file
mdFilename := filepath.Join(postFilesPath, postFilename)
mdFileBytes, err := os.ReadFile(mdFilename)
if err != nil {
return fmt.Errorf("os: failed to read file %s: %w", mdFilename, err)
}
ghMarkdown, _, err := ghClient.Markdown.Render(ctx, string(mdFileBytes), nil)
if err != nil {
return fmt.Errorf("github: failed to markdown: %w", err)
}
// Prepare html file
htmlFilename := strings.TrimSuffix(postFilename, filepath.Ext(postFilename)) + extHTML
htmlFilepath := filepath.Join(generatedPath, htmlFilename)
htmlFile, err := os.OpenFile(htmlFilepath, os.O_RDWR|os.O_CREATE, 0o600)
if err != nil {
return fmt.Errorf("os: failed to open file %s: %w", htmlFilepath, err)
}
defer htmlFile.Close()
// Ignore index in index file
indexHTML := `<h2><a href="index.html"><code>~</code></a></h2>`
if strings.Contains(postFilename, "index") {
indexHTML = ""
}
if err := templatePost.Execute(htmlFile, templatePostData{
Index: indexHTML,
Body: ghMarkdown,
}); err != nil {
return fmt.Errorf("template: failed to execute: %w", err)
}
return nil
})
}
// Wait for all HTTP fetches to complete.
if err := eg.Wait(); err != nil {
log.Fatalln("I am sorry :(", err)
} else {
log.Println("Build successfully. Are you happy now?")
}
}