diff --git a/go.mod b/go.mod index 22aacaa..207ebde 100644 --- a/go.mod +++ b/go.mod @@ -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 ( diff --git a/go.sum b/go.sum index 705697b..c27e3de 100644 --- a/go.sum +++ b/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/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= diff --git a/main.go b/main.go index 68ea00e..00f6159 100644 --- a/main.go +++ b/main.go @@ -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?") } }