github.com/scaleway/scaleway-cli@v1.11.1/pkg/commands/history.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  	"text/tabwriter"
    10  	"time"
    11  
    12  	"github.com/docker/go-units"
    13  	"github.com/scaleway/scaleway-cli/pkg/utils"
    14  )
    15  
    16  // HistoryArgs are flags for the `RunHistory` function
    17  type HistoryArgs struct {
    18  	NoTrunc bool
    19  	Quiet   bool
    20  	Image   string
    21  	Arch    string
    22  }
    23  
    24  // RunHistory is the handler for 'scw history'
    25  func RunHistory(ctx CommandContext, args HistoryArgs) error {
    26  	imageID, err := ctx.API.GetImageID(args.Image, args.Arch)
    27  	if err != nil {
    28  		return err
    29  	}
    30  	image, err := ctx.API.GetImage(imageID.Identifier)
    31  	if err != nil {
    32  		return fmt.Errorf("cannot get image %s: %v", imageID.Identifier, err)
    33  	}
    34  
    35  	if args.Quiet {
    36  		fmt.Fprintln(ctx.Stdout, imageID.Identifier)
    37  		return nil
    38  	}
    39  
    40  	w := tabwriter.NewWriter(ctx.Stdout, 10, 1, 3, ' ', 0)
    41  	defer w.Flush()
    42  	fmt.Fprintf(w, "IMAGE\tCREATED\tCREATED BY\tSIZE\n")
    43  
    44  	identifier := utils.TruncIf(image.Identifier, 8, !args.NoTrunc)
    45  
    46  	creationDate, err := time.Parse("2006-01-02T15:04:05.000000+00:00", image.CreationDate)
    47  	if err != nil {
    48  		return fmt.Errorf("unable to parse creation date from the Scaleway API: %v", err)
    49  	}
    50  	creationDateStr := units.HumanDuration(time.Now().UTC().Sub(creationDate))
    51  
    52  	volumeName := utils.TruncIf(image.RootVolume.Name, 25, !args.NoTrunc)
    53  	size := units.HumanSize(float64(image.RootVolume.Size))
    54  
    55  	fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", identifier, creationDateStr, volumeName, size)
    56  	return nil
    57  }