From 2a9f983d949bab714315e95419a659b5cc107d91 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 5 Jan 2019 14:39:55 +0000 Subject: [PATCH] feat: generate docs License: MIT Signed-off-by: Henrique Dias --- cmd/cmd.go | 6 +-- cmd/docs.go | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 + go.sum | 5 +++ 4 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 cmd/docs.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 10863f40..18f52337 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -1,14 +1,12 @@ package cmd import ( - "fmt" - "os" + "log" ) // Execute executes the commands. func Execute() { if err := rootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) + log.Fatal(err) } } diff --git a/cmd/docs.go b/cmd/docs.go new file mode 100644 index 00000000..7a8c901a --- /dev/null +++ b/cmd/docs.go @@ -0,0 +1,106 @@ +package cmd + +import ( + "bufio" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "sort" + "strings" + + "github.com/spf13/cobra" + "github.com/spf13/cobra/doc" +) + +func init() { + rootCmd.AddCommand(docsCmd) + 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) { + for i, name := range names { + name = strings.TrimSuffix(name, filepath.Ext(name)) + name = strings.Replace(name, "-", " ", -1) + names[i] = name + } + + sort.Strings(names) + + toc := "" + for _, name := range names { + toc += "* [" + name + "](cli/" + strings.Replace(name, " ", "-", -1) + ".md)\n" + } + + fmt.Println(toc) +} + +var docsCmd = &cobra.Command{ + Use: "docs", + Hidden: true, + 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) + + names := []string{} + + err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil || info.IsDir() { + return err + } + + if !strings.HasPrefix(info.Name(), "filebrowser") { + 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) + }) + + checkErr(err) + printToc(names) + }, +} diff --git a/go.mod b/go.mod index 9341d944..65afb31b 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ 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 @@ -28,6 +29,7 @@ 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 bdaa211d..096984b8 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ 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= @@ -51,11 +53,14 @@ github.com/mholt/caddy v0.11.1 h1:oNfejqftVesLoFxw53Gh17aBPNbTxQ9xJw1pn4IiAPk= github.com/mholt/caddy v0.11.1/go.mod h1:Wb1PlT4DAYSqOEd03MsqkdkXnTxA8v9pKjdpxbqM1kY= github.com/nwaples/rardecode v1.0.0 h1:r7vGuS5akxOnR4JQSkko62RJ1ReCMXxQRPtxsiFMBOs= github.com/nwaples/rardecode v1.0.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= +github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM8aXeqhl0I= 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=