Skip to content

Commit

Permalink
Refactored 'events' command (scaleway#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Aug 6, 2015
1 parent 712735a commit 7bc8e27
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions pkg/commands/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

var cmdEvents = &types.Command{
Exec: runEvents,
Exec: cmdExecEvents,
UsageLine: "events [OPTIONS]",
Description: "Get real time events from the API",
Help: "Get real time events from the API.",
Expand All @@ -28,34 +28,47 @@ func init() {
// Flags
var eventsHelp bool // -h, --help flag

func runEvents(cmd *types.Command, args []string) {
type EventsArgs struct{}

func cmdExecEvents(cmd *types.Command, rawArgs []string) {
if eventsHelp {
cmd.PrintUsage()
}
if len(args) != 0 {
if len(rawArgs) != 0 {
cmd.PrintShortUsage()
}

events, err := cmd.API.GetTasks()
args := EventsArgs{}
ctx := cmd.GetContext(rawArgs)
err := RunEvents(ctx, args)
if err != nil {
log.Fatalf("Cannot execute 'events': %v", err)
}
}

// RunEvents is the handler for 'scw events'
func RunEvents(ctx types.CommandContext, args EventsArgs) error {
events, err := ctx.API.GetTasks()
if err != nil {
log.Fatalf("unable to fetch tasks from the Scaleway API: %v", err)
return fmt.Errorf("unable to fetch tasks from the Scaleway API: %v", err)
}

for _, event := range *events {
startedAt, err := time.Parse("2006-01-02T15:04:05.000000+00:00", event.StartDate)
if err != nil {
log.Fatalf("unable to parse started date from the Scaleway API: %v", err)
return fmt.Errorf("unable to parse started date from the Scaleway API: %v", err)
}

terminatedAt := ""
if event.TerminationDate != "" {
terminatedAtTime, err := time.Parse("2006-01-02T15:04:05.000000+00:00", event.TerminationDate)
if err != nil {
log.Fatalf("unable to parse terminated date from the Scaleway API: %v", err)
return fmt.Errorf("unable to parse terminated date from the Scaleway API: %v", err)
}
terminatedAt = units.HumanDuration(time.Now().UTC().Sub(terminatedAtTime))
}

fmt.Printf("%s %s: %s (%s %d) %s\n", startedAt, event.HrefFrom, event.Description, event.Status, event.Progress, terminatedAt)
}
return nil
}

0 comments on commit 7bc8e27

Please sign in to comment.