2016-08-05 18:12:27 +02:00
|
|
|
package gendocs
|
|
|
|
|
|
|
|
import (
|
2019-06-20 17:18:02 +02:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2020-05-16 16:11:55 +02:00
|
|
|
"log"
|
2016-08-05 18:12:27 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2020-05-22 12:17:37 +02:00
|
|
|
"regexp"
|
2016-08-05 18:12:27 +02:00
|
|
|
"strings"
|
2020-05-16 16:11:55 +02:00
|
|
|
"text/template"
|
2016-08-05 18:12:27 +02:00
|
|
|
"time"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2016-08-05 18:12:27 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/cobra/doc"
|
2019-05-11 00:18:45 +02:00
|
|
|
"github.com/spf13/pflag"
|
2016-08-05 18:12:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-11 17:58:11 +02:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2016-08-05 18:12:27 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 16:11:55 +02:00
|
|
|
// define things which go into the frontmatter
|
|
|
|
type frontmatter struct {
|
|
|
|
Date string
|
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
Slug string
|
|
|
|
URL string
|
|
|
|
Source string
|
|
|
|
}
|
|
|
|
|
|
|
|
var frontmatterTemplate = template.Must(template.New("frontmatter").Parse(`---
|
|
|
|
title: "{{ .Title }}"
|
|
|
|
description: "{{ .Description }}"
|
|
|
|
slug: {{ .Slug }}
|
|
|
|
url: {{ .URL }}
|
|
|
|
# autogenerated - DO NOT EDIT, instead edit the source code in {{ .Source }} and as part of making a release run "make commanddocs"
|
2016-08-05 18:12:27 +02:00
|
|
|
---
|
2020-05-16 16:11:55 +02:00
|
|
|
`))
|
2016-08-05 18:12:27 +02:00
|
|
|
|
2019-10-11 17:58:11 +02:00
|
|
|
var commandDefinition = &cobra.Command{
|
2016-08-05 18:12:27 +02:00
|
|
|
Use: "gendocs output_directory",
|
|
|
|
Short: `Output markdown docs for rclone to the directory supplied.`,
|
|
|
|
Long: `
|
|
|
|
This produces markdown docs for the rclone commands to the directory
|
|
|
|
supplied. These are in a format suitable for hugo to render into the
|
|
|
|
rclone.org website.`,
|
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
2019-06-20 17:18:02 +02:00
|
|
|
now := time.Now().Format(time.RFC3339)
|
|
|
|
|
|
|
|
// Create the directory structure
|
|
|
|
root := args[0]
|
|
|
|
out := filepath.Join(root, "commands")
|
2016-08-05 18:12:27 +02:00
|
|
|
err := os.MkdirAll(out, 0777)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-20 17:18:02 +02:00
|
|
|
|
|
|
|
// Write the flags page
|
|
|
|
var buf bytes.Buffer
|
|
|
|
cmd.Root.SetOutput(&buf)
|
|
|
|
cmd.Root.SetArgs([]string{"help", "flags"})
|
|
|
|
cmd.GeneratingDocs = true
|
|
|
|
err = cmd.Root.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-01 14:04:41 +02:00
|
|
|
err = ioutil.WriteFile(filepath.Join(root, "flags.md"), buf.Bytes(), 0777)
|
2019-06-20 17:18:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:11:55 +02:00
|
|
|
// Look up name => description for prepender
|
|
|
|
var description = map[string]string{}
|
|
|
|
var addDescription func(root *cobra.Command)
|
|
|
|
addDescription = func(root *cobra.Command) {
|
|
|
|
name := strings.Replace(root.CommandPath(), " ", "_", -1) + ".md"
|
|
|
|
description[name] = root.Short
|
|
|
|
for _, c := range root.Commands() {
|
|
|
|
addDescription(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addDescription(cmd.Root)
|
|
|
|
|
2019-06-20 17:18:02 +02:00
|
|
|
// markup for the docs files
|
2016-08-05 18:12:27 +02:00
|
|
|
prepender := func(filename string) string {
|
|
|
|
name := filepath.Base(filename)
|
|
|
|
base := strings.TrimSuffix(name, path.Ext(name))
|
2020-05-16 16:11:55 +02:00
|
|
|
data := frontmatter{
|
|
|
|
Date: now,
|
|
|
|
Title: strings.Replace(base, "_", " ", -1),
|
|
|
|
Description: description[name],
|
|
|
|
Slug: base,
|
|
|
|
URL: "/commands/" + strings.ToLower(base) + "/",
|
|
|
|
Source: strings.Replace(strings.Replace(base, "rclone", "cmd", -1), "_", "/", -1) + "/",
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := frontmatterTemplate.Execute(&buf, data)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to render frontmatter template: %v", err)
|
|
|
|
}
|
|
|
|
return buf.String()
|
2016-08-05 18:12:27 +02:00
|
|
|
}
|
|
|
|
linkHandler := func(name string) string {
|
|
|
|
base := strings.TrimSuffix(name, path.Ext(name))
|
|
|
|
return "/commands/" + strings.ToLower(base) + "/"
|
|
|
|
}
|
2019-06-20 17:18:02 +02:00
|
|
|
|
2019-05-11 00:18:45 +02:00
|
|
|
// Hide all of the root entries flags
|
|
|
|
cmd.Root.Flags().VisitAll(func(flag *pflag.Flag) {
|
|
|
|
flag.Hidden = true
|
|
|
|
})
|
2019-06-20 17:18:02 +02:00
|
|
|
err = doc.GenMarkdownTreeCustom(cmd.Root, out, prepender, linkHandler)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-22 12:17:37 +02:00
|
|
|
var outdentTitle = regexp.MustCompile(`(?m)^#(#+)`)
|
|
|
|
|
2019-06-20 17:18:02 +02:00
|
|
|
// Munge the files to add a link to the global flags page
|
|
|
|
err = filepath.Walk(out, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
doc := string(b)
|
|
|
|
doc = strings.Replace(doc, "\n### SEE ALSO", `
|
|
|
|
See the [global flags page](/flags/) for global options not listed here.
|
|
|
|
|
|
|
|
### SEE ALSO`, 1)
|
2020-05-22 12:17:37 +02:00
|
|
|
// outdent all the titles by one
|
|
|
|
doc = outdentTitle.ReplaceAllString(doc, `$1`)
|
2019-06-20 17:18:02 +02:00
|
|
|
err = ioutil.WriteFile(path, []byte(doc), 0777)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-08-05 18:12:27 +02:00
|
|
|
},
|
|
|
|
}
|