Skip to content

Commit 14f2972

Browse files
authored
Merge pull request #6289 from thaJeztah/28.x_backport_hosts_regular_stringarray
[28.x backport] cli/flags: add "hostVar" to handle --host / -H as a single string
2 parents bcc479b + 7091e8b commit 14f2972

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

cli/flags/options.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package flags
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"path/filepath"
78

89
"github.com/docker/cli/cli/config"
9-
"github.com/docker/cli/opts"
1010
"github.com/docker/docker/client"
1111
"github.com/docker/go-connections/tlsconfig"
1212
"github.com/sirupsen/logrus"
@@ -54,6 +54,39 @@ var (
5454
dockerTLS = os.Getenv(EnvEnableTLS) != ""
5555
)
5656

57+
// hostVar is used for the '--host' / '-H' flag to set [ClientOptions.Hosts].
58+
// The [ClientOptions.Hosts] field is a slice because it was originally shared
59+
// with the daemon config. However, the CLI only allows for a single host to
60+
// be specified.
61+
//
62+
// hostVar presents itself as a "string", but stores the value in a string
63+
// slice. It produces an error when trying to set multiple values, matching
64+
// the check in [getServerHost].
65+
//
66+
// [getServerHost]: https://github.com/docker/cli/blob/7eab668982645def1cd46fe1b60894cba6fd17a4/cli/command/cli.go#L542-L551
67+
type hostVar struct {
68+
dst *[]string
69+
set bool
70+
}
71+
72+
func (h *hostVar) String() string {
73+
if h.dst == nil || len(*h.dst) == 0 {
74+
return ""
75+
}
76+
return (*h.dst)[0]
77+
}
78+
79+
func (h *hostVar) Set(s string) error {
80+
if h.set {
81+
return errors.New("specify only one -H")
82+
}
83+
*h.dst = []string{s}
84+
h.set = true
85+
return nil
86+
}
87+
88+
func (*hostVar) Type() string { return "string" }
89+
5790
// ClientOptions are the options used to configure the client cli.
5891
type ClientOptions struct {
5992
Debug bool
@@ -94,9 +127,9 @@ func (o *ClientOptions) InstallFlags(flags *pflag.FlagSet) {
94127
flags.Var(&quotedString{&tlsOptions.CertFile}, "tlscert", "Path to TLS certificate file")
95128
flags.Var(&quotedString{&tlsOptions.KeyFile}, "tlskey", "Path to TLS key file")
96129

97-
// opts.ValidateHost is not used here, so as to allow connection helpers
98-
hostOpt := opts.NewNamedListOptsRef("hosts", &o.Hosts, nil)
99-
flags.VarP(hostOpt, "host", "H", "Daemon socket to connect to")
130+
// TODO(thaJeztah): show the default host.
131+
// TODO(thaJeztah): this should be a string, not an "array" as we only allow a single host.
132+
flags.VarP(&hostVar{dst: &o.Hosts}, "host", "H", "Daemon socket to connect to")
100133
flags.StringVarP(&o.Context, "context", "c", "",
101134
`Name of the context to use to connect to the daemon (overrides `+client.EnvOverrideHost+` env var and default context set with "docker context use")`)
102135
}

docs/reference/commandline/docker.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The base command for the Docker CLI.
7474
| `--config` | `string` | `/root/.docker` | Location of client config files |
7575
| `-c`, `--context` | `string` | | Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with `docker context use`) |
7676
| `-D`, `--debug` | `bool` | | Enable debug mode |
77-
| [`-H`](#host), [`--host`](#host) | `list` | | Daemon socket to connect to |
77+
| [`-H`](#host), [`--host`](#host) | `string` | | Daemon socket to connect to |
7878
| `-l`, `--log-level` | `string` | `info` | Set the logging level (`debug`, `info`, `warn`, `error`, `fatal`) |
7979
| `--tls` | `bool` | | Use TLS; implied by --tlsverify |
8080
| `--tlscacert` | `string` | `/root/.docker/ca.pem` | Trust certs signed only by this CA |

e2e/cli-plugins/flags_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cliplugins
22

33
import (
4+
"fmt"
45
"os"
56
"testing"
67

@@ -91,9 +92,9 @@ func TestGlobalArgsOnlyParsedOnce(t *testing.T) {
9192
// This is checking the precondition wrt -H mentioned in the function comment
9293
name: "fails-if-H-used-twice",
9394
args: []string{"-H", dh, "-H", dh, "version", "-f", "{{.Client.Version}}"},
94-
expectedExitCode: 1,
95+
expectedExitCode: 125,
9596
expectedOut: icmd.None,
96-
expectedErr: "Specify only one -H",
97+
expectedErr: fmt.Sprintf(`invalid argument %q for "-H, --host" flag: specify only one -H`, dh),
9798
},
9899
{
99100
name: "builtin",

0 commit comments

Comments
 (0)