github.com/alecthomas/kong@v0.9.1-0.20240410131203-2ab5733f1179/_examples/docker/main.go (about)

     1  // nolint
     2  package main
     3  
     4  import (
     5  	"fmt"
     6  
     7  	"github.com/alecthomas/kong"
     8  )
     9  
    10  type Globals struct {
    11  	Config    string      `help:"Location of client config files" default:"~/.docker" type:"path"`
    12  	Debug     bool        `short:"D" help:"Enable debug mode"`
    13  	Host      []string    `short:"H" help:"Daemon socket(s) to connect to"`
    14  	LogLevel  string      `short:"l" help:"Set the logging level (debug|info|warn|error|fatal)" default:"info"`
    15  	TLS       bool        `help:"Use TLS; implied by --tls-verify"`
    16  	TLSCACert string      `name:"tls-ca-cert" help:"Trust certs signed only by this CA" default:"~/.docker/ca.pem" type:"path"`
    17  	TLSCert   string      `help:"Path to TLS certificate file" default:"~/.docker/cert.pem" type:"path"`
    18  	TLSKey    string      `help:"Path to TLS key file" default:"~/.docker/key.pem" type:"path"`
    19  	TLSVerify bool        `help:"Use TLS and verify the remote"`
    20  	Version   VersionFlag `name:"version" help:"Print version information and quit"`
    21  }
    22  
    23  type VersionFlag string
    24  
    25  func (v VersionFlag) Decode(ctx *kong.DecodeContext) error { return nil }
    26  func (v VersionFlag) IsBool() bool                         { return true }
    27  func (v VersionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error {
    28  	fmt.Println(vars["version"])
    29  	app.Exit(0)
    30  	return nil
    31  }
    32  
    33  type CLI struct {
    34  	Globals
    35  
    36  	Attach  AttachCmd  `cmd:"" help:"Attach local standard input, output, and error streams to a running container"`
    37  	Build   BuildCmd   `cmd:"" help:"Build an image from a Dockerfile"`
    38  	Commit  CommitCmd  `cmd:"" help:"Create a new image from a container's changes"`
    39  	Cp      CpCmd      `cmd:"" help:"Copy files/folders between a container and the local filesystem"`
    40  	Create  CreateCmd  `cmd:"" help:"Create a new container"`
    41  	Deploy  DeployCmd  `cmd:"" help:"Deploy a new stack or update an existing stack"`
    42  	Diff    DiffCmd    `cmd:"" help:"Inspect changes to files or directories on a container's filesystem"`
    43  	Events  EventsCmd  `cmd:"" help:"Get real time events from the server"`
    44  	Exec    ExecCmd    `cmd:"" help:"Run a command in a running container"`
    45  	Export  ExportCmd  `cmd:"" help:"Export a container's filesystem as a tar archive"`
    46  	History HistoryCmd `cmd:"" help:"Show the history of an image"`
    47  	Images  ImagesCmd  `cmd:"" help:"List images"`
    48  	Import  ImportCmd  `cmd:"" help:"Import the contents from a tarball to create a filesystem image"`
    49  	Info    InfoCmd    `cmd:"" help:"Display system-wide information"`
    50  	Inspect InspectCmd `cmd:"" help:"Return low-level information on Docker objects"`
    51  	Kill    KillCmd    `cmd:"" help:"Kill one or more running containers"`
    52  	Load    LoadCmd    `cmd:"" help:"Load an image from a tar archive or STDIN"`
    53  	Login   LoginCmd   `cmd:"" help:"Log in to a Docker registry"`
    54  	Logout  LogoutCmd  `cmd:"" help:"Log out from a Docker registry"`
    55  	Logs    LogsCmd    `cmd:"" help:"Fetch the logs of a container"`
    56  	Pause   PauseCmd   `cmd:"" help:"Pause all processes within one or more containers"`
    57  	Port    PortCmd    `cmd:"" help:"List port mappings or a specific mapping for the container"`
    58  	Ps      PsCmd      `cmd:"" help:"List containers"`
    59  	Pull    PullCmd    `cmd:"" help:"Pull an image or a repository from a registry"`
    60  	Push    PushCmd    `cmd:"" help:"Push an image or a repository to a registry"`
    61  	Rename  RenameCmd  `cmd:"" help:"Rename a container"`
    62  	Restart RestartCmd `cmd:"" help:"Restart one or more containers"`
    63  	Rm      RmCmd      `cmd:"" help:"Remove one or more containers"`
    64  	Rmi     RmiCmd     `cmd:"" help:"Remove one or more images"`
    65  	Run     RunCmd     `cmd:"" help:"Run a command in a new container"`
    66  	Save    SaveCmd    `cmd:"" help:"Save one or more images to a tar archive (streamed to STDOUT by default)"`
    67  	Search  SearchCmd  `cmd:"" help:"Search the Docker Hub for images"`
    68  	Start   StartCmd   `cmd:"" help:"Start one or more stopped containers"`
    69  	Stats   StatsCmd   `cmd:"" help:"Display a live stream of container(s) resource usage statistics"`
    70  	Stop    StopCmd    `cmd:"" help:"Stop one or more running containers"`
    71  	Tag     TagCmd     `cmd:"" help:"Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE"`
    72  	Top     TopCmd     `cmd:"" help:"Display the running processes of a container"`
    73  	Unpause UnpauseCmd `cmd:"" help:"Unpause all processes within one or more containers"`
    74  	Update  UpdateCmd  `cmd:"" help:"Update configuration of one or more containers"`
    75  	Version VersionCmd `cmd:"" help:"Show the Docker version information"`
    76  	Wait    WaitCmd    `cmd:"" help:"Block until one or more containers stop, then print their exit codes"`
    77  }
    78  
    79  func main() {
    80  	cli := CLI{
    81  		Globals: Globals{
    82  			Version: VersionFlag("0.1.1"),
    83  		},
    84  	}
    85  
    86  	ctx := kong.Parse(&cli,
    87  		kong.Name("docker"),
    88  		kong.Description("A self-sufficient runtime for containers"),
    89  		kong.UsageOnError(),
    90  		kong.ConfigureHelp(kong.HelpOptions{
    91  			Compact: true,
    92  		}),
    93  		kong.Vars{
    94  			"version": "0.0.1",
    95  		})
    96  	err := ctx.Run(&cli.Globals)
    97  	ctx.FatalIfErrorf(err)
    98  }