Skip to content

Commit

Permalink
refactor: export list commands and only pass flags to ListCmd.Fetch (#…
Browse files Browse the repository at this point in the history
…571)

In preparation for #545
  • Loading branch information
phm07 authored Oct 19, 2023
1 parent 3ce048c commit 2473380
Show file tree
Hide file tree
Showing 28 changed files with 65 additions and 70 deletions.
5 changes: 3 additions & 2 deletions internal/cmd/base/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package base
import (
"context"
"fmt"
"github.com/spf13/pflag"

"github.com/hetznercloud/cli/internal/cmd/output"
"github.com/hetznercloud/cli/internal/cmd/util"
Expand All @@ -16,7 +17,7 @@ import (
type ListCmd struct {
ResourceNamePlural string // e.g. "servers"
DefaultColumns []string
Fetch func(context.Context, hcapi2.Client, *cobra.Command, hcloud.ListOpts, []string) ([]interface{}, error)
Fetch func(context.Context, hcapi2.Client, *pflag.FlagSet, hcloud.ListOpts, []string) ([]interface{}, error)
AdditionalFlags func(*cobra.Command)
OutputTable func(client hcapi2.Client) *output.Table
JSONSchema func([]interface{}) interface{}
Expand Down Expand Up @@ -62,7 +63,7 @@ func (lc *ListCmd) Run(ctx context.Context, client hcapi2.Client, cmd *cobra.Com
}
sorts, _ := cmd.Flags().GetStringSlice("sort")

resources, err := lc.Fetch(ctx, client, cmd, listOpts, sorts)
resources, err := lc.Fetch(ctx, client, cmd.Flags(), listOpts, sorts)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/certificate/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
DisableFlagsInUseLine: true,
}
cmd.AddCommand(
listCmd.CobraCommand(cli.Context, client, cli),
ListCmd.CobraCommand(cli.Context, client, cli),
newCreateCommand(cli),
updateCmd.CobraCommand(cli.Context, client, cli),
labelCmds.AddCobraCommand(cli.Context, client, cli),
Expand Down
9 changes: 4 additions & 5 deletions internal/cmd/certificate/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ package certificate

import (
"context"
"github.com/spf13/pflag"
"strings"
"time"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/spf13/cobra"

"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/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

var listCmd = base.ListCmd{
var ListCmd = base.ListCmd{
ResourceNamePlural: "certificates",
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) {
Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.CertificateListOpts{ListOpts: listOpts}
if len(sorts) > 0 {
opts.Sort = sorts
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/datacenter/datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
DisableFlagsInUseLine: true,
}
cmd.AddCommand(
listCmd.CobraCommand(cli.Context, client, cli),
ListCmd.CobraCommand(cli.Context, client, cli),
describeCmd.CobraCommand(cli.Context, client, cli),
)
return cmd
Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/datacenter/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package datacenter

import (
"context"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/output"
Expand All @@ -13,11 +12,11 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

var listCmd = base.ListCmd{
var ListCmd = base.ListCmd{
ResourceNamePlural: "datacenters",
DefaultColumns: []string{"id", "name", "description", "location"},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.DatacenterListOpts{ListOpts: listOpts}
if len(sorts) > 0 {
opts.Sort = sorts
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
DisableFlagsInUseLine: true,
}
cmd.AddCommand(
listCmd.CobraCommand(cli.Context, client, cli),
ListCmd.CobraCommand(cli.Context, client, cli),
describeCmd.CobraCommand(cli.Context, client, cli),
newCreateCommand(cli),
updateCmd.CobraCommand(cli.Context, client, cli),
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/firewall/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package firewall
import (
"context"
"fmt"
"github.com/spf13/pflag"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/output"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
"github.com/spf13/cobra"
)

var listCmd = base.ListCmd{
var ListCmd = base.ListCmd{
ResourceNamePlural: "Firewalls",
DefaultColumns: []string{"id", "name", "rules_count", "applied_to_count"},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.FirewallListOpts{ListOpts: listOpts}
if len(sorts) > 0 {
opts.Sort = sorts
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/floatingip.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
}
cmd.AddCommand(
updateCmd.CobraCommand(cli.Context, client, cli),
listCmd.CobraCommand(cli.Context, client, cli),
ListCmd.CobraCommand(cli.Context, client, cli),
CreateCommand.CobraCommand(cli.Context, client, cli, cli),
describeCmd.CobraCommand(cli.Context, client, cli),
AssignCommand.CobraCommand(cli.Context, client, cli, cli),
Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/floatingip/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package floatingip
import (
"context"
"fmt"
"github.com/spf13/pflag"
"strings"
"time"

"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/hcapi2"

Expand All @@ -18,11 +17,11 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var listCmd = base.ListCmd{
var ListCmd = base.ListCmd{
ResourceNamePlural: "Floating IPs",
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) {
Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.FloatingIPListOpts{ListOpts: listOpts}
if len(sorts) > 0 {
opts.Sort = sorts
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
DisableFlagsInUseLine: true,
}
cmd.AddCommand(
listCmd.CobraCommand(cli.Context, client, cli),
ListCmd.CobraCommand(cli.Context, client, cli),
deleteCmd.CobraCommand(cli.Context, client, cli, cli),
describeCmd.CobraCommand(cli.Context, client, cli),
updateCmd.CobraCommand(cli.Context, client, cli),
Expand Down
9 changes: 5 additions & 4 deletions internal/cmd/image/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package image
import (
"context"
"fmt"
"github.com/spf13/pflag"
"strings"
"time"

Expand All @@ -19,7 +20,7 @@ import (
"github.com/spf13/cobra"
)

var listCmd = base.ListCmd{
var ListCmd = base.ListCmd{
ResourceNamePlural: "images",
DefaultColumns: []string{"id", "type", "name", "description", "architecture", "image_size", "disk_size", "created", "deprecated"},
AdditionalFlags: func(cmd *cobra.Command) {
Expand All @@ -29,15 +30,15 @@ var listCmd = base.ListCmd{
cmd.Flags().StringSliceP("architecture", "a", []string{}, "Only show images of given architecture: x86|arm")
cmd.RegisterFlagCompletionFunc("architecture", cmpl.SuggestCandidates(string(hcloud.ArchitectureX86), string(hcloud.ArchitectureARM)))
},
Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
Fetch: func(ctx context.Context, client hcapi2.Client, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.ImageListOpts{ListOpts: listOpts, IncludeDeprecated: true}

imageType, _ := cmd.Flags().GetString("type")
imageType, _ := flags.GetString("type")
if len(imageType) > 0 {
opts.Type = []hcloud.ImageType{hcloud.ImageType(imageType)}
}

architecture, _ := cmd.Flags().GetStringSlice("architecture")
architecture, _ := flags.GetStringSlice("architecture")
if len(architecture) > 0 {
for _, arch := range architecture {
opts.Architecture = append(opts.Architecture, hcloud.Architecture(arch))
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/iso/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
DisableFlagsInUseLine: true,
}
cmd.AddCommand(
listCmd.CobraCommand(cli.Context, client, cli),
ListCmd.CobraCommand(cli.Context, client, cli),
DescribeCmd.CobraCommand(cli.Context, client, cli),
)
return cmd
Expand Down
9 changes: 5 additions & 4 deletions internal/cmd/iso/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iso

import (
"context"
"github.com/spf13/pflag"

"github.com/hetznercloud/cli/internal/cmd/cmpl"

Expand All @@ -14,7 +15,7 @@ import (
"github.com/spf13/cobra"
)

var listCmd = base.ListCmd{
var ListCmd = base.ListCmd{
ResourceNamePlural: "isos",
DefaultColumns: []string{"id", "name", "description", "type", "architecture"},
AdditionalFlags: func(cmd *cobra.Command) {
Expand All @@ -24,17 +25,17 @@ var listCmd = base.ListCmd{
cmd.Flags().Bool("include-architecture-wildcard", false, "Include ISOs with unknown architecture, only required if you want so show custom ISOs and still filter for architecture.")
},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
Fetch: func(ctx context.Context, client hcapi2.Client, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.ISOListOpts{ListOpts: listOpts}

architecture, _ := cmd.Flags().GetStringSlice("architecture")
architecture, _ := flags.GetStringSlice("architecture")
if len(architecture) > 0 {
for _, arch := range architecture {
opts.Architecture = append(opts.Architecture, hcloud.Architecture(arch))
}
}

includeArchitectureWildcard, _ := cmd.Flags().GetBool("include-architecture-wildcard")
includeArchitectureWildcard, _ := flags.GetBool("include-architecture-wildcard")
if includeArchitectureWildcard {
opts.IncludeWildcardArchitecture = includeArchitectureWildcard
}
Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/loadbalancer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ package loadbalancer

import (
"context"
"github.com/spf13/pflag"
"strings"
"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/v2/hcloud/schema"
"github.com/spf13/cobra"

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

var ListCmd = base.ListCmd{
ResourceNamePlural: "Load Balancer",

DefaultColumns: []string{"id", "name", "health", "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) {
Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.LoadBalancerListOpts{ListOpts: listOpts}
if len(sorts) > 0 {
opts.Sort = sorts
Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/loadbalancertype/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ package loadbalancertype

import (
"context"
"github.com/spf13/pflag"

"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/v2/hcloud/schema"
"github.com/spf13/cobra"

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

var ListCmd = base.ListCmd{
ResourceNamePlural: "Load Balancer Types",

DefaultColumns: []string{"id", "name", "description", "max_services", "max_connections", "max_targets"},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.LoadBalancerTypeListOpts{ListOpts: listOpts}
if len(sorts) > 0 {
opts.Sort = sorts
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/location/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package location

import (
"context"
"github.com/spf13/pflag"

"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/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
"github.com/spf13/cobra"
)

var listCmd = base.ListCmd{
var ListCmd = base.ListCmd{
ResourceNamePlural: "locations",
DefaultColumns: []string{"id", "name", "description", "network_zone", "country", "city"},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.LocationListOpts{ListOpts: listOpts}
if len(sorts) > 0 {
opts.Sort = sorts
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/location/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
DisableFlagsInUseLine: true,
}
cmd.AddCommand(
listCmd.CobraCommand(cli.Context, client, cli),
ListCmd.CobraCommand(cli.Context, client, cli),
DescribeCmd.CobraCommand(cli.Context, client, cli),
)
return cmd
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/network/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package network
import (
"context"
"fmt"
"github.com/spf13/pflag"
"strings"
"time"

Expand All @@ -12,14 +13,13 @@ import (
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
"github.com/spf13/cobra"
)

var ListCmd = base.ListCmd{
ResourceNamePlural: "networks",
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) {
Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.NetworkListOpts{ListOpts: listOpts}
if len(sorts) > 0 {
opts.Sort = sorts
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/placementgroup/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package placementgroup
import (
"context"
"fmt"
"github.com/spf13/pflag"
"time"

"github.com/hetznercloud/cli/internal/cmd/base"
Expand All @@ -11,14 +12,13 @@ import (
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
"github.com/spf13/cobra"
)

var ListCmd = base.ListCmd{
ResourceNamePlural: "placement groups",
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) {
Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.PlacementGroupListOpts{ListOpts: listOpts}
if len(sorts) > 0 {
opts.Sort = sorts
Expand Down
Loading

0 comments on commit 2473380

Please sign in to comment.