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.

Revisions

  1. mattlord revised this gist Oct 16, 2024. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions cobra_docs_improvements.diff
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    diff --git a/go/cmd/internal/docgen/docgen.go b/go/cmd/internal/docgen/docgen.go
    index eea935ed39..40b97b322c 100644
    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 {
    @@ -11,18 +11,16 @@ index eea935ed39..40b97b322c 100644
    return err
    }

    @@ -174,7 +174,9 @@ func restructure(rootDir string, dir string, name string, commands []*cobra.Comm
    @@ -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)
    + // This requires an explicit empty string for the in-place edit backup
    + // files, meaning don't create any.
    + return exec.Command("sed", "-e", fmt.Sprintf("s:(./%s/):(../):i", parent), file)
    }

    var (
    @@ -194,7 +196,7 @@ func anonymizeHomedir(file string) (err error) {
    @@ -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>\]):'
    @@ -31,7 +29,7 @@ index eea935ed39..40b97b322c 100644
    if out, err := sed.CombinedOutput(); err != nil {
    return fmt.Errorf("%w: %s", err, out)
    }
    @@ -222,17 +224,26 @@ func getCommitID(ref string) (string, error) {
    @@ -222,17 +222,26 @@ func getCommitID(ref string) (string, error) {
    }

    const frontmatter = `---
    @@ -60,7 +58,7 @@ index eea935ed39..40b97b322c 100644
    root, cmdName, ok := strings.Cut(base, "_")
    if !ok { // no `_`, so not a subcommand
    cmdName = root
    @@ -240,7 +251,7 @@ func frontmatterFilePrepender(sha string) func(filename string) string {
    @@ -240,7 +249,7 @@ func frontmatterFilePrepender(sha string) func(filename string) string {

    cmdName = strings.ReplaceAll(cmdName, "_", " ")

  2. mattlord renamed this gist Oct 16, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. mattlord created this gist Oct 16, 2024.
    71 changes: 71 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    diff --git a/go/cmd/internal/docgen/docgen.go b/go/cmd/internal/docgen/docgen.go
    index eea935ed39..40b97b322c 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,9 @@ 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)
    + // This requires an explicit empty string for the in-place edit backup
    + // files, meaning don't create any.
    + return exec.Command("sed", "-e", fmt.Sprintf("s:(./%s/):(../):i", parent), file)
    }

    var (
    @@ -194,7 +196,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 +224,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 +251,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)
    }
    }