2022-08-28 19:21:57 +08:00
|
|
|
// Package gendocs provides the gendocs command.
|
2016-08-06 00:12:27 +08:00
|
|
|
package gendocs
|
|
|
|
|
|
|
|
import (
|
2019-06-20 23:18:02 +08:00
|
|
|
"bytes"
|
2020-05-16 22:11:55 +08:00
|
|
|
"log"
|
2016-08-06 00:12:27 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2020-05-22 18:17:37 +08:00
|
|
|
"regexp"
|
2016-08-06 00:12:27 +08:00
|
|
|
"strings"
|
2020-05-16 22:11:55 +08:00
|
|
|
"text/template"
|
2016-08-06 00:12:27 +08:00
|
|
|
"time"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2021-06-11 06:46:36 +08:00
|
|
|
"github.com/rclone/rclone/lib/file"
|
2016-08-06 00:12:27 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/cobra/doc"
|
2019-05-11 06:18:45 +08:00
|
|
|
"github.com/spf13/pflag"
|
2016-08-06 00:12:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-11 23:58:11 +08:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2016-08-06 00:12:27 +08:00
|
|
|
}
|
|
|
|
|
2020-05-16 22:11:55 +08:00
|
|
|
// define things which go into the frontmatter
|
|
|
|
type frontmatter struct {
|
|
|
|
Date string
|
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
Slug string
|
|
|
|
URL string
|
|
|
|
Source string
|
2022-11-26 06:41:23 +08:00
|
|
|
Annotations map[string]string
|
2020-05-16 22:11:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var frontmatterTemplate = template.Must(template.New("frontmatter").Parse(`---
|
|
|
|
title: "{{ .Title }}"
|
|
|
|
description: "{{ .Description }}"
|
|
|
|
slug: {{ .Slug }}
|
|
|
|
url: {{ .URL }}
|
2022-11-26 06:41:23 +08:00
|
|
|
{{- range $key, $value := .Annotations }}
|
|
|
|
{{ $key }}: {{ $value }}
|
|
|
|
{{- end }}
|
2020-05-16 22:11:55 +08:00
|
|
|
# autogenerated - DO NOT EDIT, instead edit the source code in {{ .Source }} and as part of making a release run "make commanddocs"
|
2016-08-06 00:12:27 +08:00
|
|
|
---
|
2020-05-16 22:11:55 +08:00
|
|
|
`))
|
2016-08-06 00:12:27 +08:00
|
|
|
|
2019-10-11 23:58:11 +08:00
|
|
|
var commandDefinition = &cobra.Command{
|
2016-08-06 00:12:27 +08: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 23:18:02 +08:00
|
|
|
now := time.Now().Format(time.RFC3339)
|
|
|
|
|
|
|
|
// Create the directory structure
|
|
|
|
root := args[0]
|
|
|
|
out := filepath.Join(root, "commands")
|
2021-06-11 06:46:36 +08:00
|
|
|
err := file.MkdirAll(out, 0777)
|
2016-08-06 00:12:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-20 23:18:02 +08: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
|
|
|
|
}
|
2022-08-20 22:38:02 +08:00
|
|
|
err = os.WriteFile(filepath.Join(root, "flags.md"), buf.Bytes(), 0777)
|
2019-06-20 23:18:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-26 06:41:23 +08:00
|
|
|
// Look up name => details for prepender
|
|
|
|
type commandDetails struct {
|
|
|
|
Short string
|
|
|
|
Annotations map[string]string
|
|
|
|
}
|
|
|
|
var commands = map[string]commandDetails{}
|
|
|
|
var addCommandDetails func(root *cobra.Command)
|
|
|
|
addCommandDetails = func(root *cobra.Command) {
|
2022-05-17 00:11:45 +08:00
|
|
|
name := strings.ReplaceAll(root.CommandPath(), " ", "_") + ".md"
|
2022-11-26 06:41:23 +08:00
|
|
|
commands[name] = commandDetails{
|
|
|
|
Short: root.Short,
|
|
|
|
Annotations: root.Annotations,
|
|
|
|
}
|
2020-05-16 22:11:55 +08:00
|
|
|
for _, c := range root.Commands() {
|
2022-11-26 06:41:23 +08:00
|
|
|
addCommandDetails(c)
|
2020-05-16 22:11:55 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-26 06:41:23 +08:00
|
|
|
addCommandDetails(cmd.Root)
|
2020-05-16 22:11:55 +08:00
|
|
|
|
2019-06-20 23:18:02 +08:00
|
|
|
// markup for the docs files
|
2016-08-06 00:12:27 +08:00
|
|
|
prepender := func(filename string) string {
|
|
|
|
name := filepath.Base(filename)
|
|
|
|
base := strings.TrimSuffix(name, path.Ext(name))
|
2020-05-16 22:11:55 +08:00
|
|
|
data := frontmatter{
|
|
|
|
Date: now,
|
2022-05-17 00:11:45 +08:00
|
|
|
Title: strings.ReplaceAll(base, "_", " "),
|
2022-11-26 06:41:23 +08:00
|
|
|
Description: commands[name].Short,
|
2020-05-16 22:11:55 +08:00
|
|
|
Slug: base,
|
|
|
|
URL: "/commands/" + strings.ToLower(base) + "/",
|
2022-05-17 00:11:45 +08:00
|
|
|
Source: strings.ReplaceAll(strings.ReplaceAll(base, "rclone", "cmd"), "_", "/") + "/",
|
2022-11-26 06:41:23 +08:00
|
|
|
Annotations: commands[name].Annotations,
|
2020-05-16 22:11:55 +08:00
|
|
|
}
|
|
|
|
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-06 00:12:27 +08:00
|
|
|
}
|
|
|
|
linkHandler := func(name string) string {
|
|
|
|
base := strings.TrimSuffix(name, path.Ext(name))
|
|
|
|
return "/commands/" + strings.ToLower(base) + "/"
|
|
|
|
}
|
2019-06-20 23:18:02 +08:00
|
|
|
|
2019-05-11 06:18:45 +08:00
|
|
|
// Hide all of the root entries flags
|
|
|
|
cmd.Root.Flags().VisitAll(func(flag *pflag.Flag) {
|
|
|
|
flag.Hidden = true
|
|
|
|
})
|
2019-06-20 23:18:02 +08:00
|
|
|
err = doc.GenMarkdownTreeCustom(cmd.Root, out, prepender, linkHandler)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-22 18:17:37 +08:00
|
|
|
var outdentTitle = regexp.MustCompile(`(?m)^#(#+)`)
|
|
|
|
|
2019-06-20 23:18:02 +08: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() {
|
2022-08-20 22:38:02 +08:00
|
|
|
b, err := os.ReadFile(path)
|
2019-06-20 23:18:02 +08:00
|
|
|
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 18:17:37 +08:00
|
|
|
// outdent all the titles by one
|
|
|
|
doc = outdentTitle.ReplaceAllString(doc, `$1`)
|
2022-08-20 22:38:02 +08:00
|
|
|
err = os.WriteFile(path, []byte(doc), 0777)
|
2019-06-20 23:18:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-08-06 00:12:27 +08:00
|
|
|
},
|
|
|
|
}
|