diff --git a/cmd/applications/printer.go b/cmd/applications/printer.go index b24d8613..1ee07def 100644 --- a/cmd/applications/printer.go +++ b/cmd/applications/printer.go @@ -4,6 +4,7 @@ import ( "strconv" "github.com/vultr/govultr/v3" + "github.com/vultr/vultr-cli/v3/cmd/printer" ) @@ -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() } diff --git a/cmd/backups/printer.go b/cmd/backups/printer.go index 3d123252..e5871a5b 100644 --- a/cmd/backups/printer.go +++ b/cmd/backups/printer.go @@ -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() } // ====================================== diff --git a/cmd/baremetal/printer.go b/cmd/baremetal/printer.go index 1a67c367..208353ef 100644 --- a/cmd/baremetal/printer.go +++ b/cmd/baremetal/printer.go @@ -4,6 +4,7 @@ import ( "strconv" "github.com/vultr/govultr/v3" + "github.com/vultr/vultr-cli/v3/cmd/printer" ) @@ -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() } // ====================================== diff --git a/cmd/billing/printer.go b/cmd/billing/printer.go index fdb9a030..bb34374e 100644 --- a/cmd/billing/printer.go +++ b/cmd/billing/printer.go @@ -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() } // ====================================== @@ -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() } // ====================================== @@ -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() } diff --git a/cmd/blockstorage/printer.go b/cmd/blockstorage/printer.go index 6ed35044..1ede8c00 100644 --- a/cmd/blockstorage/printer.go +++ b/cmd/blockstorage/printer.go @@ -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() } // ====================================== diff --git a/cmd/containerregistry/printer.go b/cmd/containerregistry/printer.go index 098fc94e..c6d2d148 100644 --- a/cmd/containerregistry/printer.go +++ b/cmd/containerregistry/printer.go @@ -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() } // ====================================== @@ -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() } // ====================================== @@ -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() } // ====================================== diff --git a/cmd/dns/printer.go b/cmd/dns/printer.go index 4dea4dfd..f3523895 100644 --- a/cmd/dns/printer.go +++ b/cmd/dns/printer.go @@ -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() } // ====================================== @@ -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() } // ====================================== diff --git a/cmd/firewall/printer.go b/cmd/firewall/printer.go index 9fc1a53b..2b98ce2e 100644 --- a/cmd/firewall/printer.go +++ b/cmd/firewall/printer.go @@ -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() } // ====================================== @@ -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() } // ====================================== diff --git a/cmd/instance/printer.go b/cmd/instance/printer.go index b232694d..8d3970d3 100644 --- a/cmd/instance/printer.go +++ b/cmd/instance/printer.go @@ -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() } // ====================================== @@ -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() } diff --git a/cmd/ip/printer.go b/cmd/ip/printer.go index f05a5272..b6d72c38 100644 --- a/cmd/ip/printer.go +++ b/cmd/ip/printer.go @@ -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() } // ====================================== @@ -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() } diff --git a/cmd/iso/printer.go b/cmd/iso/printer.go index ccb36b3a..e1d434d5 100644 --- a/cmd/iso/printer.go +++ b/cmd/iso/printer.go @@ -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() } // ====================================== @@ -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() } diff --git a/cmd/kubernetes/printer.go b/cmd/kubernetes/printer.go index 469f7572..7fdf6bd8 100644 --- a/cmd/kubernetes/printer.go +++ b/cmd/kubernetes/printer.go @@ -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() } // ====================================== @@ -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() } // ====================================== @@ -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() } // ====================================== diff --git a/cmd/loadbalancer/printer.go b/cmd/loadbalancer/printer.go index 73636361..679aaf82 100644 --- a/cmd/loadbalancer/printer.go +++ b/cmd/loadbalancer/printer.go @@ -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() } // ====================================== @@ -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() } // ====================================== @@ -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() } // ====================================== @@ -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() } // ====================================== diff --git a/cmd/objectstorage/printer.go b/cmd/objectstorage/printer.go index e1989167..864e1f7a 100644 --- a/cmd/objectstorage/printer.go +++ b/cmd/objectstorage/printer.go @@ -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() } // ====================================== @@ -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() } // ====================================== diff --git a/cmd/operatingsystems/printer.go b/cmd/operatingsystems/printer.go index 49dcd751..a3d3dc47 100644 --- a/cmd/operatingsystems/printer.go +++ b/cmd/operatingsystems/printer.go @@ -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() } diff --git a/cmd/plans/printer.go b/cmd/plans/printer.go index 51850266..2fb64454 100644 --- a/cmd/plans/printer.go +++ b/cmd/plans/printer.go @@ -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() } // ====================================== @@ -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() } diff --git a/cmd/printer/printer.go b/cmd/printer/printer.go index d135d8f3..ac0715b3 100644 --- a/cmd/printer/printer.go +++ b/cmd/printer/printer.go @@ -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) } @@ -113,21 +113,32 @@ 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 @@ -135,6 +146,11 @@ func NewPaging(total int, next, prev *string) *Paging { // 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{"======================================"}, diff --git a/cmd/regions/printer.go b/cmd/regions/printer.go index e426406b..4ce9da99 100644 --- a/cmd/regions/printer.go +++ b/cmd/regions/printer.go @@ -2,6 +2,7 @@ package regions import ( "github.com/vultr/govultr/v3" + "github.com/vultr/vultr-cli/v3/cmd/printer" ) @@ -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() } // ====================================== diff --git a/cmd/reservedip/printer.go b/cmd/reservedip/printer.go index 00e5056c..bb877518 100644 --- a/cmd/reservedip/printer.go +++ b/cmd/reservedip/printer.go @@ -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() } // ====================================== diff --git a/cmd/script/printer.go b/cmd/script/printer.go index db2d57e9..2a756ea6 100644 --- a/cmd/script/printer.go +++ b/cmd/script/printer.go @@ -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() } // ====================================== diff --git a/cmd/snapshot/printer.go b/cmd/snapshot/printer.go index 36cba506..c9e7fd9a 100644 --- a/cmd/snapshot/printer.go +++ b/cmd/snapshot/printer.go @@ -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() } // ====================================== diff --git a/cmd/sshkeys/printer.go b/cmd/sshkeys/printer.go index 3924d0bc..a475acf2 100644 --- a/cmd/sshkeys/printer.go +++ b/cmd/sshkeys/printer.go @@ -47,7 +47,7 @@ func (s *SSHKeysPrinter) Data() [][]string { // Paging ... func (s *SSHKeysPrinter) Paging() [][]string { - return printer.NewPaging(s.Meta.Total, &s.Meta.Links.Next, &s.Meta.Links.Prev).Compose() + return printer.NewPagingFromMeta(s.Meta).Compose() } // SSHKeyPrinter ... diff --git a/cmd/users/printer.go b/cmd/users/printer.go index 02ef8866..415c873b 100644 --- a/cmd/users/printer.go +++ b/cmd/users/printer.go @@ -51,7 +51,7 @@ func (u *UsersPrinter) Data() [][]string { // Paging ... func (u *UsersPrinter) Paging() [][]string { - return printer.NewPaging(u.Meta.Total, &u.Meta.Links.Next, &u.Meta.Links.Prev).Compose() + return printer.NewPagingFromMeta(u.Meta).Compose() } // ====================================== diff --git a/cmd/vpc/printer.go b/cmd/vpc/printer.go index 845596fa..91f8461b 100644 --- a/cmd/vpc/printer.go +++ b/cmd/vpc/printer.go @@ -4,6 +4,7 @@ import ( "strconv" "github.com/vultr/govultr/v3" + "github.com/vultr/vultr-cli/v3/cmd/printer" ) @@ -58,7 +59,7 @@ func (s *VPCsPrinter) Data() [][]string { // Paging ... func (s *VPCsPrinter) Paging() [][]string { - return printer.NewPaging(s.Meta.Total, &s.Meta.Links.Next, &s.Meta.Links.Prev).Compose() + return printer.NewPagingFromMeta(s.Meta).Compose() } // ====================================== diff --git a/cmd/vpc2/printer.go b/cmd/vpc2/printer.go index dc34f8f6..3d0aaf54 100644 --- a/cmd/vpc2/printer.go +++ b/cmd/vpc2/printer.go @@ -58,7 +58,7 @@ func (s *VPC2sPrinter) Data() [][]string { // Paging ... func (s *VPC2sPrinter) Paging() [][]string { - return printer.NewPaging(s.Meta.Total, &s.Meta.Links.Next, &s.Meta.Links.Prev).Compose() + return printer.NewPagingFromMeta(s.Meta).Compose() } // ====================================== @@ -160,5 +160,5 @@ func (s *VPC2NodesPrinter) Data() [][]string { // Paging ... func (s *VPC2NodesPrinter) Paging() [][]string { - return printer.NewPaging(s.Meta.Total, &s.Meta.Links.Next, &s.Meta.Links.Prev).Compose() + return printer.NewPagingFromMeta(s.Meta).Compose() }