github.com/scaleway/scaleway-cli@v1.11.1/pkg/commands/events.go (about)

     1  // Copyright (C) 2015 Scaleway. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE.md file.
     4  
     5  package commands
     6  
     7  import (
     8  	"fmt"
     9  	"time"
    10  
    11  	"github.com/docker/go-units"
    12  )
    13  
    14  // EventsArgs are arguments passed to `RunEvents`
    15  type EventsArgs struct{}
    16  
    17  // RunEvents is the handler for 'scw events'
    18  func RunEvents(ctx CommandContext, args EventsArgs) error {
    19  	events, err := ctx.API.GetTasks()
    20  	if err != nil {
    21  		return fmt.Errorf("unable to fetch tasks from the Scaleway API: %v", err)
    22  	}
    23  
    24  	for _, event := range *events {
    25  		startedAt, err := time.Parse("2006-01-02T15:04:05.000000+00:00", event.StartDate)
    26  		if err != nil {
    27  			return fmt.Errorf("unable to parse started date from the Scaleway API: %v", err)
    28  		}
    29  
    30  		terminatedAt := ""
    31  		if event.TerminationDate != "" {
    32  			terminatedAtTime, err := time.Parse("2006-01-02T15:04:05.000000+00:00", event.TerminationDate)
    33  			if err != nil {
    34  				return fmt.Errorf("unable to parse terminated date from the Scaleway API: %v", err)
    35  			}
    36  			terminatedAt = units.HumanDuration(time.Now().UTC().Sub(terminatedAtTime))
    37  		}
    38  
    39  		fmt.Fprintf(ctx.Stdout, "%s %s: %s (%s %d) %s\n", startedAt, event.HrefFrom, event.Description, event.Status, event.Progress, terminatedAt)
    40  	}
    41  	return nil
    42  }