2017-06-01 20:25:19 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2017-06-02 10:54:54 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/commands"
|
2017-06-01 20:25:19 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
const descriptionSourcePath = "docs/reference/commandline/"
|
|
|
|
|
|
|
|
func generateCliYaml(opts *options) error {
|
2021-03-31 17:36:03 -04:00
|
|
|
dockerCLI, err := command.NewDockerCli()
|
2019-01-28 08:52:58 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-19 13:51:12 -04:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "docker [OPTIONS] COMMAND [ARG...]",
|
|
|
|
Short: "The base command for the Docker CLI.",
|
|
|
|
}
|
2021-03-31 17:36:03 -04:00
|
|
|
commands.AddCommands(cmd, dockerCLI)
|
2018-11-29 09:39:07 -05:00
|
|
|
disableFlagsInUseLine(cmd)
|
2017-06-01 20:25:19 -04:00
|
|
|
source := filepath.Join(opts.source, descriptionSourcePath)
|
2020-03-15 08:13:38 -04:00
|
|
|
fmt.Println("Markdown source:", source)
|
2017-06-01 20:25:19 -04:00
|
|
|
if err := loadLongDescription(cmd, source); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:36:03 -04:00
|
|
|
if err := os.MkdirAll(opts.target, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-01 20:25:19 -04:00
|
|
|
cmd.DisableAutoGenTag = true
|
|
|
|
return GenYamlTree(cmd, opts.target)
|
|
|
|
}
|
|
|
|
|
2018-11-29 09:39:07 -05:00
|
|
|
func disableFlagsInUseLine(cmd *cobra.Command) {
|
|
|
|
visitAll(cmd, func(ccmd *cobra.Command) {
|
|
|
|
// do not add a `[flags]` to the end of the usage line.
|
|
|
|
ccmd.DisableFlagsInUseLine = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// visitAll will traverse all commands from the root.
|
|
|
|
// This is different from the VisitAll of cobra.Command where only parents
|
|
|
|
// are checked.
|
|
|
|
func visitAll(root *cobra.Command, fn func(*cobra.Command)) {
|
|
|
|
for _, cmd := range root.Commands() {
|
|
|
|
visitAll(cmd, fn)
|
|
|
|
}
|
|
|
|
fn(root)
|
|
|
|
}
|
|
|
|
|
2020-03-15 08:13:38 -04:00
|
|
|
func loadLongDescription(parentCmd *cobra.Command, path string) error {
|
|
|
|
for _, cmd := range parentCmd.Commands() {
|
2017-06-01 20:25:19 -04:00
|
|
|
if cmd.HasSubCommands() {
|
2020-03-15 08:13:38 -04:00
|
|
|
if err := loadLongDescription(cmd, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
2020-03-15 08:13:38 -04:00
|
|
|
name := cmd.CommandPath()
|
|
|
|
log.Println("INFO: Generating docs for", name)
|
|
|
|
if i := strings.Index(name, " "); i >= 0 {
|
|
|
|
// remove root command / binary name
|
|
|
|
name = name[i+1:]
|
|
|
|
}
|
|
|
|
if name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mdFile := strings.ReplaceAll(name, " ", "_") + ".md"
|
|
|
|
fullPath := filepath.Join(path, mdFile)
|
|
|
|
content, err := ioutil.ReadFile(fullPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Printf("WARN: %s does not exist, skipping\n", mdFile)
|
2017-06-01 20:25:19 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-31 17:36:03 -04:00
|
|
|
applyDescriptionAndExamples(cmd, string(content))
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type options struct {
|
|
|
|
source string
|
|
|
|
target string
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseArgs() (*options, error) {
|
|
|
|
opts := &options{}
|
|
|
|
cwd, _ := os.Getwd()
|
|
|
|
flags := pflag.NewFlagSet(os.Args[0], pflag.ContinueOnError)
|
|
|
|
flags.StringVar(&opts.source, "root", cwd, "Path to project root")
|
|
|
|
flags.StringVar(&opts.target, "target", "/tmp", "Target path for generated yaml files")
|
|
|
|
err := flags.Parse(os.Args[1:])
|
|
|
|
return opts, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
opts, err := parseArgs()
|
|
|
|
if err != nil {
|
2020-03-15 08:13:38 -04:00
|
|
|
log.Println(err)
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
2020-03-15 08:13:38 -04:00
|
|
|
fmt.Println("Project root: ", opts.source)
|
|
|
|
fmt.Println("YAML output dir:", opts.target)
|
2017-06-01 20:25:19 -04:00
|
|
|
if err := generateCliYaml(opts); err != nil {
|
2020-03-15 08:13:38 -04:00
|
|
|
log.Println("Failed to generate yaml files:", err)
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
|
|
|
}
|