未验证 提交 c7434778 编写于 作者: LinuxSuRen's avatar LinuxSuRen 提交者: GitHub

Add a command to generate the whole documents for jcli (#174)

* Add a command to generate the whole documents for jcli

* Use anchor in the md document
上级 b8b4cd52
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"path"
"path/filepath"
"strings"
"time"
)
func init() {
rootCmd.AddCommand(docCmd)
}
const (
gendocFrontmatterTemplate = `---
date: %s
title: "%s"
anchor: %s
url: %s
---
`
)
var docCmd = &cobra.Command{
Use: "doc",
Short: "Genereate document",
Long: `Genereate document`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
return
}
now := time.Now().Format(time.RFC3339)
prepender := func(filename string) string {
name := filepath.Base(filename)
base := strings.TrimSuffix(name, path.Ext(name))
url := "/commands/" + strings.ToLower(base) + "/"
return fmt.Sprintf(gendocFrontmatterTemplate, now, strings.Replace(base, "_", " ", -1), base, url)
}
linkHandler := func(name string) string {
base := strings.TrimSuffix(name, path.Ext(name))
return "/commands/" + strings.ToLower(base) + "/"
}
outputDir := args[0]
err := doc.GenMarkdownTreeCustom(rootCmd, outputDir, prepender, linkHandler)
if err != nil {
cmd.PrintErr(err)
}
},
}
package cmd
import (
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
"bytes"
"os"
"io/ioutil"
"path/filepath"
)
var _ = Describe("doc command test", func() {
var (
ctrl *gomock.Controller
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
config = nil
})
AfterEach(func() {
config = nil
ctrl.Finish()
})
Context("basic test", func() {
It("lack of arguments", func() {
buf := new(bytes.Buffer)
rootCmd.SetOutput(buf)
docCmd.SetHelpFunc(func(cmd *cobra.Command, _ []string) {
cmd.Print("help")
})
rootCmd.SetArgs([]string{"doc"})
_, err := rootCmd.ExecuteC()
Expect(err).To(BeNil())
Expect(buf.String()).To(Equal("help"))
})
It("should success", func(){
buf := new(bytes.Buffer)
rootCmd.SetOutput(buf)
tmpdir, err := ioutil.TempDir("", "test-gen-cmd-tree")
Expect(err).To(BeNil())
defer os.RemoveAll(tmpdir)
rootCmd.SetArgs([]string{"doc", tmpdir})
_, err = rootCmd.ExecuteC()
Expect(err).To(BeNil())
Expect(buf.String()).To(Equal(""))
_, err = os.Stat(filepath.Join(tmpdir, "jcli_doc.md"))
Expect(err).To(BeNil())
})
})
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册