2016-06-09 11:33:28 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-22 14:11:08 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2016-06-09 11:33:28 -04:00
|
|
|
"os"
|
2016-09-22 14:11:08 -04:00
|
|
|
"path/filepath"
|
2016-06-09 11:33:28 -04:00
|
|
|
|
2017-06-02 10:54:54 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/commands"
|
2016-06-23 11:25:51 -04:00
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/spf13/cobra"
|
2016-06-09 11:33:28 -04:00
|
|
|
"github.com/spf13/cobra/doc"
|
2016-09-22 14:11:08 -04:00
|
|
|
"github.com/spf13/pflag"
|
2016-06-09 11:33:28 -04:00
|
|
|
)
|
|
|
|
|
2016-09-22 14:11:08 -04:00
|
|
|
const descriptionSourcePath = "man/src/"
|
|
|
|
|
|
|
|
func generateManPages(opts *options) error {
|
2016-06-09 11:33:28 -04:00
|
|
|
header := &doc.GenManHeader{
|
|
|
|
Title: "DOCKER",
|
|
|
|
Section: "1",
|
|
|
|
Source: "Docker Community",
|
|
|
|
}
|
2016-06-23 11:25:51 -04:00
|
|
|
|
|
|
|
stdin, stdout, stderr := term.StdStreams()
|
2018-09-11 08:46:30 -04:00
|
|
|
dockerCli := command.NewDockerCli(stdin, stdout, stderr, false, nil)
|
2016-06-23 11:25:51 -04:00
|
|
|
cmd := &cobra.Command{Use: "docker"}
|
2016-09-08 13:11:39 -04:00
|
|
|
commands.AddCommands(cmd, dockerCli)
|
2016-09-22 14:11:08 -04:00
|
|
|
source := filepath.Join(opts.source, descriptionSourcePath)
|
|
|
|
if err := loadLongDescription(cmd, source); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-23 11:25:51 -04:00
|
|
|
|
2016-06-09 11:33:28 -04:00
|
|
|
cmd.DisableAutoGenTag = true
|
2017-11-20 12:48:10 -05:00
|
|
|
cmd.DisableFlagsInUseLine = true
|
2016-06-09 11:33:28 -04:00
|
|
|
return doc.GenManTreeFromOpts(cmd, doc.GenManTreeOptions{
|
|
|
|
Header: header,
|
2016-09-22 14:11:08 -04:00
|
|
|
Path: opts.target,
|
2016-06-09 11:33:28 -04:00
|
|
|
CommandSeparator: "-",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-22 14:11:08 -04:00
|
|
|
func loadLongDescription(cmd *cobra.Command, path string) error {
|
|
|
|
for _, cmd := range cmd.Commands() {
|
2017-11-20 12:48:10 -05:00
|
|
|
cmd.DisableFlagsInUseLine = true
|
2016-09-22 14:11:08 -04:00
|
|
|
if cmd.Name() == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fullpath := filepath.Join(path, cmd.Name()+".md")
|
|
|
|
|
|
|
|
if cmd.HasSubCommands() {
|
|
|
|
loadLongDescription(cmd, filepath.Join(path, cmd.Name()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(fullpath); err != nil {
|
|
|
|
log.Printf("WARN: %s does not exist, skipping\n", fullpath)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(fullpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.Long = string(content)
|
2017-01-13 12:05:29 -05:00
|
|
|
|
|
|
|
fullpath = filepath.Join(path, cmd.Name()+"-example.md")
|
|
|
|
if _, err := os.Stat(fullpath); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err = ioutil.ReadFile(fullpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.Example = string(content)
|
|
|
|
|
2016-09-22 14:11:08 -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 man pages")
|
|
|
|
err := flags.Parse(os.Args[1:])
|
|
|
|
return opts, err
|
|
|
|
}
|
|
|
|
|
2016-06-09 11:33:28 -04:00
|
|
|
func main() {
|
2016-09-22 14:11:08 -04:00
|
|
|
opts, err := parseArgs()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
2016-06-09 11:33:28 -04:00
|
|
|
}
|
2016-09-22 14:11:08 -04:00
|
|
|
fmt.Printf("Project root: %s\n", opts.source)
|
|
|
|
fmt.Printf("Generating man pages into %s\n", opts.target)
|
|
|
|
if err := generateManPages(opts); err != nil {
|
2016-06-09 11:33:28 -04:00
|
|
|
fmt.Fprintf(os.Stderr, "Failed to generate man pages: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
}
|