feat: generate using errgroup

main
sudo pacman -Syu 2022-12-30 16:41:19 +07:00
parent 935ffea466
commit 15a5605dd1
3 changed files with 46 additions and 26 deletions

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.18
require (
github.com/google/go-github/v48 v48.2.0
golang.org/x/oauth2 v0.3.0
golang.org/x/sync v0.1.0
)
require (

2
go.sum
View File

@ -17,6 +17,8 @@ golang.org/x/net v0.3.0 h1:VWL6FNY2bEEmsGVKabSlHu5Irp34xmMRoqb/9lF9lxk=
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/oauth2 v0.3.0 h1:6l90koy8/LaBLmLu8jpHeHexzMwEita0zFfYlggy2F8=
golang.org/x/oauth2 v0.3.0/go.mod h1:rQrIauxkUhJ6CuwEXwymO2/eh4xz2ZWF1nBkcxS+tGk=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

69
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
@ -10,6 +11,7 @@ import (
"github.com/google/go-github/v48/github"
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"
)
const (
@ -68,41 +70,56 @@ func main() {
ghHTTPClient := oauth2.NewClient(ctx, ghTokenSrc)
ghClient := github.NewClient(ghHTTPClient)
eg := new(errgroup.Group)
// Generate post files
for _, postFile := range postFiles {
if postFile.IsDir() {
continue
}
// Prepare post file
mdFilename := filepath.Join(postFilesPath, postFile.Name())
mdFileBytes, err := os.ReadFile(mdFilename)
if err != nil {
log.Fatalln("Failed to read file", mdFilename, err)
}
postFilename := postFile.Name()
ghMarkdown, _, err := ghClient.Markdown(ctx, string(mdFileBytes), &github.MarkdownOptions{
Mode: "markdown",
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(ctx, string(mdFileBytes), &github.MarkdownOptions{
Mode: "markdown",
})
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()
if err := templatePost.Execute(htmlFile, templatePostData{
Body: ghMarkdown,
}); err != nil {
return fmt.Errorf("template: failed to execute: %w", err)
}
return nil
})
if err != nil {
log.Fatalln("Failed to GitHub markdown", err)
}
// 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)
}
htmlFile.Close()
// 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?")
}
}