2017-06-01 20:25:19 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
2017-09-26 12:33:35 -04:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2017-06-01 20:25:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdOption struct {
|
2018-01-02 17:56:07 -05:00
|
|
|
Option string
|
|
|
|
Shorthand string `yaml:",omitempty"`
|
|
|
|
ValueType string `yaml:"value_type,omitempty"`
|
|
|
|
DefaultValue string `yaml:"default_value,omitempty"`
|
|
|
|
Description string `yaml:",omitempty"`
|
2021-03-31 17:36:03 -04:00
|
|
|
DetailsURL string `yaml:"details_url,omitempty"` // DetailsURL contains an anchor-id or link for more information on this flag
|
2018-01-02 17:56:07 -05:00
|
|
|
Deprecated bool
|
|
|
|
MinAPIVersion string `yaml:"min_api_version,omitempty"`
|
|
|
|
Experimental bool
|
|
|
|
ExperimentalCLI bool
|
|
|
|
Kubernetes bool
|
|
|
|
Swarm bool
|
2018-05-30 17:17:06 -04:00
|
|
|
OSType string `yaml:"os_type,omitempty"`
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type cmdDoc struct {
|
|
|
|
Name string `yaml:"command"`
|
|
|
|
SeeAlso []string `yaml:"parent,omitempty"`
|
|
|
|
Version string `yaml:"engine_version,omitempty"`
|
|
|
|
Aliases string `yaml:",omitempty"`
|
|
|
|
Short string `yaml:",omitempty"`
|
|
|
|
Long string `yaml:",omitempty"`
|
|
|
|
Usage string `yaml:",omitempty"`
|
|
|
|
Pname string `yaml:",omitempty"`
|
|
|
|
Plink string `yaml:",omitempty"`
|
|
|
|
Cname []string `yaml:",omitempty"`
|
|
|
|
Clink []string `yaml:",omitempty"`
|
|
|
|
Options []cmdOption `yaml:",omitempty"`
|
|
|
|
InheritedOptions []cmdOption `yaml:"inherited_options,omitempty"`
|
|
|
|
Example string `yaml:"examples,omitempty"`
|
2017-09-12 05:24:45 -04:00
|
|
|
Deprecated bool
|
|
|
|
MinAPIVersion string `yaml:"min_api_version,omitempty"`
|
|
|
|
Experimental bool
|
2018-01-02 17:56:07 -05:00
|
|
|
ExperimentalCLI bool
|
|
|
|
Kubernetes bool
|
|
|
|
Swarm bool
|
2018-05-30 17:17:06 -04:00
|
|
|
OSType string `yaml:"os_type,omitempty"`
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenYamlTree creates yaml structured ref files
|
|
|
|
func GenYamlTree(cmd *cobra.Command, dir string) error {
|
|
|
|
emptyStr := func(s string) string { return "" }
|
2017-09-26 12:33:35 -04:00
|
|
|
return GenYamlTreeCustom(cmd, dir, emptyStr)
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenYamlTreeCustom creates yaml structured ref files
|
2017-09-26 12:33:35 -04:00
|
|
|
func GenYamlTreeCustom(cmd *cobra.Command, dir string, filePrepender func(string) string) error {
|
2017-06-01 20:25:19 -04:00
|
|
|
for _, c := range cmd.Commands() {
|
2021-03-31 17:36:03 -04:00
|
|
|
if !c.Runnable() && !c.HasAvailableSubCommands() {
|
|
|
|
// skip non-runnable commands without subcommands
|
|
|
|
// but *do* generate YAML for hidden and deprecated commands
|
|
|
|
// the YAML will have those included as metadata, so that the
|
|
|
|
// documentation repository can decide whether or not to present them
|
2017-06-01 20:25:19 -04:00
|
|
|
continue
|
|
|
|
}
|
2017-09-26 12:33:35 -04:00
|
|
|
if err := GenYamlTreeCustom(c, dir, filePrepender); err != nil {
|
2017-06-01 20:25:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 08:12:11 -04:00
|
|
|
|
|
|
|
// TODO: conditionally skip the root command (for plugins)
|
|
|
|
//
|
|
|
|
// The "root" command used in the generator is just a "stub", and only has a
|
|
|
|
// list of subcommands, but not (e.g.) global options/flags. We should fix
|
|
|
|
// that, so that the YAML file for the docker "root" command contains the
|
|
|
|
// global flags.
|
|
|
|
//
|
|
|
|
// If we're using this code to generate YAML docs for a plugin, the root-
|
|
|
|
// command is even less useful; in that case, the root command represents
|
|
|
|
// the "docker" command, and is a "dummy" with no flags, and only a single
|
|
|
|
// subcommand (the plugin's top command). For plugins, we should skip the
|
|
|
|
// root command altogether, to prevent generating a useless YAML file.
|
|
|
|
// if !cmd.HasParent() {
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
|
2017-06-01 20:25:19 -04:00
|
|
|
basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".yaml"
|
|
|
|
filename := filepath.Join(dir, basename)
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if _, err := io.WriteString(f, filePrepender(filename)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-13 07:16:34 -05:00
|
|
|
return GenYamlCustom(cmd, f)
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenYamlCustom creates custom yaml output
|
2017-09-12 05:24:45 -04:00
|
|
|
// nolint: gocyclo
|
2017-06-09 17:16:56 -04:00
|
|
|
func GenYamlCustom(cmd *cobra.Command, w io.Writer) error {
|
2021-03-31 17:36:03 -04:00
|
|
|
const (
|
|
|
|
// shortMaxWidth is the maximum width for the "Short" description before
|
|
|
|
// we force YAML to use multi-line syntax. The goal is to make the total
|
|
|
|
// width fit within 80 characters. This value is based on 80 characters
|
|
|
|
// minus the with of the field, colon, and whitespace ('short: ').
|
|
|
|
shortMaxWidth = 73
|
|
|
|
|
|
|
|
// longMaxWidth is the maximum width for the "Short" description before
|
|
|
|
// we force YAML to use multi-line syntax. The goal is to make the total
|
|
|
|
// width fit within 80 characters. This value is based on 80 characters
|
|
|
|
// minus the with of the field, colon, and whitespace ('long: ').
|
|
|
|
longMaxWidth = 74
|
|
|
|
)
|
|
|
|
|
|
|
|
cliDoc := cmdDoc{
|
|
|
|
Name: cmd.CommandPath(),
|
|
|
|
Aliases: strings.Join(cmd.Aliases, ", "),
|
|
|
|
Short: forceMultiLine(cmd.Short, shortMaxWidth),
|
|
|
|
Long: forceMultiLine(cmd.Long, longMaxWidth),
|
|
|
|
Example: cmd.Example,
|
|
|
|
Deprecated: len(cmd.Deprecated) > 0,
|
|
|
|
}
|
2017-06-01 20:25:19 -04:00
|
|
|
|
|
|
|
if len(cliDoc.Long) == 0 {
|
|
|
|
cliDoc.Long = cliDoc.Short
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Runnable() {
|
2018-05-18 20:46:27 -04:00
|
|
|
cliDoc.Usage = cmd.UseLine()
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
|
|
|
|
2017-09-12 05:24:45 -04:00
|
|
|
// Check recursively so that, e.g., `docker stack ls` returns the same output as `docker stack`
|
|
|
|
for curr := cmd; curr != nil; curr = curr.Parent() {
|
2017-10-25 12:59:32 -04:00
|
|
|
if v, ok := curr.Annotations["version"]; ok && cliDoc.MinAPIVersion == "" {
|
2017-09-12 05:24:45 -04:00
|
|
|
cliDoc.MinAPIVersion = v
|
|
|
|
}
|
2017-10-25 12:59:32 -04:00
|
|
|
if _, ok := curr.Annotations["experimental"]; ok && !cliDoc.Experimental {
|
2017-09-12 05:24:45 -04:00
|
|
|
cliDoc.Experimental = true
|
|
|
|
}
|
2018-01-02 17:56:07 -05:00
|
|
|
if _, ok := curr.Annotations["experimentalCLI"]; ok && !cliDoc.ExperimentalCLI {
|
|
|
|
cliDoc.ExperimentalCLI = true
|
|
|
|
}
|
|
|
|
if _, ok := curr.Annotations["kubernetes"]; ok && !cliDoc.Kubernetes {
|
|
|
|
cliDoc.Kubernetes = true
|
|
|
|
}
|
|
|
|
if _, ok := curr.Annotations["swarm"]; ok && !cliDoc.Swarm {
|
2018-01-11 12:30:50 -05:00
|
|
|
cliDoc.Swarm = true
|
2018-01-02 17:56:07 -05:00
|
|
|
}
|
2021-03-31 17:36:03 -04:00
|
|
|
if o, ok := curr.Annotations["ostype"]; ok && cliDoc.OSType == "" {
|
|
|
|
cliDoc.OSType = o
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
anchors := make(map[string]struct{})
|
|
|
|
if a, ok := cmd.Annotations["anchors"]; ok && a != "" {
|
|
|
|
for _, anchor := range strings.Split(a, ",") {
|
|
|
|
anchors[anchor] = struct{}{}
|
2018-05-30 17:17:06 -04:00
|
|
|
}
|
2017-09-12 05:24:45 -04:00
|
|
|
}
|
2017-06-01 20:25:19 -04:00
|
|
|
|
|
|
|
flags := cmd.NonInheritedFlags()
|
|
|
|
if flags.HasFlags() {
|
2021-03-31 17:36:03 -04:00
|
|
|
cliDoc.Options = genFlagResult(flags, anchors)
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
|
|
|
flags = cmd.InheritedFlags()
|
|
|
|
if flags.HasFlags() {
|
2021-03-31 17:36:03 -04:00
|
|
|
cliDoc.InheritedOptions = genFlagResult(flags, anchors)
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if hasSeeAlso(cmd) {
|
|
|
|
if cmd.HasParent() {
|
|
|
|
parent := cmd.Parent()
|
|
|
|
cliDoc.Pname = parent.CommandPath()
|
2021-03-31 17:36:03 -04:00
|
|
|
cliDoc.Plink = strings.Replace(cliDoc.Pname, " ", "_", -1) + ".yaml"
|
2017-06-01 20:25:19 -04:00
|
|
|
cmd.VisitParents(func(c *cobra.Command) {
|
|
|
|
if c.DisableAutoGenTag {
|
|
|
|
cmd.DisableAutoGenTag = c.DisableAutoGenTag
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
children := cmd.Commands()
|
|
|
|
sort.Sort(byName(children))
|
|
|
|
|
|
|
|
for _, child := range children {
|
2017-10-25 12:59:32 -04:00
|
|
|
if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
|
2017-06-01 20:25:19 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
cliDoc.Cname = append(cliDoc.Cname, cliDoc.Name+" "+child.Name())
|
2021-03-31 17:36:03 -04:00
|
|
|
cliDoc.Clink = append(cliDoc.Clink, strings.Replace(cliDoc.Name+"_"+child.Name(), " ", "_", -1)+".yaml")
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final, err := yaml.Marshal(&cliDoc)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprintln(w, string(final)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:36:03 -04:00
|
|
|
func genFlagResult(flags *pflag.FlagSet, anchors map[string]struct{}) []cmdOption {
|
2017-09-12 05:02:51 -04:00
|
|
|
var (
|
|
|
|
result []cmdOption
|
|
|
|
opt cmdOption
|
|
|
|
)
|
2017-06-01 20:25:19 -04:00
|
|
|
|
2021-03-31 17:36:03 -04:00
|
|
|
const (
|
|
|
|
// shortMaxWidth is the maximum width for the "Short" description before
|
|
|
|
// we force YAML to use multi-line syntax. The goal is to make the total
|
|
|
|
// width fit within 80 characters. This value is based on 80 characters
|
|
|
|
// minus the with of the field, colon, and whitespace (' default_value: ').
|
|
|
|
defaultValueMaxWidth = 64
|
|
|
|
|
|
|
|
// longMaxWidth is the maximum width for the "Short" description before
|
|
|
|
// we force YAML to use multi-line syntax. The goal is to make the total
|
|
|
|
// width fit within 80 characters. This value is based on 80 characters
|
|
|
|
// minus the with of the field, colon, and whitespace (' description: ').
|
|
|
|
descriptionMaxWidth = 66
|
|
|
|
)
|
|
|
|
|
2017-06-01 20:25:19 -04:00
|
|
|
flags.VisitAll(func(flag *pflag.Flag) {
|
2017-09-12 05:02:51 -04:00
|
|
|
opt = cmdOption{
|
|
|
|
Option: flag.Name,
|
|
|
|
ValueType: flag.Value.Type(),
|
2021-03-31 17:36:03 -04:00
|
|
|
DefaultValue: forceMultiLine(flag.DefValue, defaultValueMaxWidth),
|
|
|
|
Description: forceMultiLine(flag.Usage, descriptionMaxWidth),
|
2017-09-12 05:02:51 -04:00
|
|
|
Deprecated: len(flag.Deprecated) > 0,
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:36:03 -04:00
|
|
|
if v, ok := flag.Annotations["docs.external.url"]; ok && len(v) > 0 {
|
|
|
|
opt.DetailsURL = strings.TrimPrefix(v[0], "https://docs.docker.com")
|
|
|
|
} else if _, ok = anchors[flag.Name]; ok {
|
|
|
|
opt.DetailsURL = "#" + flag.Name
|
|
|
|
}
|
|
|
|
|
2017-06-01 20:25:19 -04:00
|
|
|
// Todo, when we mark a shorthand is deprecated, but specify an empty message.
|
|
|
|
// The flag.ShorthandDeprecated is empty as the shorthand is deprecated.
|
|
|
|
// Using len(flag.ShorthandDeprecated) > 0 can't handle this, others are ok.
|
|
|
|
if !(len(flag.ShorthandDeprecated) > 0) && len(flag.Shorthand) > 0 {
|
2017-09-12 05:02:51 -04:00
|
|
|
opt.Shorthand = flag.Shorthand
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
2017-09-12 05:02:51 -04:00
|
|
|
if _, ok := flag.Annotations["experimental"]; ok {
|
|
|
|
opt.Experimental = true
|
|
|
|
}
|
2021-06-29 08:48:14 -04:00
|
|
|
if _, ok := flag.Annotations["deprecated"]; ok {
|
|
|
|
opt.Deprecated = true
|
|
|
|
}
|
2017-09-12 05:02:51 -04:00
|
|
|
if v, ok := flag.Annotations["version"]; ok {
|
|
|
|
opt.MinAPIVersion = v[0]
|
|
|
|
}
|
2018-01-02 17:56:07 -05:00
|
|
|
if _, ok := flag.Annotations["experimentalCLI"]; ok {
|
|
|
|
opt.ExperimentalCLI = true
|
|
|
|
}
|
|
|
|
if _, ok := flag.Annotations["kubernetes"]; ok {
|
|
|
|
opt.Kubernetes = true
|
|
|
|
}
|
|
|
|
if _, ok := flag.Annotations["swarm"]; ok {
|
2018-01-11 12:30:50 -05:00
|
|
|
opt.Swarm = true
|
2018-01-02 17:56:07 -05:00
|
|
|
}
|
2017-09-12 05:02:51 -04:00
|
|
|
|
2018-05-30 17:17:06 -04:00
|
|
|
// Note that the annotation can have multiple ostypes set, however, multiple
|
|
|
|
// values are currently not used (and unlikely will).
|
|
|
|
//
|
|
|
|
// To simplify usage of the os_type property in the YAML, and for consistency
|
|
|
|
// with the same property for commands, we're only using the first ostype that's set.
|
|
|
|
if ostypes, ok := flag.Annotations["ostype"]; ok && len(opt.OSType) == 0 && len(ostypes) > 0 {
|
|
|
|
opt.OSType = ostypes[0]
|
|
|
|
}
|
|
|
|
|
2017-09-12 05:02:51 -04:00
|
|
|
result = append(result, opt)
|
2017-06-01 20:25:19 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:36:03 -04:00
|
|
|
// forceMultiLine appends a newline (\n) to strings that are longer than max
|
|
|
|
// to force the yaml lib to use block notation (https://yaml.org/spec/1.2/spec.html#Block)
|
|
|
|
// instead of a single-line string with newlines and tabs encoded("string\nline1\nline2").
|
|
|
|
//
|
|
|
|
// This makes the generated YAML more readable, and easier to review changes.
|
|
|
|
// max can be used to customize the width to keep the whole line < 80 chars.
|
|
|
|
func forceMultiLine(s string, max int) string {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
if len(s) > max && !strings.Contains(s, "\n") {
|
2017-06-01 20:25:19 -04:00
|
|
|
s = s + "\n"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Small duplication for cobra utils
|
|
|
|
func hasSeeAlso(cmd *cobra.Command) bool {
|
|
|
|
if cmd.HasParent() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, c := range cmd.Commands() {
|
2017-10-25 12:59:32 -04:00
|
|
|
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
|
2017-06-01 20:25:19 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:36:03 -04:00
|
|
|
// applyDescriptionAndExamples fills in cmd.Long and cmd.Example with the
|
|
|
|
// "Description" and "Examples" H2 sections in mdString (if present).
|
|
|
|
func applyDescriptionAndExamples(cmd *cobra.Command, mdString string) {
|
|
|
|
sections := getSections(mdString)
|
|
|
|
var (
|
|
|
|
anchors []string
|
|
|
|
md string
|
|
|
|
)
|
|
|
|
if sections["description"] != "" {
|
|
|
|
md, anchors = cleanupMarkDown(sections["description"])
|
|
|
|
cmd.Long = md
|
|
|
|
anchors = append(anchors, md)
|
|
|
|
}
|
|
|
|
if sections["examples"] != "" {
|
|
|
|
md, anchors = cleanupMarkDown(sections["examples"])
|
|
|
|
cmd.Example = md
|
|
|
|
anchors = append(anchors, md)
|
|
|
|
}
|
|
|
|
if len(anchors) > 0 {
|
|
|
|
if cmd.Annotations == nil {
|
|
|
|
cmd.Annotations = make(map[string]string)
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
2021-03-31 17:36:03 -04:00
|
|
|
cmd.Annotations["anchors"] = strings.Join(anchors, ",")
|
2017-06-01 20:25:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type byName []*cobra.Command
|
|
|
|
|
|
|
|
func (s byName) Len() int { return len(s) }
|
|
|
|
func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }
|