Use constants for flag names.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2016-06-14 12:33:50 -04:00
parent 6381ed14d1
commit 1b2c59bf86
2 changed files with 49 additions and 35 deletions

View file

@ -432,13 +432,13 @@ func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) {
// Any flags that are not common are added separately in the individual command
func addServiceFlags(cmd *cobra.Command, opts *serviceOptions) {
flags := cmd.Flags()
flags.StringVar(&opts.name, "name", "", "Service name")
flags.VarP(&opts.labels, "label", "l", "Service labels")
flags.StringVar(&opts.name, flagName, "", "Service name")
flags.VarP(&opts.labels, flagLabel, "l", "Service labels")
flags.VarP(&opts.env, "env", "e", "Set environment variables")
flags.StringVarP(&opts.workdir, "workdir", "w", "", "Working directory inside the container")
flags.StringVarP(&opts.user, "user", "u", "", "Username or UID")
flags.VarP(&opts.mounts, "mount", "m", "Attach a mount to the service")
flags.VarP(&opts.mounts, flagMount, "m", "Attach a mount to the service")
flags.Var(&opts.resources.limitCPU, "limit-cpu", "Limit CPUs")
flags.Var(&opts.resources.limitMemBytes, "limit-memory", "Limit Memory")
@ -446,20 +446,34 @@ func addServiceFlags(cmd *cobra.Command, opts *serviceOptions) {
flags.Var(&opts.resources.resMemBytes, "reserve-memory", "Reserve Memory")
flags.Var(&opts.stopGrace, "stop-grace-period", "Time to wait before force killing a container")
flags.StringVar(&opts.mode, "mode", "replicated", "Service mode (replicated or global)")
flags.Var(&opts.replicas, "replicas", "Number of tasks")
flags.StringVar(&opts.mode, flagMode, "replicated", "Service mode (replicated or global)")
flags.Var(&opts.replicas, flagReplicas, "Number of tasks")
flags.StringVar(&opts.restartPolicy.condition, "restart-condition", "", "Restart when condition is met (none, on_failure, or any)")
flags.StringVar(&opts.restartPolicy.condition, flagRestartCondition, "", "Restart when condition is met (none, on_failure, or any)")
flags.Var(&opts.restartPolicy.delay, "restart-delay", "Delay between restart attempts")
flags.Var(&opts.restartPolicy.maxAttempts, "restart-max-attempts", "Maximum number of restarts before giving up")
flags.Var(&opts.restartPolicy.window, "restart-window", "Window used to evalulate the restart policy")
flags.StringSliceVar(&opts.constraints, "constraint", []string{}, "Placement constraints")
flags.Uint64Var(&opts.update.parallelism, "update-parallelism", 1, "Maximum number of tasks updated simultaneously")
flags.DurationVar(&opts.update.delay, "update-delay", time.Duration(0), "Delay between updates")
flags.Uint64Var(&opts.update.parallelism, flagUpdateParallelism, 1, "Maximum number of tasks updated simultaneously")
flags.DurationVar(&opts.update.delay, flagUpdateDelay, time.Duration(0), "Delay between updates")
flags.StringSliceVar(&opts.networks, "network", []string{}, "Network attachments")
flags.StringVar(&opts.endpoint.mode, "endpoint-mode", "", "Endpoint mode(Valid values: VIP, DNSRR)")
flags.VarP(&opts.endpoint.ports, "publish", "p", "Publish a port as a node port")
flags.StringSliceVar(&opts.networks, flagNetwork, []string{}, "Network attachments")
flags.StringVar(&opts.endpoint.mode, flagEndpointMode, "", "Endpoint mode(Valid values: VIP, DNSRR)")
flags.VarP(&opts.endpoint.ports, flagPublish, "p", "Publish a port as a node port")
}
const (
flagName = "name"
flagLabel = "label"
flagMount = "mount"
flagMode = "mode"
flagReplicas = "replicas"
flagPublish = "publish"
flagNetwork = "network"
flagRestartCondition = "restart-condition"
flagEndpointMode = "endpoint-mode"
flagUpdateParallelism = "update-parallelism"
flagUpdateDelay = "update-delay"
)

View file

@ -112,7 +112,7 @@ func mergeService(spec *swarm.ServiceSpec, flags *pflag.FlagSet) error {
cspec := &spec.TaskTemplate.ContainerSpec
task := &spec.TaskTemplate
mergeString("name", &spec.Name)
mergeString(flagName, &spec.Name)
mergeLabels(flags, &spec.Labels)
mergeString("image", &cspec.Image)
mergeSlice("command", &cspec.Command)
@ -129,25 +129,25 @@ func mergeService(spec *swarm.ServiceSpec, flags *pflag.FlagSet) error {
mergeDurationOpt("stop-grace-period", cspec.StopGracePeriod)
if flags.Changed("restart-policy-condition") {
value, _ := flags.GetString("restart-policy-condition")
if flags.Changed(flagRestartCondition) {
value, _ := flags.GetString(flagRestartCondition)
task.RestartPolicy.Condition = swarm.RestartPolicyCondition(value)
}
mergeDurationOpt("restart-policy-delay", task.RestartPolicy.Delay)
mergeUint64Opt("restart-policy-max-attempts", task.RestartPolicy.MaxAttempts)
mergeDurationOpt("restart-policy-window", task.RestartPolicy.Window)
mergeDurationOpt("restart-delay", task.RestartPolicy.Delay)
mergeUint64Opt("restart-max-attempts", task.RestartPolicy.MaxAttempts)
mergeDurationOpt("restart-window", task.RestartPolicy.Window)
mergeSlice("constraint", &task.Placement.Constraints)
if err := mergeMode(flags, &spec.Mode); err != nil {
return err
}
mergeUint64("updateconfig-parallelism", &spec.UpdateConfig.Parallelism)
mergeDuration("updateconfig-delay", &spec.UpdateConfig.Delay)
mergeUint64(flagUpdateParallelism, &spec.UpdateConfig.Parallelism)
mergeDuration(flagUpdateDelay, &spec.UpdateConfig.Delay)
mergeNetworks(flags, &spec.Networks)
if flags.Changed("endpoint-mode") {
value, _ := flags.GetString("endpoint-mode")
if flags.Changed(flagEndpointMode) {
value, _ := flags.GetString(flagEndpointMode)
spec.EndpointSpec.Mode = swarm.ResolutionMode(value)
}
@ -157,7 +157,7 @@ func mergeService(spec *swarm.ServiceSpec, flags *pflag.FlagSet) error {
}
func mergeLabels(flags *pflag.FlagSet, field *map[string]string) {
if !flags.Changed("label") {
if !flags.Changed(flagLabel) {
return
}
@ -165,7 +165,7 @@ func mergeLabels(flags *pflag.FlagSet, field *map[string]string) {
*field = make(map[string]string)
}
values := flags.Lookup("label").Value.(*opts.ListOpts).GetAll()
values := flags.Lookup(flagLabel).Value.(*opts.ListOpts).GetAll()
for key, value := range runconfigopts.ConvertKVStringsToMap(values) {
(*field)[key] = value
}
@ -173,21 +173,21 @@ func mergeLabels(flags *pflag.FlagSet, field *map[string]string) {
// TODO: should this override by destination path, or does swarm handle that?
func mergeMounts(flags *pflag.FlagSet, mounts *[]swarm.Mount) {
if !flags.Changed("mount") {
if !flags.Changed(flagMount) {
return
}
values := flags.Lookup("mount").Value.(*MountOpt).Value()
values := flags.Lookup(flagMount).Value.(*MountOpt).Value()
*mounts = append(*mounts, values...)
}
// TODO: should this override by name, or does swarm handle that?
func mergePorts(flags *pflag.FlagSet, portConfig *[]swarm.PortConfig) {
if !flags.Changed("ports") {
if !flags.Changed(flagPublish) {
return
}
values := flags.Lookup("ports").Value.(*opts.ListOpts).GetAll()
values := flags.Lookup(flagPublish).Value.(*opts.ListOpts).GetAll()
ports, portBindings, _ := nat.ParsePortSpecs(values)
for port := range ports {
@ -196,26 +196,26 @@ func mergePorts(flags *pflag.FlagSet, portConfig *[]swarm.PortConfig) {
}
func mergeNetworks(flags *pflag.FlagSet, attachments *[]swarm.NetworkAttachmentConfig) {
if !flags.Changed("network") {
if !flags.Changed(flagNetwork) {
return
}
networks, _ := flags.GetStringSlice("network")
networks, _ := flags.GetStringSlice(flagNetwork)
for _, network := range networks {
*attachments = append(*attachments, swarm.NetworkAttachmentConfig{Target: network})
}
}
func mergeMode(flags *pflag.FlagSet, serviceMode *swarm.ServiceMode) error {
if !flags.Changed("mode") && !flags.Changed("scale") {
if !flags.Changed(flagMode) && !flags.Changed(flagReplicas) {
return nil
}
var mode string
if flags.Changed("mode") {
mode, _ = flags.GetString("mode")
if flags.Changed(flagMode) {
mode, _ = flags.GetString(flagMode)
}
if !(mode == "replicated" || serviceMode.Replicated != nil) && flags.Changed("replicas") {
if !(mode == "replicated" || serviceMode.Replicated != nil) && flags.Changed(flagReplicas) {
return fmt.Errorf("replicas can only be used with replicated mode")
}
@ -225,8 +225,8 @@ func mergeMode(flags *pflag.FlagSet, serviceMode *swarm.ServiceMode) error {
return nil
}
if flags.Changed("replicas") {
replicas := flags.Lookup("replicas").Value.(*Uint64Opt).Value()
if flags.Changed(flagReplicas) {
replicas := flags.Lookup(flagReplicas).Value.(*Uint64Opt).Value()
serviceMode.Replicated = &swarm.ReplicatedService{Replicas: replicas}
serviceMode.Global = nil
return nil