diff --git a/pkg/cli/history.go b/pkg/cli/history.go index 8fbd8272f4..d6a865725e 100644 --- a/pkg/cli/history.go +++ b/pkg/cli/history.go @@ -5,15 +5,8 @@ package cli import ( - "fmt" - "os" - "text/tabwriter" - "time" - - log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus" - "github.com/scaleway/scaleway-cli/vendor/github.com/docker/docker/pkg/units" - - utils "github.com/scaleway/scaleway-cli/pkg/utils" + "github.com/Sirupsen/logrus" + "github.com/scaleway/scaleway-cli/pkg/commands" ) var cmdHistory = &Command{ @@ -34,39 +27,22 @@ var historyNoTrunc bool // --no-trunc flag var historyQuiet bool // -q, --quiet flag var historyHelp bool // -h, --help flag -func runHistory(cmd *Command, args []string) { +func runHistory(cmd *Command, rawArgs []string) { if historyHelp { cmd.PrintUsage() } - if len(args) != 1 { + if len(rawArgs) != 1 { cmd.PrintShortUsage() } - imageID := cmd.API.GetImageID(args[0], true) - image, err := cmd.API.GetImage(imageID) - if err != nil { - log.Fatalf("Cannot get image %s: %v", imageID, err) - } - - if imagesQ { - fmt.Println(imageID) - return + args := commands.HistoryArgs{ + Quiet: historyQuiet, + NoTrunc: historyNoTrunc, + Image: rawArgs[0], } - - w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0) - defer w.Flush() - fmt.Fprintf(w, "IMAGE\tCREATED\tCREATED BY\tSIZE\n") - - identifier := utils.TruncIf(image.Identifier, 8, !historyNoTrunc) - - creationDate, err := time.Parse("2006-01-02T15:04:05.000000+00:00", image.CreationDate) + ctx := cmd.GetContext(rawArgs) + err := commands.RunHistory(ctx, args) if err != nil { - log.Fatalf("Unable to parse creation date from the Scaleway API: %v", err) + logrus.Fatalf("Cannot execute 'history': %v", err) } - creationDateStr := units.HumanDuration(time.Now().UTC().Sub(creationDate)) - - volumeName := utils.TruncIf(image.RootVolume.Name, 25, !historyNoTrunc) - size := units.HumanSize(float64(image.RootVolume.Size)) - - fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", identifier, creationDateStr, volumeName, size) } diff --git a/pkg/commands/history.go b/pkg/commands/history.go new file mode 100644 index 0000000000..ed09de4a3f --- /dev/null +++ b/pkg/commands/history.go @@ -0,0 +1,53 @@ +// 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" + "text/tabwriter" + "time" + + "github.com/docker/docker/pkg/units" + "github.com/scaleway/scaleway-cli/pkg/utils" +) + +// HistoryArgs are flags for the `RunHistory` function +type HistoryArgs struct { + NoTrunc bool + Quiet bool + Image string +} + +// RunHistory is the handler for 'scw history' +func RunHistory(ctx CommandContext, args HistoryArgs) error { + imageID := ctx.API.GetImageID(args.Image, true) + image, err := ctx.API.GetImage(imageID) + if err != nil { + return fmt.Errorf("cannot get image %s: %v", imageID, err) + } + + if args.Quiet { + fmt.Fprintln(ctx.Stdout, imageID) + return nil + } + + w := tabwriter.NewWriter(ctx.Stdout, 10, 1, 3, ' ', 0) + defer w.Flush() + fmt.Fprintf(w, "IMAGE\tCREATED\tCREATED BY\tSIZE\n") + + identifier := utils.TruncIf(image.Identifier, 8, !args.NoTrunc) + + creationDate, err := time.Parse("2006-01-02T15:04:05.000000+00:00", image.CreationDate) + if err != nil { + return fmt.Errorf("unable to parse creation date from the Scaleway API: %v", err) + } + creationDateStr := units.HumanDuration(time.Now().UTC().Sub(creationDate)) + + volumeName := utils.TruncIf(image.RootVolume.Name, 25, !args.NoTrunc) + size := units.HumanSize(float64(image.RootVolume.Size)) + + fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", identifier, creationDateStr, volumeName, size) + return nil +}