From f11348ebaa0984a25b6a1513ba60348523f2250c Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Fri, 7 Aug 2015 11:01:39 +0200 Subject: [PATCH] Refactored 'wait' command (#80) --- pkg/cli/wait.go | 28 ++++++++++------------------ pkg/commands/wait.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 18 deletions(-) create mode 100644 pkg/commands/wait.go diff --git a/pkg/cli/wait.go b/pkg/cli/wait.go index 54b3a9724b..7086bf909d 100644 --- a/pkg/cli/wait.go +++ b/pkg/cli/wait.go @@ -5,11 +5,9 @@ package cli import ( - "os" + "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus" - log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus" - - "github.com/scaleway/scaleway-cli/pkg/api" + "github.com/scaleway/scaleway-cli/pkg/commands" ) var cmdWait = &Command{ @@ -26,26 +24,20 @@ func init() { // Flags var waitHelp bool // -h, --help flag -func runWait(cmd *Command, args []string) { +func runWait(cmd *Command, rawArgs []string) { if waitHelp { cmd.PrintUsage() } - if len(args) < 1 { + if len(rawArgs) < 1 { cmd.PrintShortUsage() } - hasError := false - for _, needle := range args { - serverIdentifier := cmd.API.GetServerID(needle) - - _, err := api.WaitForServerStopped(cmd.API, serverIdentifier) - if err != nil { - log.Errorf("failed to wait for server %s: %v", serverIdentifier, err) - hasError = true - } + args := commands.WaitArgs{ + Servers: rawArgs, } - - if hasError { - os.Exit(1) + ctx := cmd.GetContext(rawArgs) + err := commands.RunWait(ctx, args) + if err != nil { + logrus.Fatalf("Cannot execute 'wait': %v", err) } } diff --git a/pkg/commands/wait.go b/pkg/commands/wait.go new file mode 100644 index 0000000000..e178aecf65 --- /dev/null +++ b/pkg/commands/wait.go @@ -0,0 +1,36 @@ +// Copyright (C) 2015 Scaleway. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE.md file. + +package commands + +import ( + "fmt" + + "github.com/scaleway/scaleway-cli/pkg/api" + "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus" +) + +// WaitArgs are flags for the `RunWait` function +type WaitArgs struct { + Servers []string +} + +// RunWait is the handler for 'scw wait' +func RunWait(ctx CommandContext, args WaitArgs) error { + hasError := false + for _, needle := range args.Servers { + serverIdentifier := ctx.API.GetServerID(needle) + + _, err := api.WaitForServerStopped(ctx.API, serverIdentifier) + if err != nil { + logrus.Errorf("failed to wait for server %s: %v", serverIdentifier, err) + hasError = true + } + } + + if hasError { + return fmt.Errorf("at least 1 server failed to be stopped") + } + return nil +}