-
Notifications
You must be signed in to change notification settings - Fork 80
/
list.go
101 lines (84 loc) · 3.05 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package iso
import (
"fmt"
"slices"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/output"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)
var ListCmd = base.ListCmd{
ResourceNamePlural: "ISOs",
JSONKeyGetByName: "isos",
DefaultColumns: []string{"id", "name", "description", "type", "architecture"},
SortOption: nil, // ISOs does not support sorting
AdditionalFlags: func(cmd *cobra.Command) {
cmd.Flags().StringSlice("architecture", []string{}, "Only show images of given architecture: x86|arm")
_ = cmd.RegisterFlagCompletionFunc("architecture", cmpl.SuggestCandidates(string(hcloud.ArchitectureX86), string(hcloud.ArchitectureARM)))
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.")
cmd.Flags().StringSlice("type", []string{"public", "private"}, "Types to include (public, private)")
_ = cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("public", "private"))
},
Fetch: func(s state.State, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.ISOListOpts{ListOpts: listOpts}
types, _ := flags.GetStringSlice("type")
var unknown []string
for _, t := range types {
switch t {
case string(hcloud.ISOTypePublic), string(hcloud.ISOTypePrivate):
break
default:
unknown = append(unknown, t)
}
}
if len(unknown) > 0 {
return nil, fmt.Errorf("unknown ISO types %s", strings.Join(unknown, ", "))
}
architecture, _ := flags.GetStringSlice("architecture")
if len(architecture) > 0 {
for _, arch := range architecture {
opts.Architecture = append(opts.Architecture, hcloud.Architecture(arch))
}
}
includeArchitectureWildcard, _ := flags.GetBool("include-architecture-wildcard")
if includeArchitectureWildcard {
opts.IncludeWildcardArchitecture = includeArchitectureWildcard
}
if len(sorts) > 0 {
opts.Sort = sorts
}
isos, err := s.Client().ISO().AllWithOpts(s, opts)
var resources []interface{}
for _, iso := range isos {
if slices.Contains(types, string(iso.Type)) {
resources = append(resources, iso)
}
}
return resources, err
},
OutputTable: func(t *output.Table, _ hcapi2.Client) {
t.
AddAllowedFields(hcloud.ISO{}).
AddFieldFn("architecture", func(obj interface{}) string {
iso := obj.(*hcloud.ISO)
if iso.Architecture == nil {
return "-"
}
return string(*iso.Architecture)
})
},
Schema: func(resources []interface{}) interface{} {
isoSchemas := make([]schema.ISO, 0, len(resources))
for _, resource := range resources {
iso := resource.(*hcloud.ISO)
isoSchemas = append(isoSchemas, hcloud.SchemaFromISO(iso))
}
return isoSchemas
},
}