Skip to content

Instantly share code, notes, and snippets.

@mattlord
Last active October 16, 2024 13:05
Show Gist options
  • Save mattlord/a3a24e620dc1c77cf68cd42f3782cfc3 to your computer and use it in GitHub Desktop.
Save mattlord/a3a24e620dc1c77cf68cd42f3782cfc3 to your computer and use it in GitHub Desktop.
This addresses the creation of unwanted backup files using the BSD sed and it changes the commit hash notations so that they are ONLY added to the binary's parent page.
diff --git a/go/cmd/internal/docgen/docgen.go b/go/cmd/internal/docgen/docgen.go
index eea935ed39..a5c42c5435 100644
--- a/go/cmd/internal/docgen/docgen.go
+++ b/go/cmd/internal/docgen/docgen.go
@@ -75,7 +75,7 @@ func GenerateMarkdownTree(cmd *cobra.Command, dir string) error {
}
recursivelyDisableAutoGenTags(cmd)
- if err := doc.GenMarkdownTreeCustom(cmd, dir, frontmatterFilePrepender(sha), linkHandler); err != nil {
+ if err := doc.GenMarkdownTreeCustom(cmd, dir, frontmatterFilePrepender(cmd.Name(), sha), linkHandler); err != nil {
return err
}
@@ -174,7 +174,7 @@ func restructure(rootDir string, dir string, name string, commands []*cobra.Comm
}
func newParentLinkSedCommand(parent string, file string) *exec.Cmd {
- return exec.Command("sed", "-i", "", "-e", fmt.Sprintf("s:(./%s/):(../):i", parent), file)
+ return exec.Command("sed", "-e", fmt.Sprintf("s:(./%s/):(../):i", parent), file)
}
var (
@@ -194,7 +194,7 @@ func anonymizeHomedir(file string) (err error) {
// We're replacing the stuff inside the square brackets in the example sed
// below:
// 's:Paths to search for config files in. (default \[.*\])$:Paths to search for config files in. (default \[<WORKDIR>\]):'
- sed := exec.Command("sed", "-i", "-e", fmt.Sprintf("s:%s:<WORKDIR>:i", wd), file)
+ sed := exec.Command("sed", "-e", fmt.Sprintf("s:%s:<WORKDIR>:i", wd), file)
if out, err := sed.CombinedOutput(); err != nil {
return fmt.Errorf("%w: %s", err, out)
}
@@ -222,17 +222,26 @@ func getCommitID(ref string) (string, error) {
}
const frontmatter = `---
+
title: %s
series: %s
-commit: %s
+%s
---
`
-func frontmatterFilePrepender(sha string) func(filename string) string {
+func frontmatterFilePrepender(binary string, sha string) func(filename string) string {
return func(filename string) string {
name := filepath.Base(filename)
base := strings.TrimSuffix(name, filepath.Ext(name))
+ optionalItem := "" // No additional info by default.
+
+ if base == binary && filepath.Base(filepath.Dir(filename)) == binary {
+ // This is the top level page for the binary so let's note the
+ // commit hash used to build the binary's docs.
+ optionalItem = fmt.Sprintf("commit: %s\n", sha)
+ }
+
root, cmdName, ok := strings.Cut(base, "_")
if !ok { // no `_`, so not a subcommand
cmdName = root
@@ -240,7 +249,7 @@ func frontmatterFilePrepender(sha string) func(filename string) string {
cmdName = strings.ReplaceAll(cmdName, "_", " ")
- return fmt.Sprintf(frontmatter, cmdName, root, sha)
+ return fmt.Sprintf(frontmatter, cmdName, root, optionalItem)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment