|
1 | 1 | package flags |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
5 | 6 | "os" |
6 | 7 | "path/filepath" |
7 | 8 |
|
8 | 9 | "github.com/docker/cli/cli/config" |
9 | | - "github.com/docker/cli/opts" |
10 | 10 | "github.com/docker/docker/client" |
11 | 11 | "github.com/docker/go-connections/tlsconfig" |
12 | 12 | "github.com/sirupsen/logrus" |
|
54 | 54 | dockerTLS = os.Getenv(EnvEnableTLS) != "" |
55 | 55 | ) |
56 | 56 |
|
| 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 | + |
57 | 90 | // ClientOptions are the options used to configure the client cli. |
58 | 91 | type ClientOptions struct { |
59 | 92 | Debug bool |
@@ -94,9 +127,9 @@ func (o *ClientOptions) InstallFlags(flags *pflag.FlagSet) { |
94 | 127 | flags.Var("edString{&tlsOptions.CertFile}, "tlscert", "Path to TLS certificate file") |
95 | 128 | flags.Var("edString{&tlsOptions.KeyFile}, "tlskey", "Path to TLS key file") |
96 | 129 |
|
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") |
100 | 133 | flags.StringVarP(&o.Context, "context", "c", "", |
101 | 134 | `Name of the context to use to connect to the daemon (overrides `+client.EnvOverrideHost+` env var and default context set with "docker context use")`) |
102 | 135 | } |
|
0 commit comments