From d9524d92a9ddf02fa8f209291032ce4e37cb2a3f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 10 May 2022 22:35:09 +0200 Subject: [PATCH] api/types/swarm: Version: implement stringer interface makes the code a bit more DRY. Signed-off-by: Sebastiaan van Stijn --- api/types/swarm/common.go | 10 +++++++++- client/config_update.go | 3 +-- client/node_update.go | 3 +-- client/secret_update.go | 3 +-- client/service_update.go | 3 +-- client/swarm_update.go | 9 ++++----- client/volume_update.go | 3 +-- 7 files changed, 18 insertions(+), 16 deletions(-) diff --git a/api/types/swarm/common.go b/api/types/swarm/common.go index ef020f458b..5ded7dba8a 100644 --- a/api/types/swarm/common.go +++ b/api/types/swarm/common.go @@ -1,12 +1,20 @@ package swarm // import "github.com/docker/docker/api/types/swarm" -import "time" +import ( + "strconv" + "time" +) // Version represents the internal object version. type Version struct { Index uint64 `json:",omitempty"` } +// String implements fmt.Stringer interface. +func (v Version) String() string { + return strconv.FormatUint(v.Index, 10) +} + // Meta is a base object inherited by most of the other once. type Meta struct { Version Version `json:",omitempty"` diff --git a/client/config_update.go b/client/config_update.go index ba79ae64e5..1ac2985435 100644 --- a/client/config_update.go +++ b/client/config_update.go @@ -3,7 +3,6 @@ package client // import "github.com/docker/docker/client" import ( "context" "net/url" - "strconv" "github.com/docker/docker/api/types/swarm" ) @@ -14,7 +13,7 @@ func (cli *Client) ConfigUpdate(ctx context.Context, id string, version swarm.Ve return err } query := url.Values{} - query.Set("version", strconv.FormatUint(version.Index, 10)) + query.Set("version", version.String()) resp, err := cli.post(ctx, "/configs/"+id+"/update", query, config, nil) ensureReaderClosed(resp) return err diff --git a/client/node_update.go b/client/node_update.go index de32a617fb..0d0fc3b788 100644 --- a/client/node_update.go +++ b/client/node_update.go @@ -3,7 +3,6 @@ package client // import "github.com/docker/docker/client" import ( "context" "net/url" - "strconv" "github.com/docker/docker/api/types/swarm" ) @@ -11,7 +10,7 @@ import ( // NodeUpdate updates a Node. func (cli *Client) NodeUpdate(ctx context.Context, nodeID string, version swarm.Version, node swarm.NodeSpec) error { query := url.Values{} - query.Set("version", strconv.FormatUint(version.Index, 10)) + query.Set("version", version.String()) resp, err := cli.post(ctx, "/nodes/"+nodeID+"/update", query, node, nil) ensureReaderClosed(resp) return err diff --git a/client/secret_update.go b/client/secret_update.go index d082dcef75..2e939e8ced 100644 --- a/client/secret_update.go +++ b/client/secret_update.go @@ -3,7 +3,6 @@ package client // import "github.com/docker/docker/client" import ( "context" "net/url" - "strconv" "github.com/docker/docker/api/types/swarm" ) @@ -14,7 +13,7 @@ func (cli *Client) SecretUpdate(ctx context.Context, id string, version swarm.Ve return err } query := url.Values{} - query.Set("version", strconv.FormatUint(version.Index, 10)) + query.Set("version", version.String()) resp, err := cli.post(ctx, "/secrets/"+id+"/update", query, secret, nil) ensureReaderClosed(resp) return err diff --git a/client/service_update.go b/client/service_update.go index c63895f74f..8014b86258 100644 --- a/client/service_update.go +++ b/client/service_update.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "net/url" - "strconv" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/swarm" @@ -35,7 +34,7 @@ func (cli *Client) ServiceUpdate(ctx context.Context, serviceID string, version query.Set("rollback", options.Rollback) } - query.Set("version", strconv.FormatUint(version.Index, 10)) + query.Set("version", version.String()) if err := validateServiceSpec(service); err != nil { return response, err diff --git a/client/swarm_update.go b/client/swarm_update.go index 56a5bea761..9fde7d75ee 100644 --- a/client/swarm_update.go +++ b/client/swarm_update.go @@ -2,7 +2,6 @@ package client // import "github.com/docker/docker/client" import ( "context" - "fmt" "net/url" "strconv" @@ -12,10 +11,10 @@ import ( // SwarmUpdate updates the swarm. func (cli *Client) SwarmUpdate(ctx context.Context, version swarm.Version, swarm swarm.Spec, flags swarm.UpdateFlags) error { query := url.Values{} - query.Set("version", strconv.FormatUint(version.Index, 10)) - query.Set("rotateWorkerToken", fmt.Sprintf("%v", flags.RotateWorkerToken)) - query.Set("rotateManagerToken", fmt.Sprintf("%v", flags.RotateManagerToken)) - query.Set("rotateManagerUnlockKey", fmt.Sprintf("%v", flags.RotateManagerUnlockKey)) + query.Set("version", version.String()) + query.Set("rotateWorkerToken", strconv.FormatBool(flags.RotateWorkerToken)) + query.Set("rotateManagerToken", strconv.FormatBool(flags.RotateManagerToken)) + query.Set("rotateManagerUnlockKey", strconv.FormatBool(flags.RotateManagerUnlockKey)) resp, err := cli.post(ctx, "/swarm/update", query, swarm, nil) ensureReaderClosed(resp) return err diff --git a/client/volume_update.go b/client/volume_update.go index 9753e05ef1..33bd31e531 100644 --- a/client/volume_update.go +++ b/client/volume_update.go @@ -3,7 +3,6 @@ package client // import "github.com/docker/docker/client" import ( "context" "net/url" - "strconv" "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/volume" @@ -17,7 +16,7 @@ func (cli *Client) VolumeUpdate(ctx context.Context, volumeID string, version sw } query := url.Values{} - query.Set("version", strconv.FormatUint(version.Index, 10)) + query.Set("version", version.String()) resp, err := cli.put(ctx, "/volumes/"+volumeID, query, options, nil) ensureReaderClosed(resp)