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

Consolidate pagination metadata nil pointer checks #410

Merged
merged 4 commits into from
Feb 27, 2024
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
3 changes: 2 additions & 1 deletion cmd/applications/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strconv"

"github.com/vultr/govultr/v3"

"github.com/vultr/vultr-cli/v3/cmd/printer"
)

Expand Down Expand Up @@ -62,5 +63,5 @@ func (a *ApplicationsPrinter) Data() [][]string {

// Paging validates and forms the paging data for output
func (a *ApplicationsPrinter) Paging() [][]string {
return printer.NewPaging(a.Meta.Total, &a.Meta.Links.Next, &a.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(a.Meta).Compose()
}
2 changes: 1 addition & 1 deletion cmd/backups/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (b *BackupsPrinter) Data() [][]string {

// Paging ...
func (b *BackupsPrinter) Paging() [][]string {
return printer.NewPaging(b.Meta.Total, &b.Meta.Links.Next, &b.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(b.Meta).Compose()
}

// ======================================
Expand Down
3 changes: 2 additions & 1 deletion cmd/baremetal/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strconv"

"github.com/vultr/govultr/v3"

"github.com/vultr/vultr-cli/v3/cmd/printer"
)

Expand Down Expand Up @@ -67,7 +68,7 @@ func (b *BareMetalsPrinter) Data() [][]string {

// Paging ...
func (b *BareMetalsPrinter) Paging() [][]string {
return printer.NewPaging(b.Meta.Total, &b.Meta.Links.Next, &b.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(b.Meta).Compose()
}

// ======================================
Expand Down
6 changes: 3 additions & 3 deletions cmd/billing/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (b *BillingHistoryPrinter) Data() [][]string {

// Paging ...
func (b *BillingHistoryPrinter) Paging() [][]string {
return printer.NewPaging(b.Meta.Total, &b.Meta.Links.Next, &b.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(b.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -113,7 +113,7 @@ func (b *BillingInvoicesPrinter) Data() [][]string {

// Paging ...
func (b *BillingInvoicesPrinter) Paging() [][]string {
return printer.NewPaging(b.Meta.Total, &b.Meta.Links.Next, &b.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(b.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -217,5 +217,5 @@ func (b *BillingInvoiceItemsPrinter) Data() [][]string {

// Paging ...
func (b *BillingInvoiceItemsPrinter) Paging() [][]string {
return printer.NewPaging(b.Meta.Total, &b.Meta.Links.Next, &b.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(b.Meta).Compose()
}
2 changes: 1 addition & 1 deletion cmd/blockstorage/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (b *BlockStoragesPrinter) Data() [][]string {

// Paging ...
func (b *BlockStoragesPrinter) Paging() [][]string {
return printer.NewPaging(b.Meta.Total, &b.Meta.Links.Next, &b.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(b.Meta).Compose()
}

// ======================================
Expand Down
6 changes: 3 additions & 3 deletions cmd/containerregistry/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (c *ContainerRegistriesPrinter) Data() [][]string {

// Paging ...
func (c *ContainerRegistriesPrinter) Paging() [][]string {
return printer.NewPaging(c.Meta.Total, &c.Meta.Links.Next, &c.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(c.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -234,7 +234,7 @@ func (c *ContainerRegistryRegionsPrinter) Data() [][]string {

// Paging ...
func (c *ContainerRegistryRegionsPrinter) Paging() [][]string {
return printer.NewPaging(c.Meta.Total, &c.Meta.Links.Next, &c.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(c.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -340,7 +340,7 @@ func (c *ContainerRegistryRepositoriesPrinter) Data() [][]string {

// Paging ...
func (c *ContainerRegistryRepositoriesPrinter) Paging() [][]string {
return printer.NewPaging(c.Meta.Total, &c.Meta.Links.Next, &c.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(c.Meta).Compose()
}

// ======================================
Expand Down
4 changes: 2 additions & 2 deletions cmd/dns/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (d *DNSRecordsPrinter) Data() [][]string {

// Paging ...
func (d *DNSRecordsPrinter) Paging() [][]string {
return printer.NewPaging(d.Meta.Total, &d.Meta.Links.Next, &d.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(d.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -154,7 +154,7 @@ func (d *DNSDomainsPrinter) Data() [][]string {

// Paging ...
func (d *DNSDomainsPrinter) Paging() [][]string {
return printer.NewPaging(d.Meta.Total, &d.Meta.Links.Next, &d.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(d.Meta).Compose()
}

// ======================================
Expand Down
4 changes: 2 additions & 2 deletions cmd/firewall/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (f *FirewallGroupsPrinter) Data() [][]string {

// Paging ...
func (f *FirewallGroupsPrinter) Paging() [][]string {
return printer.NewPaging(f.Meta.Total, &f.Meta.Links.Next, &f.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(f.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -169,7 +169,7 @@ func (f *FirewallRulesPrinter) Data() [][]string {

// Paging ...
func (f *FirewallRulesPrinter) Paging() [][]string {
return printer.NewPaging(f.Meta.Total, &f.Meta.Links.Next, &f.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(f.Meta).Compose()
}

// ======================================
Expand Down
4 changes: 2 additions & 2 deletions cmd/instance/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (i *InstancesPrinter) Data() [][]string {

// Paging ...
func (i *InstancesPrinter) Paging() [][]string {
return printer.NewPaging(i.Meta.Total, &i.Meta.Links.Next, &i.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(i.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -509,5 +509,5 @@ func (v *VPC2sPrinter) Data() [][]string {

// Paging ...
func (v *VPC2sPrinter) Paging() [][]string {
return printer.NewPaging(v.Meta.Total, &v.Meta.Links.Next, &v.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(v.Meta).Compose()
}
4 changes: 2 additions & 2 deletions cmd/ip/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (i *IPv4sPrinter) Data() [][]string {

// Paging ...
func (i *IPv4sPrinter) Paging() [][]string {
return printer.NewPaging(i.Meta.Total, &i.Meta.Links.Next, &i.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(i.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -99,5 +99,5 @@ func (i *IPv6sPrinter) Data() [][]string {

// Paging ...
func (i *IPv6sPrinter) Paging() [][]string {
return printer.NewPaging(i.Meta.Total, &i.Meta.Links.Next, &i.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(i.Meta).Compose()
}
4 changes: 2 additions & 2 deletions cmd/iso/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (i *ISOsPrinter) Data() [][]string {

// Paging ...
func (i *ISOsPrinter) Paging() [][]string {
return printer.NewPaging(i.Meta.Total, &i.Meta.Links.Next, &i.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(i.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -154,5 +154,5 @@ func (i *PublicISOsPrinter) Data() [][]string {

// Paging ...
func (i *PublicISOsPrinter) Paging() [][]string {
return printer.NewPaging(i.Meta.Total, &i.Meta.Links.Next, &i.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(i.Meta).Compose()
}
6 changes: 3 additions & 3 deletions cmd/kubernetes/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *ClustersSummaryPrinter) Data() [][]string {

// Paging ...
func (c *ClustersSummaryPrinter) Paging() [][]string {
return printer.NewPaging(c.Meta.Total, &c.Meta.Links.Next, &c.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(c.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -161,7 +161,7 @@ func (c *ClustersPrinter) Data() [][]string {

// Paging ...
func (c *ClustersPrinter) Paging() [][]string {
return printer.NewPaging(c.Meta.Total, &c.Meta.Links.Next, &c.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(c.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -321,7 +321,7 @@ func (n *NodePoolsPrinter) Data() [][]string {

// Paging ...
func (n *NodePoolsPrinter) Paging() [][]string {
return printer.NewPaging(n.Meta.Total, &n.Meta.Links.Next, &n.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(n.Meta).Compose()
}

// ======================================
Expand Down
8 changes: 4 additions & 4 deletions cmd/loadbalancer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (l *LBsPrinter) Data() [][]string {

// Paging ...
func (l *LBsPrinter) Paging() [][]string {
return printer.NewPaging(l.Meta.Total, &l.Meta.Links.Next, &l.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(l.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -295,7 +295,7 @@ func (l *LBsSummaryPrinter) Data() [][]string {

// Paging ...
func (l *LBsSummaryPrinter) Paging() [][]string {
return printer.NewPaging(l.Meta.Total, &l.Meta.Links.Next, &l.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(l.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -349,7 +349,7 @@ func (l *LBRulesPrinter) Data() [][]string {

// Paging ...
func (l *LBRulesPrinter) Paging() [][]string {
return printer.NewPaging(l.Meta.Total, &l.Meta.Links.Next, &l.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(l.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -445,7 +445,7 @@ func (f *FWRulesPrinter) Data() [][]string {

// Paging ...
func (f *FWRulesPrinter) Paging() [][]string {
return printer.NewPaging(f.Meta.Total, &f.Meta.Links.Next, &f.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(f.Meta).Compose()
}

// ======================================
Expand Down
4 changes: 2 additions & 2 deletions cmd/objectstorage/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (o *ObjectStoragesPrinter) Data() [][]string {

// Paging ...
func (o *ObjectStoragesPrinter) Paging() [][]string {
return printer.NewPaging(o.Meta.Total, &o.Meta.Links.Next, &o.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(o.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -168,7 +168,7 @@ func (o *ObjectStorageClustersPrinter) Data() [][]string {

// Paging ...
func (o *ObjectStorageClustersPrinter) Paging() [][]string {
return printer.NewPaging(o.Meta.Total, &o.Meta.Links.Next, &o.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(o.Meta).Compose()
}

// ======================================
Expand Down
2 changes: 1 addition & 1 deletion cmd/operatingsystems/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ func (o *OSPrinter) Data() [][]string {

// Paging validates and forms the paging data for output
func (o *OSPrinter) Paging() [][]string {
return printer.NewPaging(o.Meta.Total, &o.Meta.Links.Next, &o.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(o.Meta).Compose()
}
4 changes: 2 additions & 2 deletions cmd/plans/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (p *PlansPrinter) Data() [][]string {

// Paging validates and forms the paging data for output
func (p *PlansPrinter) Paging() [][]string {
return printer.NewPaging(p.Meta.Total, &p.Meta.Links.Next, &p.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(p.Meta).Compose()
}

// ======================================
Expand Down Expand Up @@ -139,5 +139,5 @@ func (m *MetalPlansPrinter) Data() [][]string {

// Paging validates and forms the paging data for output
func (m *MetalPlansPrinter) Paging() [][]string {
return printer.NewPaging(m.Meta.Total, &m.Meta.Links.Next, &m.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(m.Meta).Compose()
}
42 changes: 29 additions & 13 deletions cmd/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (o *Output) Display(r ResourceOutput, err error) {
defer o.flush()

if err != nil {
//todo move this so it can follow the flow of the other printers and support json/yaml
// todo move this so it can follow the flow of the other printers and support json/yaml
Error(err)
}

Expand Down Expand Up @@ -113,28 +113,44 @@ type Paging struct {
CursorPrev string
}

// NewPaging validates and intializes the paging data
func NewPaging(total int, next, prev *string) *Paging {
p := new(Paging)
p.Total = total
// NewPagingFromMeta validates and initializes the paging data, from a govultr.Meta struct.
func NewPagingFromMeta(m *govultr.Meta) *Paging {
if m == nil {
// If no metadata, then no paging to show.
return nil
}
if m.Links == nil {
return NewPaging(m.Total, "", "")
}
return NewPaging(m.Total, m.Links.Next, m.Links.Prev)
}

if next != nil {
p.CursorNext = *next
} else {
p.CursorNext = emptyPlaceholder
// NewPaging validates and initializes the paging data.
func NewPaging(total int, next, prev string) *Paging {
p := &Paging{
Total: total,
CursorNext: emptyPlaceholder,
CursorPrev: emptyPlaceholder,
}

if prev != nil {
p.CursorPrev = *prev
} else {
p.CursorPrev = emptyPlaceholder
if next != "" {
p.CursorNext = next
}

if prev != "" {
p.CursorPrev = prev
}

return p
}

// Compose returns the paging data for output
func (p *Paging) Compose() [][]string {
if p == nil {
// If no paging information, don't show anything.
return nil
}

var display [][]string
display = append(display,
[]string{"======================================"},
Expand Down
3 changes: 2 additions & 1 deletion cmd/regions/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package regions

import (
"github.com/vultr/govultr/v3"

"github.com/vultr/vultr-cli/v3/cmd/printer"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ func (r *RegionsPrinter) Data() [][]string {

// Paging validates and forms the paging data for output
func (r *RegionsPrinter) Paging() [][]string {
return printer.NewPaging(r.Meta.Total, &r.Meta.Links.Next, &r.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(r.Meta).Compose()
}

// ======================================
Expand Down
2 changes: 1 addition & 1 deletion cmd/reservedip/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *ReservedIPsPrinter) Data() [][]string {

// Paging ...
func (r *ReservedIPsPrinter) Paging() [][]string {
return printer.NewPaging(r.Meta.Total, &r.Meta.Links.Next, &r.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(r.Meta).Compose()
}

// ======================================
Expand Down
2 changes: 1 addition & 1 deletion cmd/script/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *ScriptsPrinter) Data() [][]string {

// Paging ...
func (s *ScriptsPrinter) Paging() [][]string {
return printer.NewPaging(s.Meta.Total, &s.Meta.Links.Next, &s.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(s.Meta).Compose()
}

// ======================================
Expand Down
2 changes: 1 addition & 1 deletion cmd/snapshot/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *SnapshotsPrinter) Data() [][]string {

// Paging ...
func (s *SnapshotsPrinter) Paging() [][]string {
return printer.NewPaging(s.Meta.Total, &s.Meta.Links.Next, &s.Meta.Links.Prev).Compose()
return printer.NewPagingFromMeta(s.Meta).Compose()
}

// ======================================
Expand Down
Loading
Loading