Merge pull request #77 from vdemeester/move-duration-opts

Move duration opts into an opts package
This commit is contained in:
Sebastiaan van Stijn 2017-05-18 11:15:30 +02:00 committed by GitHub
commit eea4a38cd6
5 changed files with 106 additions and 84 deletions

View File

@ -25,56 +25,6 @@ type int64Value interface {
Value() int64
}
// PositiveDurationOpt is an option type for time.Duration that uses a pointer.
// It bahave similarly to DurationOpt but only allows positive duration values.
type PositiveDurationOpt struct {
DurationOpt
}
// Set a new value on the option. Setting a negative duration value will cause
// an error to be returned.
func (d *PositiveDurationOpt) Set(s string) error {
err := d.DurationOpt.Set(s)
if err != nil {
return err
}
if *d.DurationOpt.value < 0 {
return errors.Errorf("duration cannot be negative")
}
return nil
}
// DurationOpt is an option type for time.Duration that uses a pointer. This
// allows us to get nil values outside, instead of defaulting to 0
type DurationOpt struct {
value *time.Duration
}
// Set a new value on the option
func (d *DurationOpt) Set(s string) error {
v, err := time.ParseDuration(s)
d.value = &v
return err
}
// Type returns the type of this option, which will be displayed in `--help` output
func (d *DurationOpt) Type() string {
return "duration"
}
// String returns a string repr of this option
func (d *DurationOpt) String() string {
if d.value != nil {
return d.value.String()
}
return ""
}
// Value returns the time.Duration
func (d *DurationOpt) Value() *time.Duration {
return d.value
}
// Uint64Opt represents a uint64.
type Uint64Opt struct {
value *uint64
@ -293,9 +243,9 @@ func (r *resourceOptions) ToResourceRequirements() *swarm.ResourceRequirements {
type restartPolicyOptions struct {
condition string
delay DurationOpt
delay opts.DurationOpt
maxAttempts Uint64Opt
window DurationOpt
window opts.DurationOpt
}
func defaultRestartPolicy() *swarm.RestartPolicy {
@ -445,10 +395,10 @@ func (ldo *logDriverOptions) toLogDriver() *swarm.Driver {
type healthCheckOptions struct {
cmd string
interval PositiveDurationOpt
timeout PositiveDurationOpt
interval opts.PositiveDurationOpt
timeout opts.PositiveDurationOpt
retries int
startPeriod PositiveDurationOpt
startPeriod opts.PositiveDurationOpt
noHealthcheck bool
}
@ -530,7 +480,7 @@ type serviceOptions struct {
hosts opts.ListOpts
resources resourceOptions
stopGrace DurationOpt
stopGrace opts.DurationOpt
replicas Uint64Opt
mode string

View File

@ -31,27 +31,6 @@ func TestNanoCPUsSetAndValue(t *testing.T) {
assert.Equal(t, int64(350000000), cpus.Value())
}
func TestDurationOptString(t *testing.T) {
dur := time.Duration(300 * 10e8)
duration := DurationOpt{value: &dur}
assert.Equal(t, "5m0s", duration.String())
}
func TestDurationOptSetAndValue(t *testing.T) {
var duration DurationOpt
assert.NoError(t, duration.Set("300s"))
assert.Equal(t, time.Duration(300*10e8), *duration.Value())
assert.NoError(t, duration.Set("-300s"))
assert.Equal(t, time.Duration(-300*10e8), *duration.Value())
}
func TestPositiveDurationOptSetAndValue(t *testing.T) {
var duration PositiveDurationOpt
assert.NoError(t, duration.Set("300s"))
assert.Equal(t, time.Duration(300*10e8), *duration.Value())
assert.EqualError(t, duration.Set("-300s"), "duration cannot be negative")
}
func TestUint64OptString(t *testing.T) {
value := uint64(2345678)
opt := Uint64Opt{value: &value}
@ -71,9 +50,9 @@ func TestHealthCheckOptionsToHealthConfig(t *testing.T) {
dur := time.Second
opt := healthCheckOptions{
cmd: "curl",
interval: PositiveDurationOpt{DurationOpt{value: &dur}},
timeout: PositiveDurationOpt{DurationOpt{value: &dur}},
startPeriod: PositiveDurationOpt{DurationOpt{value: &dur}},
interval: opts.PositiveDurationOpt{*opts.NewDurationOpt(&dur)},
timeout: opts.PositiveDurationOpt{*opts.NewDurationOpt(&dur)},
startPeriod: opts.PositiveDurationOpt{*opts.NewDurationOpt(&dur)},
retries: 10,
}
config, err := opt.toHealthConfig()

View File

@ -256,7 +256,7 @@ func updateService(ctx context.Context, apiClient client.NetworkAPIClient, flags
updateDurationOpt := func(flag string, field **time.Duration) {
if flags.Changed(flag) {
val := *flags.Lookup(flag).Value.(*DurationOpt).Value()
val := *flags.Lookup(flag).Value.(*opts.DurationOpt).Value()
*field = &val
}
}
@ -963,15 +963,15 @@ func updateHealthcheck(flags *pflag.FlagSet, containerSpec *swarm.ContainerSpec)
containerSpec.Healthcheck.Test = nil
}
if flags.Changed(flagHealthInterval) {
val := *flags.Lookup(flagHealthInterval).Value.(*PositiveDurationOpt).Value()
val := *flags.Lookup(flagHealthInterval).Value.(*opts.PositiveDurationOpt).Value()
containerSpec.Healthcheck.Interval = val
}
if flags.Changed(flagHealthTimeout) {
val := *flags.Lookup(flagHealthTimeout).Value.(*PositiveDurationOpt).Value()
val := *flags.Lookup(flagHealthTimeout).Value.(*opts.PositiveDurationOpt).Value()
containerSpec.Healthcheck.Timeout = val
}
if flags.Changed(flagHealthStartPeriod) {
val := *flags.Lookup(flagHealthStartPeriod).Value.(*PositiveDurationOpt).Value()
val := *flags.Lookup(flagHealthStartPeriod).Value.(*opts.PositiveDurationOpt).Value()
containerSpec.Healthcheck.StartPeriod = val
}
if flags.Changed(flagHealthRetries) {

64
opts/duration.go Normal file
View File

@ -0,0 +1,64 @@
package opts
import (
"time"
"github.com/pkg/errors"
)
// PositiveDurationOpt is an option type for time.Duration that uses a pointer.
// It behave similarly to DurationOpt but only allows positive duration values.
type PositiveDurationOpt struct {
DurationOpt
}
// Set a new value on the option. Setting a negative duration value will cause
// an error to be returned.
func (d *PositiveDurationOpt) Set(s string) error {
err := d.DurationOpt.Set(s)
if err != nil {
return err
}
if *d.DurationOpt.value < 0 {
return errors.Errorf("duration cannot be negative")
}
return nil
}
// DurationOpt is an option type for time.Duration that uses a pointer. This
// allows us to get nil values outside, instead of defaulting to 0
type DurationOpt struct {
value *time.Duration
}
// NewDurationOpt creates a DurationOpt with the specified duration
func NewDurationOpt(value *time.Duration) *DurationOpt {
return &DurationOpt{
value: value,
}
}
// Set a new value on the option
func (d *DurationOpt) Set(s string) error {
v, err := time.ParseDuration(s)
d.value = &v
return err
}
// Type returns the type of this option, which will be displayed in `--help` output
func (d *DurationOpt) Type() string {
return "duration"
}
// String returns a string repr of this option
func (d *DurationOpt) String() string {
if d.value != nil {
return d.value.String()
}
return ""
}
// Value returns the time.Duration
func (d *DurationOpt) Value() *time.Duration {
return d.value
}

29
opts/duration_test.go Normal file
View File

@ -0,0 +1,29 @@
package opts
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDurationOptString(t *testing.T) {
dur := time.Duration(300 * 10e8)
duration := DurationOpt{value: &dur}
assert.Equal(t, "5m0s", duration.String())
}
func TestDurationOptSetAndValue(t *testing.T) {
var duration DurationOpt
assert.NoError(t, duration.Set("300s"))
assert.Equal(t, time.Duration(300*10e8), *duration.Value())
assert.NoError(t, duration.Set("-300s"))
assert.Equal(t, time.Duration(-300*10e8), *duration.Value())
}
func TestPositiveDurationOptSetAndValue(t *testing.T) {
var duration PositiveDurationOpt
assert.NoError(t, duration.Set("300s"))
assert.Equal(t, time.Duration(300*10e8), *duration.Value())
assert.EqualError(t, duration.Set("-300s"), "duration cannot be negative")
}