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"
|
2023-07-11 01:34:10 +08:00
|
|
|
"fmt"
|
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"
|
2023-07-11 01:34:10 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
Source string
|
2024-08-15 20:58:45 +08:00
|
|
|
Aliases []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 }}"
|
2024-08-15 20:58:45 +08:00
|
|
|
{{- if .Aliases }}
|
|
|
|
aliases:
|
|
|
|
{{- range $value := .Aliases }}
|
|
|
|
- {{ $value }}
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
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.`,
|
2022-11-27 06:40:49 +08:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.33",
|
|
|
|
},
|
2016-08-06 00:12:27 +08:00
|
|
|
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
|
2024-08-15 20:58:45 +08:00
|
|
|
Aliases []string
|
2022-11-26 06:41:23 +08:00
|
|
|
Annotations map[string]string
|
|
|
|
}
|
|
|
|
var commands = map[string]commandDetails{}
|
2024-08-15 20:58:45 +08:00
|
|
|
var addCommandDetails func(root *cobra.Command, parentAliases []string)
|
|
|
|
addCommandDetails = func(root *cobra.Command, parentAliases []string) {
|
2022-05-17 00:11:45 +08:00
|
|
|
name := strings.ReplaceAll(root.CommandPath(), " ", "_") + ".md"
|
2024-08-15 20:58:45 +08:00
|
|
|
var aliases []string
|
|
|
|
for _, p := range parentAliases {
|
|
|
|
aliases = append(aliases, p+" "+root.Name())
|
|
|
|
for _, v := range root.Aliases {
|
|
|
|
aliases = append(aliases, p+" "+v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range root.Aliases {
|
|
|
|
if root.HasParent() {
|
|
|
|
aliases = append(aliases, root.Parent().CommandPath()+" "+v)
|
|
|
|
} else {
|
|
|
|
aliases = append(aliases, v)
|
|
|
|
}
|
|
|
|
}
|
2022-11-26 06:41:23 +08:00
|
|
|
commands[name] = commandDetails{
|
|
|
|
Short: root.Short,
|
2024-08-15 20:58:45 +08:00
|
|
|
Aliases: aliases,
|
2022-11-26 06:41:23 +08:00
|
|
|
Annotations: root.Annotations,
|
|
|
|
}
|
2020-05-16 22:11:55 +08:00
|
|
|
for _, c := range root.Commands() {
|
2024-08-15 20:58:45 +08:00
|
|
|
addCommandDetails(c, aliases)
|
2020-05-16 22:11:55 +08:00
|
|
|
}
|
|
|
|
}
|
2024-08-15 20:58:45 +08:00
|
|
|
addCommandDetails(cmd.Root, []string{})
|
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,
|
2022-05-17 00:11:45 +08:00
|
|
|
Source: strings.ReplaceAll(strings.ReplaceAll(base, "rclone", "cmd"), "_", "/") + "/",
|
2024-08-15 20:58:45 +08:00
|
|
|
Aliases: []string{},
|
2024-06-15 19:59:49 +08:00
|
|
|
Annotations: map[string]string{},
|
|
|
|
}
|
2024-08-15 20:58:45 +08:00
|
|
|
for _, v := range commands[name].Aliases {
|
|
|
|
data.Aliases = append(data.Aliases, "/commands/"+strings.ReplaceAll(v, " ", "_")+"/")
|
|
|
|
}
|
2024-06-15 19:59:49 +08:00
|
|
|
// Filter out annotations that confuse hugo from the frontmatter
|
|
|
|
for k, v := range commands[name].Annotations {
|
|
|
|
if k != "groups" {
|
|
|
|
data.Annotations[k] = v
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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() {
|
2023-07-11 01:34:10 +08:00
|
|
|
name := filepath.Base(path)
|
|
|
|
cmd, ok := commands[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("didn't find command for %q", name)
|
|
|
|
}
|
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)
|
2023-07-11 01:34:10 +08:00
|
|
|
|
2024-08-12 22:55:49 +08:00
|
|
|
startCut := strings.Index(doc, `### Options inherited from parent commands`)
|
|
|
|
endCut := strings.Index(doc, `### SEE ALSO`)
|
|
|
|
if startCut < 0 || endCut < 0 {
|
|
|
|
if name != "rclone.md" {
|
|
|
|
return fmt.Errorf("internal error: failed to find cut points: startCut = %d, endCut = %d", startCut, endCut)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var out strings.Builder
|
|
|
|
if groupsString := cmd.Annotations["groups"]; groupsString != "" {
|
|
|
|
groups := flags.All.Include(groupsString)
|
|
|
|
for _, group := range groups.Groups {
|
|
|
|
if group.Flags.HasFlags() {
|
|
|
|
_, _ = fmt.Fprintf(&out, "\n### %s Options\n\n", group.Name)
|
|
|
|
_, _ = fmt.Fprintf(&out, "%s\n\n", group.Help)
|
|
|
|
_, _ = fmt.Fprintln(&out, "```")
|
|
|
|
_, _ = out.WriteString(group.Flags.FlagUsages())
|
|
|
|
_, _ = fmt.Fprintln(&out, "```")
|
|
|
|
}
|
2023-07-11 01:34:10 +08:00
|
|
|
}
|
|
|
|
}
|
2024-08-12 22:55:49 +08:00
|
|
|
_, _ = out.WriteString(`
|
2019-06-20 23:18:02 +08:00
|
|
|
See the [global flags page](/flags/) for global options not listed here.
|
|
|
|
|
2023-07-11 01:34:10 +08:00
|
|
|
`)
|
2024-08-12 22:55:49 +08:00
|
|
|
doc = doc[:startCut] + out.String() + doc[endCut:]
|
2023-07-11 01:34:10 +08:00
|
|
|
}
|
2024-08-12 22:55:49 +08:00
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|