Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an age column to the cli, closes #417 #420

Merged
merged 4 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/cmd/certificate/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package certificate
import (
"context"
"strings"
"time"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/hcapi2"
Expand All @@ -16,7 +17,7 @@ import (

var listCmd = base.ListCmd{
ResourceNamePlural: "certificates",
DefaultColumns: []string{"id", "name", "type", "domain_names", "not_valid_after"},
DefaultColumns: []string{"id", "name", "type", "domain_names", "not_valid_after", "age"},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.CertificateListOpts{ListOpts: listOpts}
Expand Down Expand Up @@ -70,6 +71,10 @@ var listCmd = base.ListCmd{
AddFieldFn("created", output.FieldFn(func(obj interface{}) string {
cert := obj.(*hcloud.Certificate)
return util.Datetime(cert.Created)
})).
AddFieldFn("age", output.FieldFn(func(obj interface{}) string {
cert := obj.(*hcloud.Certificate)
return util.Age(cert.Created, time.Now())
}))
},

Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/floatingip/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/spf13/cobra"

Expand All @@ -19,7 +20,7 @@ import (

var listCmd = base.ListCmd{
ResourceNamePlural: "Floating IPs",
DefaultColumns: []string{"id", "type", "name", "description", "ip", "home", "server", "dns"},
DefaultColumns: []string{"id", "type", "name", "description", "ip", "home", "server", "dns", "age"},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.FloatingIPListOpts{ListOpts: listOpts}
Expand Down Expand Up @@ -85,6 +86,10 @@ var listCmd = base.ListCmd{
AddFieldFn("created", output.FieldFn(func(obj interface{}) string {
floatingIP := obj.(*hcloud.FloatingIP)
return util.Datetime(floatingIP.Created)
})).
AddFieldFn("age", output.FieldFn(func(obj interface{}) string {
floatingIP := obj.(*hcloud.FloatingIP)
return util.Age(floatingIP.Created, time.Now())
}))
},

Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/image/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
Expand Down Expand Up @@ -102,6 +103,10 @@ var listCmd = base.ListCmd{
image := obj.(*hcloud.Image)
return util.Datetime(image.Created)
})).
AddFieldFn("age", output.FieldFn(func(obj interface{}) string {
image := obj.(*hcloud.Image)
return util.Age(image.Created, time.Now())
})).
AddFieldFn("deprecated", output.FieldFn(func(obj interface{}) string {
image := obj.(*hcloud.Image)
if image.Deprecated.IsZero() {
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/loadbalancer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package loadbalancer
import (
"context"
"strings"
"time"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/output"
Expand All @@ -17,7 +18,7 @@ import (
var ListCmd = base.ListCmd{
ResourceNamePlural: "Load Balancer",

DefaultColumns: []string{"id", "name", "ipv4", "ipv6", "type", "location", "network_zone"},
DefaultColumns: []string{"id", "name", "ipv4", "ipv6", "type", "location", "network_zone", "age"},
Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.LoadBalancerListOpts{ListOpts: listOpts}
if len(sorts) > 0 {
Expand Down Expand Up @@ -70,6 +71,10 @@ var ListCmd = base.ListCmd{
AddFieldFn("created", output.FieldFn(func(obj interface{}) string {
loadBalancer := obj.(*hcloud.LoadBalancer)
return util.Datetime(loadBalancer.Created)
})).
AddFieldFn("age", output.FieldFn(func(obj interface{}) string {
loadBalancer := obj.(*hcloud.LoadBalancer)
return util.Age(loadBalancer.Created, time.Now())
}))
},

Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/network/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/output"
Expand All @@ -16,7 +17,7 @@ import (

var ListCmd = base.ListCmd{
ResourceNamePlural: "networks",
DefaultColumns: []string{"id", "name", "ip_range", "servers"},
DefaultColumns: []string{"id", "name", "ip_range", "servers", "age"},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.NetworkListOpts{ListOpts: listOpts}
Expand Down Expand Up @@ -62,6 +63,10 @@ var ListCmd = base.ListCmd{
AddFieldFn("created", output.FieldFn(func(obj interface{}) string {
network := obj.(*hcloud.Network)
return util.Datetime(network.Created)
})).
AddFieldFn("age", output.FieldFn(func(obj interface{}) string {
network := obj.(*hcloud.Network)
return util.Age(network.Created, time.Now())
}))
},

Expand Down
6 changes: 4 additions & 2 deletions internal/cmd/network/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/hetznercloud/cli/internal/cmd/network"
Expand Down Expand Up @@ -36,14 +37,15 @@ func TestList(t *testing.T) {
Name: "test-net",
IPRange: &net.IPNet{IP: net.ParseIP("192.0.2.1"), Mask: net.CIDRMask(24, 32)},
Servers: []*hcloud.Server{{ID: 3421}},
Created: time.Now().Add(-10 * time.Second),
},
},
nil)

out, err := fx.Run(cmd, []string{"--selector", "foo=bar"})

expOut := `ID NAME IP RANGE SERVERS
123 test-net 192.0.2.1/24 1 server
expOut := `ID NAME IP RANGE SERVERS AGE
123 test-net 192.0.2.1/24 1 server 10s
`

assert.NoError(t, err)
Expand Down
12 changes: 11 additions & 1 deletion internal/cmd/placementgroup/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package placementgroup
import (
"context"
"fmt"
"time"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/output"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/hcloud-go/hcloud/schema"
Expand All @@ -14,7 +16,7 @@ import (

var ListCmd = base.ListCmd{
ResourceNamePlural: "placement groups",
DefaultColumns: []string{"id", "name", "servers", "type"},
DefaultColumns: []string{"id", "name", "servers", "type", "age"},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.PlacementGroupListOpts{ListOpts: listOpts}
Expand All @@ -40,6 +42,14 @@ var ListCmd = base.ListCmd{
return fmt.Sprintf("%d server", count)
}
return fmt.Sprintf("%d servers", count)
})).
AddFieldFn("created", output.FieldFn(func(obj interface{}) string {
placementGroup := obj.(*hcloud.PlacementGroup)
return util.Datetime(placementGroup.Created)
})).
AddFieldFn("age", output.FieldFn(func(obj interface{}) string {
placementGroup := obj.(*hcloud.PlacementGroup)
return util.Age(placementGroup.Created, time.Now())
}))
},

Expand Down
6 changes: 4 additions & 2 deletions internal/cmd/placementgroup/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package placementgroup_test
import (
"context"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/hetznercloud/cli/internal/cmd/placementgroup"
Expand Down Expand Up @@ -39,13 +40,14 @@ func TestList(t *testing.T) {
Labels: map[string]string{"key": "value"},
Servers: []int{4711, 4712},
Type: hcloud.PlacementGroupTypeSpread,
Created: time.Now().Add(-10 * time.Second),
},
}, nil)

out, err := fx.Run(cmd, []string{"--selector", "foo=bar"})

expOut := `ID NAME SERVERS TYPE
897 my Placement Group 2 servers spread
expOut := `ID NAME SERVERS TYPE AGE
897 my Placement Group 2 servers spread 10s
`

assert.NoError(t, err)
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/primaryip/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/hetznercloud/hcloud-go/hcloud/schema"

Expand All @@ -17,7 +18,7 @@ import (

var listCmd = base.ListCmd{
ResourceNamePlural: "Primary IPs",
DefaultColumns: []string{"id", "type", "name", "ip", "assignee", "dns", "auto_delete"},
DefaultColumns: []string{"id", "type", "name", "ip", "assignee", "dns", "auto_delete", "age"},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.PrimaryIPListOpts{ListOpts: listOpts}
Expand Down Expand Up @@ -79,6 +80,10 @@ var listCmd = base.ListCmd{
AddFieldFn("created", output.FieldFn(func(obj interface{}) string {
primaryIP := obj.(*hcloud.PrimaryIP)
return util.Datetime(primaryIP.Created)
})).
AddFieldFn("age", output.FieldFn(func(obj interface{}) string {
primaryIP := obj.(*hcloud.PrimaryIP)
return util.Age(primaryIP.Created, time.Now())
}))
},

Expand Down
6 changes: 4 additions & 2 deletions internal/cmd/primaryip/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/hetznercloud/cli/internal/testutil"
Expand Down Expand Up @@ -36,14 +37,15 @@ func TestList(t *testing.T) {
AutoDelete: true,
Type: hcloud.PrimaryIPTypeIPv4,
IP: net.ParseIP("127.0.0.1"),
Created: time.Now().Add(-10 * time.Second),
},
},
nil)

out, err := fx.Run(cmd, []string{"--selector", "foo=bar"})

expOut := `ID TYPE NAME IP ASSIGNEE DNS AUTO DELETE
123 ipv4 test-net 127.0.0.1 - - yes
expOut := `ID TYPE NAME IP ASSIGNEE DNS AUTO DELETE AGE
123 ipv4 test-net 127.0.0.1 - - yes 10s
`

assert.NoError(t, err)
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/server/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strconv"
"strings"
"time"

humanize "github.com/dustin/go-humanize"
"github.com/hetznercloud/cli/internal/cmd/base"
Expand All @@ -20,7 +21,7 @@ import (
var ListCmd = base.ListCmd{
ResourceNamePlural: "servers",

DefaultColumns: []string{"id", "name", "status", "ipv4", "ipv6", "private_net", "datacenter"},
DefaultColumns: []string{"id", "name", "status", "ipv4", "ipv6", "private_net", "datacenter", "age"},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.ServerListOpts{ListOpts: listOpts}
Expand Down Expand Up @@ -113,6 +114,10 @@ var ListCmd = base.ListCmd{
server := obj.(*hcloud.Server)
return util.Datetime(server.Created)
})).
AddFieldFn("age", output.FieldFn(func(obj interface{}) string {
server := obj.(*hcloud.Server)
return util.Age(server.Created, time.Now())
})).
AddFieldFn("placement_group", output.FieldFn(func(obj interface{}) string {
server := obj.(*hcloud.Server)
if server.PlacementGroup == nil {
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/sshkey/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sshkey

import (
"context"
"time"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/output"
Expand All @@ -14,7 +15,7 @@ import (

var listCmd = base.ListCmd{
ResourceNamePlural: "ssh keys",
DefaultColumns: []string{"id", "name", "fingerprint"},
DefaultColumns: []string{"id", "name", "fingerprint", "age"},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.SSHKeyListOpts{ListOpts: listOpts}
Expand All @@ -40,6 +41,10 @@ var listCmd = base.ListCmd{
AddFieldFn("created", output.FieldFn(func(obj interface{}) string {
sshKey := obj.(*hcloud.SSHKey)
return util.Datetime(sshKey.Created)
})).
AddFieldFn("age", output.FieldFn(func(obj interface{}) string {
sshKey := obj.(*hcloud.SSHKey)
return util.Age(sshKey.Created, time.Now())
}))
},

Expand Down
23 changes: 23 additions & 0 deletions internal/cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ func Datetime(t time.Time) string {
return t.Local().Format(time.UnixDate)
}

func Age(t, currentTime time.Time) string {
diff := currentTime.Sub(t)

if int(diff.Hours()) >= 24 {
days := int(diff.Hours()) / 24
return fmt.Sprintf("%dd", days)
}

if int(diff.Hours()) > 0 {
return fmt.Sprintf("%dh", int(diff.Hours()))
}

if int(diff.Minutes()) > 0 {
return fmt.Sprintf("%dm", int(diff.Minutes()))
}

if int(diff.Seconds()) > 0 {
return fmt.Sprintf("%ds", int(diff.Seconds()))
}

return "just now"
}

func ChainRunE(fns ...func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
for _, fn := range fns {
Expand Down
Loading