Skip to content

Commit

Permalink
Move datacenter commands to new structure (#335)
Browse files Browse the repository at this point in the history
* Move datacenter commands to new structure

Signed-off-by: Lukas Kämmerling <[email protected]>
  • Loading branch information
LKaemmerling authored Jun 23, 2021
1 parent fe852a5 commit 1a93c16
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 183 deletions.
19 changes: 10 additions & 9 deletions cmd/hcloud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

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

Expand All @@ -15,25 +16,25 @@ func init() {
}

func main() {
state := state.New()
cliState := state.New()

if state.ConfigPath != "" {
_, err := os.Stat(state.ConfigPath)
if cliState.ConfigPath != "" {
_, err := os.Stat(cliState.ConfigPath)
switch {
case err == nil:
if err := state.ReadConfig(); err != nil {
log.Fatalf("unable to read config file %q: %s\n", state.ConfigPath, err)
if err := cliState.ReadConfig(); err != nil {
log.Fatalf("unable to read config file %q: %s\n", cliState.ConfigPath, err)
}
case os.IsNotExist(err):
break
default:
log.Fatalf("unable to read config file %q: %s\n", state.ConfigPath, err)
log.Fatalf("unable to read config file %q: %s\n", cliState.ConfigPath, err)
}
}

state.ReadEnv()

rootCommand := cli.NewRootCommand(state)
cliState.ReadEnv()
apiClient := hcapi2.NewClient(cliState.Client())
rootCommand := cli.NewRootCommand(cliState, apiClient)
if err := rootCommand.Execute(); err != nil {
log.Fatalln(err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
"github.com/hetznercloud/cli/internal/cmd/sshkey"
"github.com/hetznercloud/cli/internal/cmd/version"
"github.com/hetznercloud/cli/internal/cmd/volume"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/spf13/cobra"
)

func NewRootCommand(state *state.State) *cobra.Command {
func NewRootCommand(state *state.State, client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "hcloud",
Short: "Hetzner Cloud CLI",
Expand All @@ -43,7 +44,7 @@ func NewRootCommand(state *state.State) *cobra.Command {
completion.NewCommand(state),
servertype.NewCommand(state),
context.NewCommand(state),
datacenter.NewCommand(state),
datacenter.NewCommand(state, client),
location.NewCommand(state),
iso.NewCommand(state),
volume.NewCommand(state),
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/certificate/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var describeCmd = base.DescribeCmd{
if len(cert.UsedBy) == 0 {
fmt.Println(" Certificate unused")
} else {
lbClient := client.LoadBalancer()
for _, ub := range cert.UsedBy {
fmt.Printf(" - Type: %s", ub.Type)
// Currently certificates can be only attached to load balancers.
Expand All @@ -63,7 +64,7 @@ var describeCmd = base.DescribeCmd{
fmt.Printf(" - ID: %d", ub.ID)
continue
}
fmt.Printf(" - Name: %s", client.LoadBalancer().LoadBalancerName(ub.ID))
fmt.Printf(" - Name: %s", lbClient.LoadBalancerName(ub.ID))
}
}
return nil
Expand Down
7 changes: 4 additions & 3 deletions internal/cmd/datacenter/datacenter.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package datacenter

import (
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/spf13/cobra"
)

func NewCommand(cli *state.State) *cobra.Command {
func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "datacenter",
Short: "Manage datacenters",
Expand All @@ -14,8 +15,8 @@ func NewCommand(cli *state.State) *cobra.Command {
DisableFlagsInUseLine: true,
}
cmd.AddCommand(
newListCommand(cli),
newDescribeCommand(cli),
listCmd.CobraCommand(cli.Context, client, cli),
describeCmd.CobraCommand(cli.Context, client, cli),
)
return cmd
}
151 changes: 47 additions & 104 deletions internal/cmd/datacenter/describe.go
Original file line number Diff line number Diff line change
@@ -1,117 +1,60 @@
package datacenter

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

"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/output"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
)

func newDescribeCommand(cli *state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "describe [FLAGS] DATACENTER",
Short: "Describe a datacenter",
Args: cobra.ExactArgs(1),
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.DataCenterNames)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: cli.EnsureToken,
RunE: cli.Wrap(runDescribe),
}
output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat())
return cmd
}

func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error {
outputFlags := output.FlagsForCommand(cmd)

idOrName := args[0]
datacenter, resp, err := cli.Client().Datacenter.Get(cli.Context, idOrName)
if err != nil {
return err
}
if datacenter == nil {
return fmt.Errorf("datacenter not found: %s", idOrName)
}

switch {
case outputFlags.IsSet("json"):
return describeJSON(resp)
case outputFlags.IsSet("format"):
return util.DescribeFormat(datacenter, outputFlags["format"][0])
default:
return describeText(cli, datacenter)
}
}

func describeText(cli *state.State, datacenter *hcloud.Datacenter) error {
fmt.Printf("ID:\t\t%d\n", datacenter.ID)
fmt.Printf("Name:\t\t%s\n", datacenter.Name)
fmt.Printf("Description:\t%s\n", datacenter.Description)
fmt.Printf("Location:\n")
fmt.Printf(" Name:\t\t%s\n", datacenter.Location.Name)
fmt.Printf(" Description:\t%s\n", datacenter.Location.Description)
fmt.Printf(" Country:\t%s\n", datacenter.Location.Country)
fmt.Printf(" City:\t\t%s\n", datacenter.Location.City)
fmt.Printf(" Latitude:\t%f\n", datacenter.Location.Latitude)
fmt.Printf(" Longitude:\t%f\n", datacenter.Location.Longitude)
fmt.Printf("Server Types:\n")

serverTypesMap := map[int]*hcloud.ServerType{}
for _, t := range datacenter.ServerTypes.Available {
serverTypesMap[t.ID] = t
}
for _, t := range datacenter.ServerTypes.Supported {
serverTypesMap[t.ID] = t
}
for id := range serverTypesMap {
var err error
serverTypesMap[id], _, err = cli.Client().ServerType.GetByID(cli.Context, id)
if err != nil {
return fmt.Errorf("error fetching server type: %v", err)
var describeCmd = base.DescribeCmd{
ResourceNameSingular: "datacenter",
ShortDescription: "Describe an datacenter",
JSONKeyGetByID: "datacenter",
JSONKeyGetByName: "datacenters",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.Datacenter().Names },
Fetch: func(ctx context.Context, client hcapi2.Client, idOrName string) (interface{}, *hcloud.Response, error) {
return client.Datacenter().Get(ctx, idOrName)
},
PrintText: func(ctx context.Context, client hcapi2.Client, resource interface{}) error {
datacenter := resource.(*hcloud.Datacenter)

fmt.Printf("ID:\t\t%d\n", datacenter.ID)
fmt.Printf("ID:\t\t%d\n", datacenter.ID)
fmt.Printf("Name:\t\t%s\n", datacenter.Name)
fmt.Printf("Description:\t%s\n", datacenter.Description)
fmt.Printf("Location:\n")
fmt.Printf(" Name:\t\t%s\n", datacenter.Location.Name)
fmt.Printf(" Description:\t%s\n", datacenter.Location.Description)
fmt.Printf(" Country:\t%s\n", datacenter.Location.Country)
fmt.Printf(" City:\t\t%s\n", datacenter.Location.City)
fmt.Printf(" Latitude:\t%f\n", datacenter.Location.Latitude)
fmt.Printf(" Longitude:\t%f\n", datacenter.Location.Longitude)
fmt.Printf("Server Types:\n")

printServerTypes := func(list []*hcloud.ServerType) {
for _, t := range list {
fmt.Printf(" - ID:\t\t %d\n", t.ID)
fmt.Printf(" Name:\t %s\n", client.ServerType().ServerTypeName(t.ID))
fmt.Printf(" Description: %s\n", client.ServerType().ServerTypeDescription(t.ID))
}
}
}

printServerTypes := func(list []*hcloud.ServerType, dataMap map[int]*hcloud.ServerType) {
for _, t := range list {
st := dataMap[t.ID]
fmt.Printf(" - ID:\t\t %d\n", st.ID)
fmt.Printf(" Name:\t %s\n", st.Name)
fmt.Printf(" Description: %s\n", st.Description)
fmt.Printf(" Available:\n")
if len(datacenter.ServerTypes.Available) > 0 {
printServerTypes(datacenter.ServerTypes.Available)
} else {
fmt.Printf(" No available server types\n")
}
fmt.Printf(" Supported:\n")
if len(datacenter.ServerTypes.Supported) > 0 {
printServerTypes(datacenter.ServerTypes.Supported)
} else {
fmt.Printf(" No supported server types\n")
}
}

fmt.Printf(" Available:\n")
if len(datacenter.ServerTypes.Available) > 0 {
printServerTypes(datacenter.ServerTypes.Available, serverTypesMap)
} else {
fmt.Printf(" No available server types\n")
}
fmt.Printf(" Supported:\n")
if len(datacenter.ServerTypes.Supported) > 0 {
printServerTypes(datacenter.ServerTypes.Supported, serverTypesMap)
} else {
fmt.Printf(" No supported server types\n")
}

return nil
}

func describeJSON(resp *hcloud.Response) error {
var data map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return err
}
if datacenter, ok := data["datacenter"]; ok {
return util.DescribeJSON(datacenter)
}
if datacenters, ok := data["datacenters"].([]interface{}); ok {
return util.DescribeJSON(datacenters[0])
}
return util.DescribeJSON(data)
return nil
},
}
93 changes: 32 additions & 61 deletions internal/cmd/datacenter/list.go
Original file line number Diff line number Diff line change
@@ -1,75 +1,46 @@
package datacenter

import (
"context"

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

var listTableOutput *output.Table

func init() {
listTableOutput = output.NewTable().
AddAllowedFields(hcloud.Datacenter{}).
AddFieldFn("location", output.FieldFn(func(obj interface{}) string {
datacenter := obj.(*hcloud.Datacenter)
return datacenter.Location.Name
}))
}

func newListCommand(cli *state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "list [FLAGS]",
Short: "List datacenters",
Long: util.ListLongDescription(
"Displays a list of datacenters.",
listTableOutput.Columns(),
),
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: cli.EnsureToken,
RunE: cli.Wrap(runList),
}
output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON())
return cmd
}

func runList(cli *state.State, cmd *cobra.Command, args []string) error {
outOpts := output.FlagsForCommand(cmd)
var listCmd = base.ListCmd{
ResourceNamePlural: "datacenters",
DefaultColumns: []string{"id", "name", "description", "location"},

datacenters, err := cli.Client().Datacenter.All(cli.Context)
Fetch: func(ctx context.Context, client hcapi2.Client, listOpts hcloud.ListOpts) ([]interface{}, error) {
datacenters, _, err := client.Datacenter().List(ctx, hcloud.DatacenterListOpts{ListOpts: listOpts})

if outOpts.IsSet("json") {
var datacenterSchemas []schema.Datacenter
for _, datacenter := range datacenters {
datacenterSchemas = append(datacenterSchemas, util.DatacenterToSchema(*datacenter))
var resources []interface{}
for _, n := range datacenters {
resources = append(resources, n)
}
return resources, err
},

OutputTable: func(_ hcapi2.Client) *output.Table {
return output.NewTable().
AddAllowedFields(hcloud.Datacenter{}).
AddFieldFn("location", output.FieldFn(func(obj interface{}) string {
datacenter := obj.(*hcloud.Datacenter)
return datacenter.Location.Name
}))
},

JSONSchema: func(resources []interface{}) interface{} {
var certSchemas []schema.Datacenter
for _, resource := range resources {
cert := resource.(*hcloud.Datacenter)
certSchemas = append(certSchemas, util.DatacenterToSchema(*cert))
}
return util.DescribeJSON(datacenterSchemas)
}

if err != nil {
return err
}

cols := []string{"id", "name", "description", "location"}
if outOpts.IsSet("columns") {
cols = outOpts["columns"]
}

tw := listTableOutput
if err = tw.ValidateColumns(cols); err != nil {
return err
}

if !outOpts.IsSet("noheader") {
tw.WriteHeader(cols)
}
for _, datacenter := range datacenters {
tw.Write(cols, datacenter)
}
tw.Flush()
return nil
return util.DescribeJSON(certSchemas)
},
}
Loading

0 comments on commit 1a93c16

Please sign in to comment.