mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
fa86894550
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
Former-commit-id: 984c56e0b9a9169b10c6017fbd68ab4fbd3868d7 [formerly 27c43314222c723a220b9b1d2141e1509ed05627] [formerly 0a9f6c47bff2d653035c93765ea08ade73ec450c [formerly b7fdcc3ee9
]]
Former-commit-id: c27e7fa41f20f433a9a0a97ecc40ab78968b43dc [formerly 185db4a17969cd4fb76cc2b06bd58221c9c6c100]
Former-commit-id: 9b26d1b0642c61cd38f7cdf422f95b2bf9a9614d
140 lines
3.0 KiB
Go
140 lines
3.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
)
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(docsCmd)
|
|
docsCmd.Flags().StringP("path", "p", "./docs", "path to save the docs")
|
|
}
|
|
|
|
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.Flags(), "path")
|
|
generateDocs(rootCmd, dir)
|
|
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
|
|
}
|
|
|
|
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("---\ndescription: " + short + "\n---\n\n")
|
|
buf.WriteString("# " + name + "\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 generateFlagsTable(fs *pflag.FlagSet, buf io.StringWriter) {
|
|
buf.WriteString("| Name | Shorthand | Usage |\n")
|
|
buf.WriteString("|------|-----------|-------|\n")
|
|
|
|
fs.VisitAll(func(f *pflag.Flag) {
|
|
buf.WriteString("|" + f.Name + "|" + f.Shorthand + "|" + f.Usage + "|\n")
|
|
})
|
|
}
|
|
|
|
func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) {
|
|
flags := cmd.NonInheritedFlags()
|
|
flags.SetOutput(buf)
|
|
if flags.HasAvailableFlags() {
|
|
buf.WriteString("## Options\n\n")
|
|
generateFlagsTable(flags, buf)
|
|
buf.WriteString("\n")
|
|
}
|
|
|
|
parentFlags := cmd.InheritedFlags()
|
|
parentFlags.SetOutput(buf)
|
|
if parentFlags.HasAvailableFlags() {
|
|
buf.WriteString("### Inherited\n\n")
|
|
generateFlagsTable(parentFlags, buf)
|
|
buf.WriteString("\n")
|
|
}
|
|
}
|