Skip to content

Commit 6fd8c96

Browse files
committed
Merge pull request moby#23344 from pdalpra/timeout-as-time.Duration
Timeout as time.duration
2 parents 3b08711 + b29e8ea commit 6fd8c96

File tree

9 files changed

+67
-22
lines changed

9 files changed

+67
-22
lines changed

api/client/container/restart.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package container
33
import (
44
"fmt"
55
"strings"
6+
"time"
67

78
"golang.org/x/net/context"
89

@@ -39,7 +40,7 @@ func NewRestartCommand(dockerCli *client.DockerCli) *cobra.Command {
3940
func runRestart(dockerCli *client.DockerCli, opts *restartOptions) error {
4041
var errs []string
4142
for _, name := range opts.containers {
42-
if err := dockerCli.Client().ContainerRestart(context.Background(), name, opts.nSeconds); err != nil {
43+
if err := dockerCli.Client().ContainerRestart(context.Background(), name, time.Duration(opts.nSeconds)*time.Second); err != nil {
4344
errs = append(errs, err.Error())
4445
} else {
4546
fmt.Fprintf(dockerCli.Out(), "%s\n", name)

api/client/container/stop.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package container
33
import (
44
"fmt"
55
"strings"
6+
"time"
67

78
"golang.org/x/net/context"
89

@@ -42,7 +43,7 @@ func runStop(dockerCli *client.DockerCli, opts *stopOptions) error {
4243

4344
var errs []string
4445
for _, container := range opts.containers {
45-
if err := dockerCli.Client().ContainerStop(ctx, container, opts.time); err != nil {
46+
if err := dockerCli.Client().ContainerStop(ctx, container, time.Duration(opts.time)*time.Second); err != nil {
4647
errs = append(errs, err.Error())
4748
} else {
4849
fmt.Fprintf(dockerCli.Out(), "%s\n", container)

hack/vendor.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ clone git golang.org/x/net 2beffdc2e92c8a3027590f898fe88f69af48a3f8 https://gith
6060
clone git golang.org/x/sys eb2c74142fd19a79b3f237334c7384d5167b1b46 https://github.com/golang/sys.git
6161
clone git github.com/docker/go-units 651fc226e7441360384da338d0fd37f2440ffbe3
6262
clone git github.com/docker/go-connections v0.2.0
63-
clone git github.com/docker/engine-api 772250a752e34cacaeef7c92b8e0ddf43450b629
63+
clone git github.com/docker/engine-api 8c2141e14bb9e7540938d155976b3ef0661e4814
6464
clone git github.com/RackSec/srslog 259aed10dfa74ea2961eddd1d9847619f6e98837
6565
clone git github.com/imdario/mergo 0.2.1
6666

vendor/src/github.com/docker/engine-api/client/container_restart.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package client
22

33
import (
44
"net/url"
5-
"strconv"
5+
"time"
66

7+
timetypes "github.com/docker/engine-api/types/time"
78
"golang.org/x/net/context"
89
)
910

1011
// ContainerRestart stops and starts a container again.
1112
// It makes the daemon to wait for the container to be up again for
1213
// a specific amount of time, given the timeout.
13-
func (cli *Client) ContainerRestart(ctx context.Context, containerID string, timeout int) error {
14+
func (cli *Client) ContainerRestart(ctx context.Context, containerID string, timeout time.Duration) error {
1415
query := url.Values{}
15-
query.Set("t", strconv.Itoa(timeout))
16+
query.Set("t", timetypes.DurationToSecondsString(timeout))
1617
resp, err := cli.post(ctx, "/containers/"+containerID+"/restart", query, nil, nil)
1718
ensureReaderClosed(resp)
1819
return err

vendor/src/github.com/docker/engine-api/client/container_stop.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package client
22

33
import (
44
"net/url"
5-
"strconv"
5+
"time"
66

7+
timetypes "github.com/docker/engine-api/types/time"
78
"golang.org/x/net/context"
89
)
910

1011
// ContainerStop stops a container without terminating the process.
1112
// The process is blocked until the container stops or the timeout expires.
12-
func (cli *Client) ContainerStop(ctx context.Context, containerID string, timeout int) error {
13+
func (cli *Client) ContainerStop(ctx context.Context, containerID string, timeout time.Duration) error {
1314
query := url.Values{}
14-
query.Set("t", strconv.Itoa(timeout))
15+
query.Set("t", timetypes.DurationToSecondsString(timeout))
1516
resp, err := cli.post(ctx, "/containers/"+containerID+"/stop", query, nil, nil)
1617
ensureReaderClosed(resp)
1718
return err

vendor/src/github.com/docker/engine-api/client/errors.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,49 @@ import (
88
// ErrConnectionFailed is an error raised when the connection between the client and the server failed.
99
var ErrConnectionFailed = errors.New("Cannot connect to the Docker daemon. Is the docker daemon running on this host?")
1010

11+
type notFound interface {
12+
error
13+
NotFound() bool // Is the error a NotFound error
14+
}
15+
16+
// IsErrNotFound returns true if the error is caused with an
17+
// object (image, container, network, volume, …) is not found in the docker host.
18+
func IsErrNotFound(err error) bool {
19+
te, ok := err.(notFound)
20+
return ok && te.NotFound()
21+
}
22+
1123
// imageNotFoundError implements an error returned when an image is not in the docker host.
1224
type imageNotFoundError struct {
1325
imageID string
1426
}
1527

28+
// NoFound indicates that this error type is of NotFound
29+
func (e imageNotFoundError) NotFound() bool {
30+
return true
31+
}
32+
1633
// Error returns a string representation of an imageNotFoundError
17-
func (i imageNotFoundError) Error() string {
18-
return fmt.Sprintf("Error: No such image: %s", i.imageID)
34+
func (e imageNotFoundError) Error() string {
35+
return fmt.Sprintf("Error: No such image: %s", e.imageID)
1936
}
2037

2138
// IsErrImageNotFound returns true if the error is caused
2239
// when an image is not found in the docker host.
2340
func IsErrImageNotFound(err error) bool {
24-
_, ok := err.(imageNotFoundError)
25-
return ok
41+
return IsErrNotFound(err)
2642
}
2743

2844
// containerNotFoundError implements an error returned when a container is not in the docker host.
2945
type containerNotFoundError struct {
3046
containerID string
3147
}
3248

49+
// NoFound indicates that this error type is of NotFound
50+
func (e containerNotFoundError) NotFound() bool {
51+
return true
52+
}
53+
3354
// Error returns a string representation of a containerNotFoundError
3455
func (e containerNotFoundError) Error() string {
3556
return fmt.Sprintf("Error: No such container: %s", e.containerID)
@@ -38,15 +59,19 @@ func (e containerNotFoundError) Error() string {
3859
// IsErrContainerNotFound returns true if the error is caused
3960
// when a container is not found in the docker host.
4061
func IsErrContainerNotFound(err error) bool {
41-
_, ok := err.(containerNotFoundError)
42-
return ok
62+
return IsErrNotFound(err)
4363
}
4464

4565
// networkNotFoundError implements an error returned when a network is not in the docker host.
4666
type networkNotFoundError struct {
4767
networkID string
4868
}
4969

70+
// NoFound indicates that this error type is of NotFound
71+
func (e networkNotFoundError) NotFound() bool {
72+
return true
73+
}
74+
5075
// Error returns a string representation of a networkNotFoundError
5176
func (e networkNotFoundError) Error() string {
5277
return fmt.Sprintf("Error: No such network: %s", e.networkID)
@@ -55,15 +80,19 @@ func (e networkNotFoundError) Error() string {
5580
// IsErrNetworkNotFound returns true if the error is caused
5681
// when a network is not found in the docker host.
5782
func IsErrNetworkNotFound(err error) bool {
58-
_, ok := err.(networkNotFoundError)
59-
return ok
83+
return IsErrNotFound(err)
6084
}
6185

6286
// volumeNotFoundError implements an error returned when a volume is not in the docker host.
6387
type volumeNotFoundError struct {
6488
volumeID string
6589
}
6690

91+
// NoFound indicates that this error type is of NotFound
92+
func (e volumeNotFoundError) NotFound() bool {
93+
return true
94+
}
95+
6796
// Error returns a string representation of a networkNotFoundError
6897
func (e volumeNotFoundError) Error() string {
6998
return fmt.Sprintf("Error: No such volume: %s", e.volumeID)
@@ -72,8 +101,7 @@ func (e volumeNotFoundError) Error() string {
72101
// IsErrVolumeNotFound returns true if the error is caused
73102
// when a volume is not found in the docker host.
74103
func IsErrVolumeNotFound(err error) bool {
75-
_, ok := err.(volumeNotFoundError)
76-
return ok
104+
return IsErrNotFound(err)
77105
}
78106

79107
// unauthorizedError represents an authorization error in a remote registry.

vendor/src/github.com/docker/engine-api/client/interface.go

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

33
import (
44
"io"
5+
"time"
56

67
"golang.org/x/net/context"
78

@@ -37,11 +38,11 @@ type APIClient interface {
3738
ContainerRemove(ctx context.Context, container string, options types.ContainerRemoveOptions) error
3839
ContainerRename(ctx context.Context, container, newContainerName string) error
3940
ContainerResize(ctx context.Context, container string, options types.ResizeOptions) error
40-
ContainerRestart(ctx context.Context, container string, timeout int) error
41+
ContainerRestart(ctx context.Context, container string, timeout time.Duration) error
4142
ContainerStatPath(ctx context.Context, container, path string) (types.ContainerPathStat, error)
4243
ContainerStats(ctx context.Context, container string, stream bool) (io.ReadCloser, error)
4344
ContainerStart(ctx context.Context, container string, options types.ContainerStartOptions) error
44-
ContainerStop(ctx context.Context, container string, timeout int) error
45+
ContainerStop(ctx context.Context, container string, timeout time.Duration) error
4546
ContainerTop(ctx context.Context, container string, arguments []string) (types.ContainerProcessList, error)
4647
ContainerUnpause(ctx context.Context, container string) error
4748
ContainerUpdate(ctx context.Context, container string, updateConfig container.UpdateConfig) error
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package time
2+
3+
import (
4+
"strconv"
5+
"time"
6+
)
7+
8+
// DurationToSecondsString converts the specified duration to the number
9+
// seconds it represents, formatted as a string.
10+
func DurationToSecondsString(duration time.Duration) string {
11+
return strconv.FormatFloat(duration.Seconds(), 'f', 0, 64)
12+
}

vendor/src/github.com/docker/engine-api/types/versions/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Consider moving a type here when you need to keep backwards compatibility in the
99
The package name convention is to use `v` as a prefix for the version number and `p`(patch) as a separator. We use this nomenclature due to a few restrictions in the Go package name convention:
1010

1111
1. We cannot use `.` because it's interpreted by the language, think of `v1.20.CallFunction`.
12-
2. We cannot use `_` because golint complains abount it. The code is actually valid, but it looks probably more weird: `v1_20.CallFunction`.
12+
2. We cannot use `_` because golint complains about it. The code is actually valid, but it looks probably more weird: `v1_20.CallFunction`.
1313

1414
For instance, if you want to modify a type that was available in the version `1.21` of the API but it will have different fields in the version `1.22`, you want to create a new package under `api/types/versions/v1p21`.

0 commit comments

Comments
 (0)