2022-02-25 11:05:35 -05:00
|
|
|
// This file is intended for use with "go run"; it isn't really part of the package.
|
|
|
|
|
|
|
|
// +build manpages
|
|
|
|
|
2016-06-09 11:33:28 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-22 14:11:08 -04:00
|
|
|
"log"
|
2016-06-09 11:33:28 -04:00
|
|
|
"os"
|
2016-09-22 14:11:08 -04:00
|
|
|
"path/filepath"
|
2018-08-23 05:53:55 -04:00
|
|
|
"strconv"
|
|
|
|
"time"
|
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/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 {
|
2022-02-25 11:05:35 -05:00
|
|
|
log.SetFlags(0)
|
|
|
|
|
2016-06-09 11:33:28 -04:00
|
|
|
header := &doc.GenManHeader{
|
|
|
|
Title: "DOCKER",
|
|
|
|
Section: "1",
|
|
|
|
Source: "Docker Community",
|
2020-10-20 06:48:52 -04:00
|
|
|
Manual: "Docker User Manuals",
|
2016-06-09 11:33:28 -04:00
|
|
|
}
|
2016-06-23 11:25:51 -04:00
|
|
|
|
2018-08-23 05:53:55 -04:00
|
|
|
// If SOURCE_DATE_EPOCH is set, in order to allow reproducible package
|
|
|
|
// builds, we explicitly set the build time to SOURCE_DATE_EPOCH.
|
|
|
|
if epoch := os.Getenv("SOURCE_DATE_EPOCH"); epoch != "" {
|
|
|
|
unixEpoch, err := strconv.ParseInt(epoch, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid SOURCE_DATE_EPOCH: %v", err)
|
|
|
|
}
|
|
|
|
now := time.Unix(unixEpoch, 0)
|
|
|
|
header.Date = &now
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:52:58 -05:00
|
|
|
dockerCli, err := command.NewDockerCli()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-25 11:05:35 -05:00
|
|
|
log.Printf("INFO: %s found\n", fullpath)
|
2022-02-25 09:33:19 -05:00
|
|
|
content, err := os.ReadFile(fullpath)
|
2016-09-22 14:11:08 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:33:19 -05:00
|
|
|
content, err = os.ReadFile(fullpath)
|
2017-01-13 12:05:29 -05:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|