Skip to content

Commit

Permalink
Print ISO representation for storage change time
Browse files Browse the repository at this point in the history
Also clean-up error messages, use better variable names, make test a bit
cleaner (don't just mirror implementation, show what you expect).

Fixes #60
  • Loading branch information
bkircher committed Aug 3, 2020
1 parent f10eb6b commit 9aa13c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
21 changes: 11 additions & 10 deletions cmd/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"strconv"
"time"

"github.com/gridscale/gscloud/render"
log "github.com/sirupsen/logrus"
Expand All @@ -17,7 +18,7 @@ var storageCmd = &cobra.Command{
Long: `List, create, or remove storages.`,
}

var storageListCmd = &cobra.Command{
var storageLsCmd = &cobra.Command{
Use: "ls [flags]",
Aliases: []string{"list"},
Short: "List storages",
Expand All @@ -33,14 +34,14 @@ var storageListCmd = &cobra.Command{
var storageinfo [][]string
if !jsonFlag {
heading := []string{"id", "name", "capacity", "changetime", "status"}
for _, stor := range storages {
for _, storage := range storages {
fill := [][]string{
{
stor.Properties.ObjectUUID,
stor.Properties.Name,
strconv.FormatInt(int64(stor.Properties.Capacity), 10),
strconv.FormatInt(int64(stor.Properties.ChangeTime.Hour()), 10),
stor.Properties.Status,
storage.Properties.ObjectUUID,
storage.Properties.Name,
strconv.FormatInt(int64(storage.Properties.Capacity), 10),
storage.Properties.ChangeTime.Local().Format(time.RFC3339),
storage.Properties.Status,
},
}
storageinfo = append(storageinfo, fill...)
Expand All @@ -61,7 +62,7 @@ var storageListCmd = &cobra.Command{
},
}

var storageRemoveCmd = &cobra.Command{
var storageRmCmd = &cobra.Command{
Use: "rm [flags] [ID]",
Aliases: []string{"remove"},
Short: "Remove storage",
Expand All @@ -72,12 +73,12 @@ var storageRemoveCmd = &cobra.Command{
ctx := context.Background()
err := storageOp.DeleteStorage(ctx, args[0])
if err != nil {
log.Fatalf("Removing Storage failed: %s", err)
log.Fatalf("Removing storage failed: %s", err)
}
},
}

func init() {
storageCmd.AddCommand(storageListCmd, storageRemoveCmd)
storageCmd.AddCommand(storageLsCmd, storageRmCmd)
rootCmd.AddCommand(storageCmd)
}
19 changes: 10 additions & 9 deletions cmd/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"io/ioutil"
"os"
"strconv"
"testing"
"time"

Expand All @@ -17,14 +16,16 @@ import (
"github.com/stretchr/testify/assert"
)

var changeTime, _ = time.Parse(time.RFC3339, "2020-07-02T16:15:00+02:00")

var mockStorage = gsclient.Storage{
Properties: gsclient.StorageProperties{
ObjectUUID: "xxx-xxx-xxx",
Name: "test",
Capacity: 10,
Status: "active",
ChangeTime: gsclient.GSTime{
Time: time.Now(),
Time: changeTime,
},
},
}
Expand Down Expand Up @@ -53,11 +54,11 @@ func Test_StorageListCmd(t *testing.T) {
headers := []string{"id", "name", "capacity", "changetime", "status"}
rows := [][]string{
{
mockStorage.Properties.ObjectUUID,
mockStorage.Properties.Name,
strconv.Itoa(mockStorage.Properties.Capacity),
strconv.FormatInt(int64(mockStorage.Properties.ChangeTime.Hour()), 10),
mockStorage.Properties.Status,
"xxx-xxx-xxx",
"test",
"10",
"2020-07-02T16:15:00+02:00",
"active",
},
}
render.Table(buf, headers, rows)
Expand Down Expand Up @@ -85,7 +86,7 @@ func Test_StorageListCmd(t *testing.T) {
rt, _ = runtime.NewTestRuntime()
rt.SetStorageOperator(mockClient)

cmd := storageListCmd.Run
cmd := storageLsCmd.Run
cmd(new(cobra.Command), []string{})

resetFlags()
Expand All @@ -107,7 +108,7 @@ func Test_StorageCmdDelete(t *testing.T) {
rt, _ = runtime.NewTestRuntime()
rt.SetStorageOperator(mockClient)

cmd := storageRemoveCmd.Run
cmd := storageRmCmd.Run
cmd(new(cobra.Command), []string{"rm", mockStorage.Properties.ObjectUUID})

w.Close()
Expand Down

0 comments on commit 9aa13c8

Please sign in to comment.