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