Skip to content

Commit

Permalink
Add command to generate markdown docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ipmb committed Jan 5, 2021
1 parent dbbf417 commit dc46669
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions cmd/docgen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)

var directory string

func filePrepender(filename string) string {
name := filepath.Base(filename)
base := strings.TrimSuffix(name, path.Ext(name))
commandParts := strings.Split(base, "_")
if len(commandParts) > 1 {
commandParts = commandParts[1:]
} else {
commandParts = append(commandParts, "(base command)")
}
return fmt.Sprintf(`---
title: %s
---
`, strings.Join(commandParts, " "))
}

// docgenCmd represents the docgen command
var docgenCmd = &cobra.Command{
Use: "docgen",
Short: "generate command documentation as markdown",
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
err := os.MkdirAll(directory, os.FileMode(0755))
checkErr(err)
identity := func(s string) string { return s }
doc.GenMarkdownTreeCustom(rootCmd, directory, filePrepender, identity)
checkErr(err)
},
}

func init() {
rootCmd.AddCommand(docgenCmd)
docgenCmd.Flags().StringVarP(&directory, "directory", "d", "./docs/src/command-line-reference", "output directory")
}

0 comments on commit dc46669

Please sign in to comment.