Skip to content

Commit 9a36a9f

Browse files
committed
Refactored 'history' command (scaleway#80)
1 parent 01152f7 commit 9a36a9f

File tree

2 files changed

+64
-35
lines changed

2 files changed

+64
-35
lines changed

pkg/cli/history.go

+11-35
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@
55
package cli
66

77
import (
8-
"fmt"
9-
"os"
10-
"text/tabwriter"
11-
"time"
12-
13-
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
14-
"github.com/scaleway/scaleway-cli/vendor/github.com/docker/docker/pkg/units"
15-
16-
utils "github.com/scaleway/scaleway-cli/pkg/utils"
8+
"github.com/Sirupsen/logrus"
9+
"github.com/scaleway/scaleway-cli/pkg/commands"
1710
)
1811

1912
var cmdHistory = &Command{
@@ -34,39 +27,22 @@ var historyNoTrunc bool // --no-trunc flag
3427
var historyQuiet bool // -q, --quiet flag
3528
var historyHelp bool // -h, --help flag
3629

37-
func runHistory(cmd *Command, args []string) {
30+
func runHistory(cmd *Command, rawArgs []string) {
3831
if historyHelp {
3932
cmd.PrintUsage()
4033
}
41-
if len(args) != 1 {
34+
if len(rawArgs) != 1 {
4235
cmd.PrintShortUsage()
4336
}
4437

45-
imageID := cmd.API.GetImageID(args[0], true)
46-
image, err := cmd.API.GetImage(imageID)
47-
if err != nil {
48-
log.Fatalf("Cannot get image %s: %v", imageID, err)
49-
}
50-
51-
if imagesQ {
52-
fmt.Println(imageID)
53-
return
38+
args := commands.HistoryArgs{
39+
Quiet: historyQuiet,
40+
NoTrunc: historyNoTrunc,
41+
Image: rawArgs[0],
5442
}
55-
56-
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
57-
defer w.Flush()
58-
fmt.Fprintf(w, "IMAGE\tCREATED\tCREATED BY\tSIZE\n")
59-
60-
identifier := utils.TruncIf(image.Identifier, 8, !historyNoTrunc)
61-
62-
creationDate, err := time.Parse("2006-01-02T15:04:05.000000+00:00", image.CreationDate)
43+
ctx := cmd.GetContext(rawArgs)
44+
err := commands.RunHistory(ctx, args)
6345
if err != nil {
64-
log.Fatalf("Unable to parse creation date from the Scaleway API: %v", err)
46+
logrus.Fatalf("Cannot execute 'history': %v", err)
6547
}
66-
creationDateStr := units.HumanDuration(time.Now().UTC().Sub(creationDate))
67-
68-
volumeName := utils.TruncIf(image.RootVolume.Name, 25, !historyNoTrunc)
69-
size := units.HumanSize(float64(image.RootVolume.Size))
70-
71-
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", identifier, creationDateStr, volumeName, size)
7248
}

pkg/commands/history.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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/docker/pkg/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+
}
22+
23+
// RunHistory is the handler for 'scw history'
24+
func RunHistory(ctx CommandContext, args HistoryArgs) error {
25+
imageID := ctx.API.GetImageID(args.Image, true)
26+
image, err := ctx.API.GetImage(imageID)
27+
if err != nil {
28+
return fmt.Errorf("cannot get image %s: %v", imageID, err)
29+
}
30+
31+
if args.Quiet {
32+
fmt.Fprintln(ctx.Stdout, imageID)
33+
return nil
34+
}
35+
36+
w := tabwriter.NewWriter(ctx.Stdout, 10, 1, 3, ' ', 0)
37+
defer w.Flush()
38+
fmt.Fprintf(w, "IMAGE\tCREATED\tCREATED BY\tSIZE\n")
39+
40+
identifier := utils.TruncIf(image.Identifier, 8, !args.NoTrunc)
41+
42+
creationDate, err := time.Parse("2006-01-02T15:04:05.000000+00:00", image.CreationDate)
43+
if err != nil {
44+
return fmt.Errorf("unable to parse creation date from the Scaleway API: %v", err)
45+
}
46+
creationDateStr := units.HumanDuration(time.Now().UTC().Sub(creationDate))
47+
48+
volumeName := utils.TruncIf(image.RootVolume.Name, 25, !args.NoTrunc)
49+
size := units.HumanSize(float64(image.RootVolume.Size))
50+
51+
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", identifier, creationDateStr, volumeName, size)
52+
return nil
53+
}

0 commit comments

Comments
 (0)