Skip to content

Commit

Permalink
feat: add "all list" command to list all available resources
Browse files Browse the repository at this point in the history
As described in #545
  • Loading branch information
phm07 committed Oct 24, 2023
1 parent 93bed7e commit a0dfe66
Show file tree
Hide file tree
Showing 21 changed files with 500 additions and 2 deletions.
2 changes: 2 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/all"
"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/cmd/completion"
"github.com/hetznercloud/cli/internal/cmd/context"
Expand Down Expand Up @@ -39,6 +40,7 @@ func NewRootCommand(state *state.State, client hcapi2.Client) *cobra.Command {
DisableFlagsInUseLine: true,
}
cmd.AddCommand(
all.NewCommand(state, client),
floatingip.NewCommand(state, client),
image.NewCommand(state, client),
server.NewCommand(state, client),
Expand Down
22 changes: 22 additions & 0 deletions internal/cmd/all/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package all

import (
"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
)

func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "all",
Short: "Commands that apply to all resources",
Args: cobra.NoArgs,
TraverseChildren: true,
DisableFlagsInUseLine: true,
}
cmd.AddCommand(
listCmd.CobraCommand(cli.Context, client, cli, cli),
)
return cmd
}
157 changes: 157 additions & 0 deletions internal/cmd/all/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package all

import (
"context"
"encoding/json"
"fmt"
"strings"

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

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/cmd/firewall"
"github.com/hetznercloud/cli/internal/cmd/floatingip"
"github.com/hetznercloud/cli/internal/cmd/image"
"github.com/hetznercloud/cli/internal/cmd/iso"
"github.com/hetznercloud/cli/internal/cmd/loadbalancer"
"github.com/hetznercloud/cli/internal/cmd/network"
"github.com/hetznercloud/cli/internal/cmd/output"
"github.com/hetznercloud/cli/internal/cmd/placementgroup"
"github.com/hetznercloud/cli/internal/cmd/primaryip"
"github.com/hetznercloud/cli/internal/cmd/server"
"github.com/hetznercloud/cli/internal/cmd/sshkey"
"github.com/hetznercloud/cli/internal/cmd/volume"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var listCmd = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "list FLAGS",
Short: "List all resources",
}

cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels")

cmd.Flags().Bool("paid", false, "Only list paid resources")

output.AddFlag(cmd, output.OptionJSON())

return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error {

paid, _ := cmd.Flags().GetBool("paid")

outOpts := output.FlagsForCommand(cmd)

var cmds []base.ListCmd
if paid {
cmds = []base.ListCmd{
server.ListCmd,
loadbalancer.ListCmd,
primaryip.ListCmd,
floatingip.ListCmd,
image.ListCmd,
volume.ListCmd,
}
} else {
cmds = []base.ListCmd{
server.ListCmd,
network.ListCmd,
loadbalancer.ListCmd,
certificate.ListCmd,
firewall.ListCmd,
primaryip.ListCmd,
floatingip.ListCmd,
image.ListCmd,
volume.ListCmd,
iso.ListCmd,
placementgroup.ListCmd,
sshkey.ListCmd,
}
}

type response struct {
result []any
err error
}

responseChs := make([]chan response, len(cmds))

for i, lc := range cmds {
i, lc := i, lc
ch := make(chan response)
responseChs[i] = ch

go func() {
defer close(ch)

labelSelector, _ := cmd.Flags().GetString("selector")
listOpts := hcloud.ListOpts{
LabelSelector: labelSelector,
}

flagSet := pflag.NewFlagSet(lc.JSONKeyGetByName, pflag.ExitOnError)

switch lc.JSONKeyGetByName {
case image.ListCmd.JSONKeyGetByName:
flagSet.StringSlice("type", []string{"backup", "snapshot"}, "")
case iso.ListCmd.JSONKeyGetByName:
flagSet.StringSlice("type", []string{"private"}, "")
}

_ = flagSet.Parse([]string{})
result, err := lc.Fetch(ctx, client, flagSet, listOpts, []string{})
ch <- response{result, err}
}()
}

resources := make([][]any, len(cmds))
for i, responseCh := range responseChs {
response := <-responseCh
if err := response.err; err != nil {
return err
}
resources[i] = response.result
}

if outOpts.IsSet("json") {
jsonSchema := make(map[string]any)
for i, lc := range cmds {
jsonSchema[lc.JSONKeyGetByName] = lc.JSONSchema(resources[i])
}
jsonBytes, err := json.Marshal(jsonSchema)
if err != nil {
return err
}
fmt.Printf("%s\n", jsonBytes)
return nil
}

for i, lc := range cmds {
cols := lc.DefaultColumns
table := lc.OutputTable(client)
table.WriteHeader(cols)

if len(resources[i]) == 0 {
continue
}

fmt.Print(strings.ToUpper(lc.ResourceNamePlural) + "\n---\n")
for _, resource := range resources[i] {
table.Write(cols, resource)
}
if err := table.Flush(); err != nil {
return err
}
fmt.Println()
}

return nil
},
}
Loading

0 comments on commit a0dfe66

Please sign in to comment.