posts-go/main.go

109 lines
2.4 KiB
Go
Raw Normal View History

2021-11-30 15:17:09 +00:00
package main
import (
"context"
2021-11-30 15:17:09 +00:00
"log"
"os"
"path/filepath"
"strings"
2022-07-11 04:26:24 +00:00
"text/template"
2021-11-30 15:17:09 +00:00
"github.com/google/go-github/v48/github"
"golang.org/x/oauth2"
2021-11-30 15:17:09 +00:00
)
const (
2022-07-11 04:26:24 +00:00
postFilesPath = "posts"
templatePostPath = "templates/post.html"
2022-07-10 11:09:42 +00:00
generatedPath = "docs"
2022-07-11 04:26:24 +00:00
extHTML = ".html"
2021-11-30 15:17:09 +00:00
)
2022-07-11 04:26:24 +00:00
type templatePostData struct {
Body string
}
2021-11-30 15:17:09 +00:00
func main() {
ctx := context.Background()
2022-07-10 11:32:01 +00:00
// 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)
}
2022-07-11 04:26:24 +00:00
// Read post files directory
postFiles, err := os.ReadDir(postFilesPath)
2021-11-30 15:17:09 +00:00
if err != nil {
2022-07-11 04:26:24 +00:00
log.Fatalln("Failed to read dir", postFilesPath)
2021-11-30 15:17:09 +00:00
}
2022-07-11 04:26:24 +00:00
// Prepare template
templatePostBytes, err := os.ReadFile(templatePostPath)
2021-11-30 15:17:09 +00:00
if err != nil {
2022-07-11 04:26:24 +00:00
log.Fatalln("Failed to read file", templatePostPath, err)
2021-11-30 15:17:09 +00:00
}
2022-07-11 04:26:24 +00:00
templatePost, err := template.New("post").Parse(string(templatePostBytes))
if err != nil {
log.Fatalln("Failed to parse template", err)
}
// Prepare GitHub
ghAccessTokenBytes, err := os.ReadFile(".github_access_token")
if err != nil {
log.Fatalln("Failed to read file", ".github_access_token", err)
}
2022-07-11 04:26:24 +00:00
ghTokenSrc := oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: string(ghAccessTokenBytes),
},
)
ghHTTPClient := oauth2.NewClient(ctx, ghTokenSrc)
ghClient := github.NewClient(ghHTTPClient)
2022-07-11 04:26:24 +00:00
// Generate post files
for _, postFile := range postFiles {
if postFile.IsDir() {
2021-11-30 15:17:09 +00:00
continue
}
2022-07-11 04:26:24 +00:00
// Prepare post file
mdFilename := filepath.Join(postFilesPath, postFile.Name())
mdFileBytes, err := os.ReadFile(mdFilename)
2021-11-30 15:17:09 +00:00
if err != nil {
2022-07-11 04:26:24 +00:00
log.Fatalln("Failed to read file", mdFilename, err)
2021-11-30 15:17:09 +00:00
}
ghMarkdown, _, err := ghClient.Markdown(ctx, string(mdFileBytes), &github.MarkdownOptions{
Mode: "markdown",
})
if err != nil {
log.Fatalln("Failed to GitHub markdown", err)
}
2022-07-11 04:26:24 +00:00
// Prepare html file
htmlFilename := strings.TrimSuffix(postFile.Name(), filepath.Ext(postFile.Name())) + extHTML
htmlFilepath := filepath.Join(generatedPath, htmlFilename)
htmlFile, err := os.OpenFile(htmlFilepath, os.O_RDWR|os.O_CREATE, 0o600)
if err != nil {
log.Fatalln("Failed to open file", htmlFilepath, err)
}
if err := templatePost.Execute(htmlFile, templatePostData{
Body: ghMarkdown,
}); err != nil {
log.Fatalln("Failed to execute html template", err)
2021-11-30 15:17:09 +00:00
}
2022-07-11 04:26:24 +00:00
htmlFile.Close()
}
2021-11-30 15:17:09 +00:00
}