Add the ability to specify a filePostpender in GenMarkdownTreeCustom (#1270)

This commit is contained in:
Joon-Ho Son 2021-01-21 03:33:16 +00:00 committed by GitHub
parent 4384b91fb4
commit 23a6174c7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 4 deletions

View File

@ -122,17 +122,17 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string)
func GenMarkdownTree(cmd *cobra.Command, dir string) error { func GenMarkdownTree(cmd *cobra.Command, dir string) error {
identity := func(s string) string { return s } identity := func(s string) string { return s }
emptyStr := func(s string) string { return "" } emptyStr := func(s string) string { return "" }
return GenMarkdownTreeCustom(cmd, dir, emptyStr, identity) return GenMarkdownTreeCustom(cmd, dir, emptyStr, emptyStr, identity)
} }
// GenMarkdownTreeCustom is the the same as GenMarkdownTree, but // GenMarkdownTreeCustom is the the same as GenMarkdownTree, but
// with custom filePrepender and linkHandler. // with custom filePrepender, filePostpender, and linkHandler.
func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error { func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, filePostpender, linkHandler func(string) string) error {
for _, c := range cmd.Commands() { for _, c := range cmd.Commands() {
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() { if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
continue continue
} }
if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil { if err := GenMarkdownTreeCustom(c, dir, filePrepender, filePostpender, linkHandler); err != nil {
return err return err
} }
} }
@ -151,5 +151,8 @@ func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHa
if err := GenMarkdownCustom(cmd, f, linkHandler); err != nil { if err := GenMarkdownCustom(cmd, f, linkHandler); err != nil {
return err return err
} }
if _, err := io.WriteString(f, filePostpender(filename)); err != nil {
return err
}
return nil return nil
} }

View File

@ -95,6 +95,37 @@ func TestGenMdTree(t *testing.T) {
} }
} }
func TestGenMdTreeCustom(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test-gen-md-tree")
if err != nil {
t.Fatalf("Failed to create tmpdir: %v", err)
}
defer os.RemoveAll(tmpdir)
prepender := func(s string) string { return "Prepended" }
postpender := func(s string) string { return "Postpended" }
identity := func(s string) string { return s }
if err := GenMarkdownTreeCustom(rootCmd, tmpdir, prepender, postpender, identity); err != nil {
t.Fatalf("GenMarkdownTree failed: %v", err)
}
gotRoot := fileContents(t, tmpdir, "root.md")
checkStringContains(t, gotRoot, "Prepended")
checkStringContains(t, gotRoot, rootCmd.Long)
checkStringContains(t, gotRoot, "Postpended")
gotEcho := fileContents(t, tmpdir, "root_echo.md")
checkStringContains(t, gotEcho, "Prepended")
checkStringContains(t, gotEcho, echoCmd.Long)
checkStringContains(t, gotEcho, "Postpended")
gotEchoSub := fileContents(t, tmpdir, "root_echo_echosub.md")
checkStringContains(t, gotEchoSub, "Prepended")
checkStringContains(t, gotEchoSub, echoSubCmd.Long)
checkStringContains(t, gotEchoSub, "Postpended")
}
func BenchmarkGenMarkdownToFile(b *testing.B) { func BenchmarkGenMarkdownToFile(b *testing.B) {
file, err := ioutil.TempFile("", "") file, err := ioutil.TempFile("", "")
if err != nil { if err != nil {
@ -110,3 +141,11 @@ func BenchmarkGenMarkdownToFile(b *testing.B) {
} }
} }
} }
func fileContents(t *testing.T, dir, filename string) string {
contents, err := ioutil.ReadFile(filepath.Join(dir, filename))
if err != nil {
t.Fatalf("Error loading file %q: %v ", filename, err)
}
return string(contents)
}