validate service parameter in client side to avoid api call

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud 2016-09-19 16:40:44 +08:00
parent e58c0c6e1b
commit a16fed83af
1 changed files with 14 additions and 10 deletions

View File

@ -46,9 +46,17 @@ func runScale(dockerCli *command.DockerCli, args []string) error {
var errors []string var errors []string
for _, arg := range args { for _, arg := range args {
parts := strings.SplitN(arg, "=", 2) 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 { 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")) 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() client := dockerCli.Client()
ctx := context.Background() ctx := context.Background()
service, _, err := client.ServiceInspectWithRaw(ctx, serviceID) service, _, err := client.ServiceInspectWithRaw(ctx, serviceID)
if err != nil { if err != nil {
return err return err
} }
@ -72,17 +79,14 @@ func runServiceScale(dockerCli *command.DockerCli, serviceID string, scale strin
if serviceMode.Replicated == nil { if serviceMode.Replicated == nil {
return fmt.Errorf("scale can only be used with replicated mode") return fmt.Errorf("scale can only be used with replicated mode")
} }
uintScale, err := strconv.ParseUint(scale, 10, 64)
if err != nil { serviceMode.Replicated.Replicas = &scale
return fmt.Errorf("invalid replicas value %s: %s", scale, err.Error())
}
serviceMode.Replicated.Replicas = &uintScale
err = client.ServiceUpdate(ctx, service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{}) err = client.ServiceUpdate(ctx, service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
if err != nil { if err != nil {
return err 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 return nil
} }