Skip to content

Commit

Permalink
Refactored 'wait' command (scaleway#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Aug 7, 2015
1 parent 617ee4a commit f11348e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
28 changes: 10 additions & 18 deletions pkg/cli/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
}
36 changes: 36 additions & 0 deletions pkg/commands/wait.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f11348e

Please sign in to comment.