mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #31830 from dnephin/refactor-cli-state
Small cleanup now that we have multiple details about the server stored on the cli
This commit is contained in:
commit
bce494f7a2
2 changed files with 48 additions and 30 deletions
|
@ -50,22 +50,11 @@ type DockerCli struct {
|
||||||
err io.Writer
|
err io.Writer
|
||||||
keyFile string
|
keyFile string
|
||||||
client client.APIClient
|
client client.APIClient
|
||||||
hasExperimental bool
|
|
||||||
osType string
|
|
||||||
defaultVersion string
|
defaultVersion string
|
||||||
|
server ServerInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasExperimental returns true if experimental features are accessible.
|
// DefaultVersion returns api.defaultVersion or DOCKER_API_VERSION if specified.
|
||||||
func (cli *DockerCli) HasExperimental() bool {
|
|
||||||
return cli.hasExperimental
|
|
||||||
}
|
|
||||||
|
|
||||||
// OSType returns the operating system the daemon is running on.
|
|
||||||
func (cli *DockerCli) OSType() string {
|
|
||||||
return cli.osType
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultVersion returns api.defaultVersion of DOCKER_API_VERSION if specified.
|
|
||||||
func (cli *DockerCli) DefaultVersion() string {
|
func (cli *DockerCli) DefaultVersion() string {
|
||||||
return cli.defaultVersion
|
return cli.defaultVersion
|
||||||
}
|
}
|
||||||
|
@ -102,6 +91,12 @@ func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
|
||||||
return cli.configFile
|
return cli.configFile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerInfo returns the server version details for the host this client is
|
||||||
|
// connected to
|
||||||
|
func (cli *DockerCli) ServerInfo() ServerInfo {
|
||||||
|
return cli.server
|
||||||
|
}
|
||||||
|
|
||||||
// GetAllCredentials returns all of the credentials stored in all of the
|
// GetAllCredentials returns all of the credentials stored in all of the
|
||||||
// configured credential stores.
|
// configured credential stores.
|
||||||
func (cli *DockerCli) GetAllCredentials() (map[string]types.AuthConfig, error) {
|
func (cli *DockerCli) GetAllCredentials() (map[string]types.AuthConfig, error) {
|
||||||
|
@ -161,7 +156,6 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cli.defaultVersion = cli.client.ClientVersion()
|
cli.defaultVersion = cli.client.ClientVersion()
|
||||||
|
|
||||||
if opts.Common.TrustKey == "" {
|
if opts.Common.TrustKey == "" {
|
||||||
|
@ -171,8 +165,10 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ping, err := cli.client.Ping(context.Background()); err == nil {
|
if ping, err := cli.client.Ping(context.Background()); err == nil {
|
||||||
cli.hasExperimental = ping.Experimental
|
cli.server = ServerInfo{
|
||||||
cli.osType = ping.OSType
|
HasExperimental: ping.Experimental,
|
||||||
|
OSType: ping.OSType,
|
||||||
|
}
|
||||||
|
|
||||||
// since the new header was added in 1.25, assume server is 1.24 if header is not present.
|
// since the new header was added in 1.25, assume server is 1.24 if header is not present.
|
||||||
if ping.APIVersion == "" {
|
if ping.APIVersion == "" {
|
||||||
|
@ -184,9 +180,17 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
|
||||||
cli.client.UpdateClientVersion(ping.APIVersion)
|
cli.client.UpdateClientVersion(ping.APIVersion)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerInfo stores details about the supported features and platform of the
|
||||||
|
// server
|
||||||
|
type ServerInfo struct {
|
||||||
|
HasExperimental bool
|
||||||
|
OSType string
|
||||||
|
}
|
||||||
|
|
||||||
// NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
|
// NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
|
||||||
func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
|
func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
|
||||||
return &DockerCli{in: NewInStream(in), out: NewOutStream(out), err: err}
|
return &DockerCli{in: NewInStream(in), out: NewOutStream(out), err: err}
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
cliconfig "github.com/docker/docker/cli/config"
|
cliconfig "github.com/docker/docker/cli/config"
|
||||||
"github.com/docker/docker/cli/debug"
|
"github.com/docker/docker/cli/debug"
|
||||||
cliflags "github.com/docker/docker/cli/flags"
|
cliflags "github.com/docker/docker/cli/flags"
|
||||||
|
"github.com/docker/docker/client"
|
||||||
"github.com/docker/docker/dockerversion"
|
"github.com/docker/docker/dockerversion"
|
||||||
"github.com/docker/docker/pkg/term"
|
"github.com/docker/docker/pkg/term"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -49,7 +50,7 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {
|
||||||
if err := dockerCli.Initialize(opts); err != nil {
|
if err := dockerCli.Initialize(opts); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return isSupported(cmd, dockerCli.Client().ClientVersion(), dockerCli.OSType(), dockerCli.HasExperimental())
|
return isSupported(cmd, dockerCli)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cli.SetupRootCommand(cmd)
|
cli.SetupRootCommand(cmd)
|
||||||
|
@ -80,7 +81,7 @@ func setFlagErrorFunc(dockerCli *command.DockerCli, cmd *cobra.Command, flags *p
|
||||||
flagErrorFunc := cmd.FlagErrorFunc()
|
flagErrorFunc := cmd.FlagErrorFunc()
|
||||||
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
|
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
|
||||||
initializeDockerCli(dockerCli, flags, opts)
|
initializeDockerCli(dockerCli, flags, opts)
|
||||||
if err := isSupported(cmd, dockerCli.Client().ClientVersion(), dockerCli.OSType(), dockerCli.HasExperimental()); err != nil {
|
if err := isSupported(cmd, dockerCli); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return flagErrorFunc(cmd, err)
|
return flagErrorFunc(cmd, err)
|
||||||
|
@ -90,12 +91,12 @@ func setFlagErrorFunc(dockerCli *command.DockerCli, cmd *cobra.Command, flags *p
|
||||||
func setHelpFunc(dockerCli *command.DockerCli, cmd *cobra.Command, flags *pflag.FlagSet, opts *cliflags.ClientOptions) {
|
func setHelpFunc(dockerCli *command.DockerCli, cmd *cobra.Command, flags *pflag.FlagSet, opts *cliflags.ClientOptions) {
|
||||||
cmd.SetHelpFunc(func(ccmd *cobra.Command, args []string) {
|
cmd.SetHelpFunc(func(ccmd *cobra.Command, args []string) {
|
||||||
initializeDockerCli(dockerCli, flags, opts)
|
initializeDockerCli(dockerCli, flags, opts)
|
||||||
if err := isSupported(ccmd, dockerCli.Client().ClientVersion(), dockerCli.OSType(), dockerCli.HasExperimental()); err != nil {
|
if err := isSupported(ccmd, dockerCli); err != nil {
|
||||||
ccmd.Println(err)
|
ccmd.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
hideUnsupportedFeatures(ccmd, dockerCli.Client().ClientVersion(), dockerCli.OSType(), dockerCli.HasExperimental())
|
hideUnsupportedFeatures(ccmd, dockerCli)
|
||||||
|
|
||||||
if err := ccmd.Help(); err != nil {
|
if err := ccmd.Help(); err != nil {
|
||||||
ccmd.Println(err)
|
ccmd.Println(err)
|
||||||
|
@ -122,7 +123,7 @@ func setValidateArgs(dockerCli *command.DockerCli, cmd *cobra.Command, flags *pf
|
||||||
cmdArgs := ccmd.Args
|
cmdArgs := ccmd.Args
|
||||||
ccmd.Args = func(cmd *cobra.Command, args []string) error {
|
ccmd.Args = func(cmd *cobra.Command, args []string) error {
|
||||||
initializeDockerCli(dockerCli, flags, opts)
|
initializeDockerCli(dockerCli, flags, opts)
|
||||||
if err := isSupported(cmd, dockerCli.Client().ClientVersion(), dockerCli.OSType(), dockerCli.HasExperimental()); err != nil {
|
if err := isSupported(cmd, dockerCli); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return cmdArgs(cmd, args)
|
return cmdArgs(cmd, args)
|
||||||
|
@ -198,7 +199,16 @@ func dockerPreRun(opts *cliflags.ClientOptions) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func hideUnsupportedFeatures(cmd *cobra.Command, clientVersion, osType string, hasExperimental bool) {
|
type versionDetails interface {
|
||||||
|
Client() client.APIClient
|
||||||
|
ServerInfo() command.ServerInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) {
|
||||||
|
clientVersion := details.Client().ClientVersion()
|
||||||
|
osType := details.ServerInfo().OSType
|
||||||
|
hasExperimental := details.ServerInfo().HasExperimental
|
||||||
|
|
||||||
cmd.Flags().VisitAll(func(f *pflag.Flag) {
|
cmd.Flags().VisitAll(func(f *pflag.Flag) {
|
||||||
// hide experimental flags
|
// hide experimental flags
|
||||||
if !hasExperimental {
|
if !hasExperimental {
|
||||||
|
@ -228,7 +238,11 @@ func hideUnsupportedFeatures(cmd *cobra.Command, clientVersion, osType string, h
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isSupported(cmd *cobra.Command, clientVersion, osType string, hasExperimental bool) error {
|
func isSupported(cmd *cobra.Command, details versionDetails) error {
|
||||||
|
clientVersion := details.Client().ClientVersion()
|
||||||
|
osType := details.ServerInfo().OSType
|
||||||
|
hasExperimental := details.ServerInfo().HasExperimental
|
||||||
|
|
||||||
// Check recursively so that, e.g., `docker stack ls` returns the same output as `docker stack`
|
// Check recursively so that, e.g., `docker stack ls` returns the same output as `docker stack`
|
||||||
for curr := cmd; curr != nil; curr = curr.Parent() {
|
for curr := cmd; curr != nil; curr = curr.Parent() {
|
||||||
if cmdVersion, ok := curr.Tags["version"]; ok && versions.LessThan(clientVersion, cmdVersion) {
|
if cmdVersion, ok := curr.Tags["version"]; ok && versions.LessThan(clientVersion, cmdVersion) {
|
||||||
|
|
Loading…
Reference in a new issue