From a16fed83af4716a80c79ad44712dfafff559e3df Mon Sep 17 00:00:00 2001 From: allencloud Date: Mon, 19 Sep 2016 16:40:44 +0800 Subject: [PATCH] validate service parameter in client side to avoid api call Signed-off-by: allencloud --- command/service/scale.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/command/service/scale.go b/command/service/scale.go index 2e2982db43..61b73bc354 100644 --- a/command/service/scale.go +++ b/command/service/scale.go @@ -46,9 +46,17 @@ func runScale(dockerCli *command.DockerCli, args []string) error { var errors []string for _, arg := range args { parts := strings.SplitN(arg, "=", 2) - serviceID, scale := parts[0], parts[1] + serviceID, scaleStr := parts[0], parts[1] + + // validate input arg scale number + scale, err := strconv.ParseUint(scaleStr, 10, 64) + if err != nil { + errors = append(errors, fmt.Sprintf("%s: invalid replicas value %s: %v", serviceID, scaleStr, err)) + continue + } + if err := runServiceScale(dockerCli, serviceID, scale); err != nil { - errors = append(errors, fmt.Sprintf("%s: %s", serviceID, err.Error())) + errors = append(errors, fmt.Sprintf("%s: %v", serviceID, err)) } } @@ -58,12 +66,11 @@ func runScale(dockerCli *command.DockerCli, args []string) error { return fmt.Errorf(strings.Join(errors, "\n")) } -func runServiceScale(dockerCli *command.DockerCli, serviceID string, scale string) error { +func runServiceScale(dockerCli *command.DockerCli, serviceID string, scale uint64) error { client := dockerCli.Client() ctx := context.Background() service, _, err := client.ServiceInspectWithRaw(ctx, serviceID) - if err != nil { return err } @@ -72,17 +79,14 @@ func runServiceScale(dockerCli *command.DockerCli, serviceID string, scale strin if serviceMode.Replicated == nil { return fmt.Errorf("scale can only be used with replicated mode") } - uintScale, err := strconv.ParseUint(scale, 10, 64) - if err != nil { - return fmt.Errorf("invalid replicas value %s: %s", scale, err.Error()) - } - serviceMode.Replicated.Replicas = &uintScale + + serviceMode.Replicated.Replicas = &scale err = client.ServiceUpdate(ctx, service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{}) if err != nil { return err } - fmt.Fprintf(dockerCli.Out(), "%s scaled to %s\n", serviceID, scale) + fmt.Fprintf(dockerCli.Out(), "%s scaled to %d\n", serviceID, scale) return nil }