Skip to content

Commit

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

0 comments on commit 2f80f5e

Please sign in to comment.