From e02edb878f2d336819a8a6efe2fe7e9051ced687 Mon Sep 17 00:00:00 2001 From: Marcell Mars Date: Sat, 19 Dec 2020 00:10:46 +0100 Subject: [PATCH] print commit logs back to remote git --- main.go | 78 ++++++++++++++++++++------------------------------------- 1 file changed, 27 insertions(+), 51 deletions(-) diff --git a/main.go b/main.go index fd2c0b0..1435825 100644 --- a/main.go +++ b/main.go @@ -57,8 +57,8 @@ func isPublished(gitRepoPath, gitIndexPath, prevcommit, lastcommit string) bool return false } -func hugoLogs(logLines string, lines []string, hugoInstance string) string { - logLines = logLines + fmt.Sprintf("~~~~~~~~~~ Hugo's logs %s ~~~~~~~~~~\n", hugoInstance) +func hugoLogs(logLines string, lines []string) string { + logLines = logLines + "~~~~~~~~~~ Hugo's logs ~~~~~~~~~~\n" for _, d := range lines { logLines = logLines + d + "\n" @@ -92,7 +92,6 @@ func main() { startTime := time.Now() logLines := startTime.Format(time.RFC822) + "\n" - logGiteaLines := "" ex, err := os.Executable() check(err) @@ -157,77 +156,58 @@ func main() { outSandpointsDir, sandpointsCatalogName, sandpointsCatalogPrefix := getOutdirAndConfigCatalog(tmpRepoPath, gitRepoPath) logLines = logLines + fmt.Sprintf("to be moved to: %s\n", filepath.Join(outDir, outSandpointsDir)) tmpHugoGiteaPath := filepath.Join(os.TempDir(), fmt.Sprintf("sandpoints_hugo_%06d", rand.Intn(100001))) - tmpHugoPreviewPath := filepath.Join(tmpHugoGiteaPath, "_preview") err = os.RemoveAll(tmpHugoGiteaPath) check(err) - err = os.MkdirAll(tmpHugoPreviewPath, 0755) - check(err) - - err = os.MkdirAll(filepath.Join(outDir, outSandpointsDir, "_preview"), 0755) - check(err) - published := isPublished(gitRepoPath, gitIndexPath, prevCommit, lastCommit) - defer func() { - lastPreviewCommitLog, err := os.Create(filepath.Join(outDir, outSandpointsDir, "_preview", "last-commit-log.txt")) - check(err) - defer lastPreviewCommitLog.Close() - fmt.Fprintln(lastPreviewCommitLog, logLines) - log.Printf(logLines) - }() - defer func() { if published { lastGiteaCommitLog, err := os.Create(filepath.Join(outDir, outSandpointsDir, "last-commit-log.txt")) check(err) defer lastGiteaCommitLog.Close() - fmt.Fprintln(lastGiteaCommitLog, logGiteaLines) + fmt.Fprintln(lastGiteaCommitLog, logLines) + } else { + lastPreviewCommitLog, err := os.Create(filepath.Join(outDir, outSandpointsDir, "_preview", "last-commit-log.txt")) + check(err) + defer lastPreviewCommitLog.Close() + fmt.Fprintln(lastPreviewCommitLog, logLines) } + log.Printf(logLines) }() - if published { - hugoGiteaLogs := cage.Start() - respGitea := commands.Execute([]string{"-s", tmpRepoPath, "-d", tmpHugoGiteaPath, "-e", "gitea"}) - cage.Stop(hugoGiteaLogs) + hugoGiteaLogs := cage.Start() + respGitea := commands.Execute([]string{"-s", tmpRepoPath, "-d", tmpHugoGiteaPath, "-e", "gitea"}) + cage.Stop(hugoGiteaLogs) - if respGitea.Err != nil { - logGiteaLines = fmt.Sprintf("Attempt to publish web site ended up with an Error! Check out the last commit to capture possible errors.\n\n%s\n~~~~~~~~~~\n\n%s", respGitea.Err, logLines) - logLines = fmt.Sprintf("Rendering _preview of the web site ended up with an ERROR! Check out the last commit to capture possible errors.\n\n%s\n~~~~~~~~~~\n\n%s", respGitea.Err, logLines) - runtime.Goexit() - } - - logGiteaLines = hugoLogs(logLines, hugoGiteaLogs.Data, "for published instance") - } - - hugoPreviewLogs := cage.Start() - respPreview := commands.Execute([]string{"-s", tmpRepoPath, "-d", tmpHugoPreviewPath, "-e", "preview", "--templateMetrics"}) - cage.Stop(hugoPreviewLogs) - - if respPreview.Err != nil { - logLines = fmt.Sprintf("Rendering _preview of the web site ended up with an ERROR! Check out the last commit to capture possible errors.\n\n%s\n~~~~~~~~~~\n\n%s", respPreview.Err, logLines) + if respGitea.Err != nil { + logLines = fmt.Sprintf("Hugo's attempt to render the web site ended up with an Error! Check out the last commit to capture possible errors.\n\n%s\n~~~~~~~~~~\n\n%s", respGitea.Err, logLines) runtime.Goexit() + } else { + logLines = hugoLogs(logLines, hugoGiteaLogs.Data) } - logLines = hugoLogs(logLines, hugoPreviewLogs.Data, "for _preview instance") - if published { if _, err := os.Stat(filepath.Join(outDir, outSandpointsDir)); err == nil { err := os.RemoveAll(filepath.Join(outDir, outSandpointsDir)) check(err) + err = os.MkdirAll(outDir, 0755) + check(err) } + } else if _, err := os.Lstat(filepath.Join(outDir, outSandpointsDir, "_preview")); err == nil { + err := os.Remove(filepath.Join(outDir, outSandpointsDir, "_preview")) + check(err) } else if _, err := os.Stat(filepath.Join(outDir, outSandpointsDir, "_preview")); err == nil { err := os.RemoveAll(filepath.Join(outDir, outSandpointsDir, "_preview")) check(err) } - err = os.MkdirAll(outDir, 0755) - check(err) - if published { err = os.Rename(tmpHugoGiteaPath, filepath.Join(outDir, outSandpointsDir)) check(err) + err = os.Symlink(filepath.Join(outDir, outSandpointsDir), filepath.Join(outDir, outSandpointsDir, "_preview")) + check(err) if sandpointsCatalogPrefix != "" && sandpointsCatalogName != "" { err = os.Symlink(filepath.Join(outDir, "libraries", sandpointsCatalogName), filepath.Join(outDir, outSandpointsDir, sandpointsCatalogPrefix)) check(err) @@ -235,7 +215,9 @@ func main() { check(err) } } else { - err = os.Rename(tmpHugoPreviewPath, filepath.Join(outDir, outSandpointsDir, "_preview")) + err = os.MkdirAll(filepath.Join(outDir, outSandpointsDir), 0755) + check(err) + err = os.Rename(tmpHugoGiteaPath, filepath.Join(outDir, outSandpointsDir, "_preview")) check(err) if sandpointsCatalogPrefix != "" && sandpointsCatalogName != "" { err = os.Symlink(filepath.Join(outDir, "libraries", sandpointsCatalogName), filepath.Join(outDir, outSandpointsDir, "_preview", sandpointsCatalogPrefix)) @@ -243,12 +225,6 @@ func main() { } } - publishedMessage := "" - if published { - publishedMessage = " + published instance" - } - durationMillseconds := int64(time.Since(startTime) / time.Millisecond) - logLines = logLines + fmt.Sprintf("Total processing time (_preview%s): %d ms", publishedMessage, durationMillseconds) - logGiteaLines = logGiteaLines + fmt.Sprintf("Total processing time (published + _preview instance): %d ms", durationMillseconds) + logLines = logLines + fmt.Sprintf("Total processing time: %d ms", durationMillseconds) }