2022-02-25 17:05:35 +01:00
// This file is intended for use with "go run"; it isn't really part of the package.
2022-03-26 18:34:10 +01:00
//go:build manpages
2022-02-25 17:05:35 +01:00
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"
2016-06-09 11:33:28 -04:00
2025-03-19 09:31:38 +01:00
clidocstool "github.com/docker/cli-docs-tool"
"github.com/docker/cli/cli"
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
)
2025-03-19 09:31:38 +01:00
const defaultSourcePath = "man/src/"
2016-09-22 14:11:08 -04:00
2025-03-19 09:31:38 +01:00
type options struct {
source string
target string
}
func gen ( opts * options ) error {
2022-02-25 17:05:35 +01:00
log . SetFlags ( 0 )
2025-03-19 09:31:38 +01:00
dockerCLI , err := command . NewDockerCli ( )
if err != nil {
return err
2016-06-09 11:33:28 -04:00
}
2025-03-19 09:31:38 +01:00
cmd := & cobra . Command {
Use : "docker [OPTIONS] COMMAND [ARG...]" ,
Short : "The base command for the Docker CLI." ,
2018-08-23 19:53:55 +10:00
}
2025-03-19 09:31:38 +01:00
clientOpts , _ := cli . SetupRootCommand ( cmd )
if err := dockerCLI . Initialize ( clientOpts ) ; err != nil {
2019-01-28 14:52:58 +01:00
return err
}
2025-03-19 09:31:38 +01:00
commands . AddCommands ( cmd , dockerCLI )
// TODO(thaJeztah): cli-docs-tool should already be able to do this, but assumes source-files are not in subdirectories (it looks for `src/docker_command_subcommand.md`)
if err := loadLongDescription ( cmd , opts . source ) ; err != nil {
2016-09-22 14:11:08 -04:00
return err
}
2016-06-23 11:25:51 -04:00
2025-03-19 09:31:38 +01:00
c , err := clidocstool . New ( clidocstool . Options {
Root : cmd ,
SourceDir : opts . source ,
TargetDir : opts . target ,
ManHeader : & doc . GenManHeader {
Title : "DOCKER" ,
Section : "1" ,
Source : "Docker Community" ,
Manual : "Docker User Manuals" ,
} ,
Plugin : false ,
2016-06-09 11:33:28 -04:00
} )
2025-03-19 09:31:38 +01:00
if err != nil {
return err
}
fmt . Println ( "Manpage source folder:" , opts . source )
fmt . Println ( "Generating man pages into" , opts . target )
return c . GenManTree ( cmd )
2016-06-09 11:33:28 -04:00
}
2025-03-19 10:46:35 +01:00
func loadLongDescription ( parentCommand * cobra . Command , path string ) error {
for _ , cmd := range parentCommand . 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 ( ) {
2025-03-19 10:45:52 +01:00
if err := loadLongDescription ( cmd , filepath . Join ( path , cmd . Name ( ) ) ) ; err != nil {
return err
}
2016-09-22 14:11:08 -04:00
}
if _ , err := os . Stat ( fullpath ) ; err != nil {
log . Printf ( "WARN: %s does not exist, skipping\n" , fullpath )
continue
}
2022-02-25 17:05:35 +01:00
log . Printf ( "INFO: %s found\n" , fullpath )
2022-02-25 15:33:19 +01: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 09:05:29 -08:00
fullpath = filepath . Join ( path , cmd . Name ( ) + "-example.md" )
if _ , err := os . Stat ( fullpath ) ; err != nil {
continue
}
2022-02-25 15:33:19 +01:00
content , err = os . ReadFile ( fullpath )
2017-01-13 09:05:29 -08:00
if err != nil {
return err
}
cmd . Example = string ( content )
2016-09-22 14:11:08 -04:00
}
return nil
}
2025-03-19 09:31:38 +01:00
func run ( ) error {
2016-09-22 14:11:08 -04:00
opts := & options { }
flags := pflag . NewFlagSet ( os . Args [ 0 ] , pflag . ContinueOnError )
2025-03-19 09:31:38 +01:00
flags . StringVar ( & opts . source , "source" , defaultSourcePath , "Manpage source folder" )
2016-09-22 14:11:08 -04:00
flags . StringVar ( & opts . target , "target" , "/tmp" , "Target path for generated man pages" )
2025-03-19 09:31:38 +01:00
if err := flags . Parse ( os . Args [ 1 : ] ) ; err != nil {
return err
}
return gen ( opts )
2016-09-22 14:11:08 -04:00
}
2016-06-09 11:33:28 -04:00
func main ( ) {
2025-03-19 09:31:38 +01:00
if err := run ( ) ; err != nil {
log . Printf ( "ERROR: %+v" , err )
os . Exit ( 1 )
2016-06-09 11:33:28 -04:00
}
}