Skip to content

Commit 013aefe

Browse files
vitess-bot[bot]dbussink
authored andcommitted
[Direct PR] [V21 backport] CobraDocs: Remove commit hash from docs. Fix issue with workdir replacement (#17392) (#17444)
Signed-off-by: Rohit Nayak <rohit@planetscale.com> Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
1 parent ffcedfb commit 013aefe

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

go/cmd/internal/docgen/docgen.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ func restructure(rootDir string, dir string, name string, commands []*cobra.Comm
116116
fullCmdFilename := strings.Join([]string{name, cmd.Name()}, "_")
117117

118118
children := cmd.Commands()
119-
120119
switch {
121120
case len(children) > 0:
122121
// Command (top-level or not) with children.
@@ -151,7 +150,6 @@ func restructure(rootDir string, dir string, name string, commands []*cobra.Comm
151150

152151
oldName := filepath.Join(rootDir, fullCmdFilename+".md")
153152
newName := filepath.Join(dir, fullCmdFilename+".md")
154-
155153
if err := os.Rename(oldName, newName); err != nil {
156154
return fmt.Errorf("failed to move child command %s to its parent's dir: %w", fullCmdFilename, err)
157155
}
@@ -166,6 +164,14 @@ func restructure(rootDir string, dir string, name string, commands []*cobra.Comm
166164
}
167165
default:
168166
// Top-level command without children. Nothing to restructure.
167+
// However we still need to anonymize the homedir in the help text.
168+
if cmd.Name() == "help" {
169+
// all commands with children have their own "help" subcommand,
170+
// which we do not generate docs for
171+
continue
172+
}
173+
f := filepath.Join(dir, fullCmdFilename+".md")
174+
_ = anonymizeHomedir(f) // it is possible that the file does not exist, so we ignore the error
169175
continue
170176
}
171177
}
@@ -190,11 +196,14 @@ func anonymizeHomedir(file string) (err error) {
190196
if err != nil {
191197
return err
192198
}
199+
if _, err := os.Stat(file); err != nil {
200+
return nil
201+
}
193202

194203
// We're replacing the stuff inside the square brackets in the example sed
195204
// below:
196205
// 's:Paths to search for config files in. (default \[.*\])$:Paths to search for config files in. (default \[<WORKDIR>\]):'
197-
sed := exec.Command("sed", "-i", "-e", fmt.Sprintf("s:%s:<WORKDIR>:i", wd), file)
206+
sed := exec.Command("sed", "-i", "", "-e", fmt.Sprintf("s:%s:%s:", wd, "<WORKDIR>"), file)
198207
if out, err := sed.CombinedOutput(); err != nil {
199208
return fmt.Errorf("%w: %s", err, out)
200209
}
@@ -224,7 +233,6 @@ func getCommitID(ref string) (string, error) {
224233
const frontmatter = `---
225234
title: %s
226235
series: %s
227-
commit: %s
228236
---
229237
`
230238

@@ -240,7 +248,7 @@ func frontmatterFilePrepender(sha string) func(filename string) string {
240248

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

243-
return fmt.Sprintf(frontmatter, cmdName, root, sha)
251+
return fmt.Sprintf(frontmatter, cmdName, root)
244252
}
245253
}
246254

go/cmd/internal/docgen/docgen_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestGenerateMarkdownTree(t *testing.T) {
4141
name: "current dir",
4242
dir: "./",
4343
cmd: &cobra.Command{},
44-
expectErr: false,
44+
expectErr: true,
4545
},
4646
{
4747
name: "Permission denied",

0 commit comments

Comments
 (0)