Skip to content

Commit ad21055

Browse files
committed
opts: move swarm-specific options to a separate package
This prevents users of the CLI that don't implement swarm-related features from depending on the swarm API types. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 4c882e0 commit ad21055

File tree

11 files changed

+41
-21
lines changed

11 files changed

+41
-21
lines changed

‎cli/command/service/create_test.go‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/docker/cli/opts/swarmopts"
78
"github.com/docker/docker/api/types"
89
"github.com/docker/docker/api/types/swarm"
910
"gotest.tools/v3/assert"
1011
is "gotest.tools/v3/assert/cmp"
11-
12-
cliopts "github.com/docker/cli/opts"
1312
)
1413

1514
// fakeConfigAPIClientList is used to let us pass a closure as a
@@ -43,8 +42,8 @@ func (fakeConfigAPIClientList) ConfigUpdate(_ context.Context, _ string, _ swarm
4342
func TestSetConfigsWithCredSpecAndConfigs(t *testing.T) {
4443
// we can't directly access the internal fields of the ConfigOpt struct, so
4544
// we need to let it do the parsing
46-
configOpt := &cliopts.ConfigOpt{}
47-
configOpt.Set("bar")
45+
configOpt := &swarmopts.ConfigOpt{}
46+
assert.Check(t, configOpt.Set("bar"))
4847
opts := &serviceOptions{
4948
credentialSpec: credentialSpecOpt{
5049
value: &swarm.CredentialSpec{
@@ -187,8 +186,8 @@ func TestSetConfigsOnlyCredSpec(t *testing.T) {
187186
// TestSetConfigsOnlyConfigs verifies setConfigs when only configs (and not a
188187
// CredentialSpec) is needed.
189188
func TestSetConfigsOnlyConfigs(t *testing.T) {
190-
configOpt := &cliopts.ConfigOpt{}
191-
configOpt.Set("bar")
189+
configOpt := &swarmopts.ConfigOpt{}
190+
assert.Check(t, configOpt.Set("bar"))
192191
opts := &serviceOptions{
193192
configs: *configOpt,
194193
}

‎cli/command/service/opts.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/docker/cli/cli/command"
1515
"github.com/docker/cli/opts"
16+
"github.com/docker/cli/opts/swarmopts"
1617
"github.com/docker/docker/api/types/container"
1718
"github.com/docker/docker/api/types/network"
1819
"github.com/docker/docker/api/types/swarm"
@@ -395,7 +396,7 @@ func convertNetworks(networks opts.NetworkOpt) []swarm.NetworkAttachmentConfig {
395396

396397
type endpointOptions struct {
397398
mode string
398-
publishPorts opts.PortOpt
399+
publishPorts swarmopts.PortOpt
399400
}
400401

401402
func (e *endpointOptions) ToEndpointSpec() *swarm.EndpointSpec {
@@ -553,8 +554,8 @@ type serviceOptions struct {
553554
logDriver logDriverOptions
554555

555556
healthcheck healthCheckOptions
556-
secrets opts.SecretOpt
557-
configs opts.ConfigOpt
557+
secrets swarmopts.SecretOpt
558+
configs swarmopts.ConfigOpt
558559

559560
isolation string
560561
}

‎cli/command/service/update.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/docker/cli/cli/command"
1212
"github.com/docker/cli/cli/command/completion"
1313
"github.com/docker/cli/opts"
14+
"github.com/docker/cli/opts/swarmopts"
1415
"github.com/docker/docker/api/types"
1516
"github.com/docker/docker/api/types/container"
1617
mounttypes "github.com/docker/docker/api/types/mount"
@@ -55,7 +56,7 @@ func newUpdateCommand(dockerCLI command.Cli) *cobra.Command {
5556
flags.Var(newListOptsVar(), flagContainerLabelRemove, "Remove a container label by its key")
5657
flags.Var(newListOptsVar(), flagMountRemove, "Remove a mount by its target path")
5758
// flags.Var(newListOptsVar().WithValidator(validatePublishRemove), flagPublishRemove, "Remove a published port by its target port")
58-
flags.Var(&opts.PortOpt{}, flagPublishRemove, "Remove a published port by its target port")
59+
flags.Var(&swarmopts.PortOpt{}, flagPublishRemove, "Remove a published port by its target port")
5960
flags.Var(newListOptsVar(), flagConstraintRemove, "Remove a constraint")
6061
flags.Var(newListOptsVar(), flagDNSRemove, "Remove a custom DNS server")
6162
flags.SetAnnotation(flagDNSRemove, "version", []string{"1.25"})
@@ -804,7 +805,7 @@ func getUpdatedSecrets(ctx context.Context, apiClient client.SecretAPIClient, fl
804805
}
805806

806807
if flags.Changed(flagSecretAdd) {
807-
values := flags.Lookup(flagSecretAdd).Value.(*opts.SecretOpt).Value()
808+
values := flags.Lookup(flagSecretAdd).Value.(*swarmopts.SecretOpt).Value()
808809

809810
addSecrets, err := ParseSecrets(ctx, apiClient, values)
810811
if err != nil {
@@ -852,7 +853,7 @@ func getUpdatedConfigs(ctx context.Context, apiClient client.ConfigAPIClient, fl
852853
resolveConfigs := []*swarm.ConfigReference{}
853854

854855
if flags.Changed(flagConfigAdd) {
855-
resolveConfigs = append(resolveConfigs, flags.Lookup(flagConfigAdd).Value.(*opts.ConfigOpt).Value()...)
856+
resolveConfigs = append(resolveConfigs, flags.Lookup(flagConfigAdd).Value.(*swarmopts.ConfigOpt).Value()...)
856857
}
857858

858859
// if credSpecConfigNameis non-empty at this point, it means its a new
@@ -1091,7 +1092,7 @@ func updatePorts(flags *pflag.FlagSet, portConfig *[]swarm.PortConfig) error {
10911092
newPorts := []swarm.PortConfig{}
10921093

10931094
// Clean current ports
1094-
toRemove := flags.Lookup(flagPublishRemove).Value.(*opts.PortOpt).Value()
1095+
toRemove := flags.Lookup(flagPublishRemove).Value.(*swarmopts.PortOpt).Value()
10951096
portLoop:
10961097
for _, port := range portSet {
10971098
for _, pConfig := range toRemove {
@@ -1107,7 +1108,7 @@ portLoop:
11071108

11081109
// Check to see if there are any conflict in flags.
11091110
if flags.Changed(flagPublishAdd) {
1110-
ports := flags.Lookup(flagPublishAdd).Value.(*opts.PortOpt).Value()
1111+
ports := flags.Lookup(flagPublishAdd).Value.(*swarmopts.PortOpt).Value()
11111112

11121113
for _, port := range ports {
11131114
if _, ok := portSet[portConfigToString(&port)]; ok {

‎cli/compose/loader/loader.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/docker/cli/cli/compose/template"
1919
"github.com/docker/cli/cli/compose/types"
2020
"github.com/docker/cli/opts"
21+
"github.com/docker/cli/opts/swarmopts"
2122
"github.com/docker/docker/api/types/versions"
2223
"github.com/docker/go-connections/nat"
2324
units "github.com/docker/go-units"
@@ -925,7 +926,7 @@ func toServicePortConfigs(value string) ([]any, error) {
925926

926927
for _, key := range keys {
927928
// Reuse ConvertPortToPortConfig so that it is consistent
928-
portConfig, err := opts.ConvertPortToPortConfig(nat.Port(key), portBindings)
929+
portConfig, err := swarmopts.ConvertPortToPortConfig(nat.Port(key), portBindings)
929930
if err != nil {
930931
return nil, err
931932
}

‎opts/opts_deprecated.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package opts
2+
3+
import "github.com/docker/cli/opts/swarmopts"
4+
5+
// PortOpt represents a port config in swarm mode.
6+
//
7+
// Deprecated: use [swarmopts.PortOpt]
8+
type PortOpt = swarmopts.PortOpt
9+
10+
// ConfigOpt is a Value type for parsing configs.
11+
//
12+
// Deprecated: use [swarmopts.ConfigOpt]
13+
type ConfigOpt = swarmopts.ConfigOpt
14+
15+
// SecretOpt is a Value type for parsing secrets
16+
//
17+
// Deprecated: use [swarmopts.SecretOpt]
18+
type SecretOpt = swarmopts.SecretOpt

opts/config.go renamed to opts/swarmopts/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package opts
1+
package swarmopts
22

33
import (
44
"encoding/csv"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package opts
1+
package swarmopts
22

33
import (
44
"os"

opts/port.go renamed to opts/swarmopts/port.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package opts
1+
package swarmopts
22

33
import (
44
"encoding/csv"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package opts
1+
package swarmopts
22

33
import (
44
"bytes"

opts/secret.go renamed to opts/swarmopts/secret.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package opts
1+
package swarmopts
22

33
import (
44
"encoding/csv"

0 commit comments

Comments
 (0)