diff --git a/cmd/docs.go b/cmd/docs.go index 7a8c901a..2adc343d 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -1,16 +1,15 @@ package cmd import ( - "bufio" + "bytes" "fmt" - "io/ioutil" + "io" "os" "path/filepath" "sort" "strings" "github.com/spf13/cobra" - "github.com/spf13/cobra/doc" ) func init() { @@ -18,17 +17,7 @@ func init() { docsCmd.Flags().StringP("path", "p", "./docs", "path to save the docs") } -func removeAll (dir string) error { - return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { - if strings.HasPrefix(info.Name(), "filebrowser") { - return os.Remove(path) - } - - return nil - }) -} - -func printToc (names []string) { +func printToc(names []string) { for i, name := range names { name = strings.TrimSuffix(name, filepath.Ext(name)) name = strings.Replace(name, "-", " ", -1) @@ -42,7 +31,7 @@ func printToc (names []string) { toc += "* [" + name + "](cli/" + strings.Replace(name, " ", "-", -1) + ".md)\n" } - fmt.Println(toc) + fmt.Println(toc) } var docsCmd = &cobra.Command{ @@ -51,15 +40,10 @@ var docsCmd = &cobra.Command{ Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { dir := mustGetString(cmd, "path") - - err := removeAll(dir) - checkErr(err) - err = doc.GenMarkdownTree(rootCmd, dir) - checkErr(err) - + generateDocs(rootCmd, dir) names := []string{} - err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil || info.IsDir() { return err } @@ -68,39 +52,78 @@ var docsCmd = &cobra.Command{ return nil } - name := strings.Replace(info.Name(), "_", "-", -1) - names = append(names, name) - newPath := filepath.Join(dir, name) - err = os.Rename(path, newPath) - if err != nil { - return err - } - path = newPath - - fd, err := os.OpenFile(path, os.O_RDONLY, os.ModePerm) - if err != nil { - return err - } - defer fd.Close() - - content := "" - sc := bufio.NewScanner(fd) - for sc.Scan() { - txt := sc.Text() - if txt == "### SEE ALSO" { - break - } - content += txt + "\n" - } - if err := sc.Err(); err != nil { - return err - } - - content = strings.TrimSpace(content) + "\n" - return ioutil.WriteFile(path, []byte(content), 077) + names = append(names, info.Name()) + return nil }) checkErr(err) printToc(names) }, } + +func generateDocs(cmd *cobra.Command, dir string) { + for _, c := range cmd.Commands() { + if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() { + continue + } + + generateDocs(c, dir) + } + + basename := strings.Replace(cmd.CommandPath(), " ", "-", -1) + ".md" + filename := filepath.Join(dir, basename) + f, err := os.Create(filename) + checkErr(err) + defer f.Close() + generateMarkdown(cmd, f) +} + +func generateMarkdown(cmd *cobra.Command, w io.Writer) { + cmd.InitDefaultHelpCmd() + cmd.InitDefaultHelpFlag() + + buf := new(bytes.Buffer) + name := cmd.CommandPath() + + short := cmd.Short + long := cmd.Long + if len(long) == 0 { + long = short + } + + buf.WriteString("# " + name + "\n\n") + buf.WriteString(short + "\n\n") + buf.WriteString("## Synopsis\n\n") + buf.WriteString(long + "\n\n") + + if cmd.Runnable() { + buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.UseLine())) + } + + if len(cmd.Example) > 0 { + buf.WriteString("## Examples\n\n") + buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.Example)) + } + + printOptions(buf, cmd, name) + _, err := buf.WriteTo(w) + checkErr(err) +} + +func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) { + flags := cmd.NonInheritedFlags() + flags.SetOutput(buf) + if flags.HasAvailableFlags() { + buf.WriteString("## Options\n\n```\n") + flags.PrintDefaults() + buf.WriteString("```\n\n") + } + + parentFlags := cmd.InheritedFlags() + parentFlags.SetOutput(buf) + if parentFlags.HasAvailableFlags() { + buf.WriteString("## Options inherited from parent commands\n\n```\n") + parentFlags.PrintDefaults() + buf.WriteString("```\n") + } +} diff --git a/go.mod b/go.mod index 65afb31b..9341d944 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/Sereal/Sereal v0.0.0-20180905114147-563b78806e28 // indirect github.com/asdine/storm v2.1.2+incompatible github.com/boltdb/bolt v1.3.1 // indirect - github.com/cpuguy83/go-md2man v1.0.8 // indirect github.com/daaku/go.zipexe v0.0.0-20150329023125-a5fe2436ffcb // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgrijalva/jwt-go v3.2.0+incompatible @@ -29,7 +28,6 @@ require ( github.com/pelletier/go-toml v1.2.0 github.com/pierrec/lz4 v2.0.5+incompatible // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/russross/blackfriday v1.5.1 // indirect github.com/spf13/afero v1.1.2 github.com/spf13/cobra v0.0.3 github.com/spf13/pflag v1.0.3 diff --git a/go.sum b/go.sum index 096984b8..7b355bef 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,6 @@ github.com/asdine/storm v2.1.2+incompatible h1:dczuIkyqwY2LrtXPz8ixMrU/OFgZp71kb github.com/asdine/storm v2.1.2+incompatible/go.mod h1:RarYDc9hq1UPLImuiXK3BIWPJLdIygvV3PsInK0FbVQ= github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= -github.com/cpuguy83/go-md2man v1.0.8 h1:DwoNytLphI8hzS2Af4D0dfaEaiSq2bN05mEm4R6vf8M= -github.com/cpuguy83/go-md2man v1.0.8/go.mod h1:N6JayAiVKtlHSnuTCeuLSQVs75hb8q+dYQLjr7cDsKY= github.com/daaku/go.zipexe v0.0.0-20150329023125-a5fe2436ffcb h1:tUf55Po0vzOendQ7NWytcdK0VuzQmfAgvGBUOQvN0WA= github.com/daaku/go.zipexe v0.0.0-20150329023125-a5fe2436ffcb/go.mod h1:U0vRfAucUOohvdCxt5MWLF+TePIL0xbCkbKIiV8TQCE= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -59,8 +57,6 @@ github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/russross/blackfriday v1.5.1 h1:B8ZN6pD4PVofmlDCDUdELeYrbsVIDM/bpjW3v3zgcRc= -github.com/russross/blackfriday v1.5.1/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=