feat: generate using errgroup
parent
935ffea466
commit
15a5605dd1
1
go.mod
1
go.mod
|
@ -5,6 +5,7 @@ go 1.18
|
||||||
require (
|
require (
|
||||||
github.com/google/go-github/v48 v48.2.0
|
github.com/google/go-github/v48 v48.2.0
|
||||||
golang.org/x/oauth2 v0.3.0
|
golang.org/x/oauth2 v0.3.0
|
||||||
|
golang.org/x/sync v0.1.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -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/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 h1:6l90koy8/LaBLmLu8jpHeHexzMwEita0zFfYlggy2F8=
|
||||||
golang.org/x/oauth2 v0.3.0/go.mod h1:rQrIauxkUhJ6CuwEXwymO2/eh4xz2ZWF1nBkcxS+tGk=
|
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/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.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
|
|
69
main.go
69
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -10,6 +11,7 @@ import (
|
||||||
|
|
||||||
"github.com/google/go-github/v48/github"
|
"github.com/google/go-github/v48/github"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -68,41 +70,56 @@ func main() {
|
||||||
ghHTTPClient := oauth2.NewClient(ctx, ghTokenSrc)
|
ghHTTPClient := oauth2.NewClient(ctx, ghTokenSrc)
|
||||||
ghClient := github.NewClient(ghHTTPClient)
|
ghClient := github.NewClient(ghHTTPClient)
|
||||||
|
|
||||||
|
eg := new(errgroup.Group)
|
||||||
|
|
||||||
// Generate post files
|
// Generate post files
|
||||||
for _, postFile := range postFiles {
|
for _, postFile := range postFiles {
|
||||||
if postFile.IsDir() {
|
if postFile.IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare post file
|
postFilename := postFile.Name()
|
||||||
mdFilename := filepath.Join(postFilesPath, postFile.Name())
|
|
||||||
mdFileBytes, err := os.ReadFile(mdFilename)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Failed to read file", mdFilename, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ghMarkdown, _, err := ghClient.Markdown(ctx, string(mdFileBytes), &github.MarkdownOptions{
|
eg.Go(func() error {
|
||||||
Mode: "markdown",
|
// 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)
|
// Wait for all HTTP fetches to complete.
|
||||||
if err != nil {
|
if err := eg.Wait(); err != nil {
|
||||||
log.Fatalln("Failed to open file", htmlFilepath, err)
|
log.Fatalln("I am sorry :(", err)
|
||||||
}
|
} else {
|
||||||
|
log.Println("Build successfully. Are you happy now?")
|
||||||
if err := templatePost.Execute(htmlFile, templatePostData{
|
|
||||||
Body: ghMarkdown,
|
|
||||||
}); err != nil {
|
|
||||||
log.Fatalln("Failed to execute html template", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
htmlFile.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue