-
Notifications
You must be signed in to change notification settings - Fork 20
/
template_list.go
84 lines (69 loc) · 1.71 KB
/
template_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
package cmd
import (
"os"
"strconv"
"strings"
humanize "github.com/dustin/go-humanize"
"github.com/exoscale/egoscale"
"github.com/exoscale/cli/table"
"github.com/spf13/cobra"
)
func init() {
templateListCmd.Flags().BoolP("iso", "i", false, "List ISOs")
templateCmd.AddCommand(templateListCmd)
}
// templateListCmd represents the list command
var templateListCmd = &cobra.Command{
Use: "list [keyword]",
Short: "List all available templates",
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
t := table.NewTable(os.Stdout)
iso, err := cmd.Flags().GetBool("iso")
if err != nil {
return err
}
if iso {
return listISOs()
}
err = listTemplates(t, args)
if err == nil {
t.Render()
}
return err
},
}
func listISOs() error {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"Name", "Size", "Zone", "ID"})
resp, err := cs.ListWithContext(gContext, &egoscale.ListISOs{})
if err != nil {
return err
}
for _, i := range resp {
iso := i.(*egoscale.ISO)
sz := humanize.IBytes(uint64(iso.Size))
t.Append([]string{iso.Name, sz, iso.ZoneName, iso.ID.String()})
}
t.Render()
return nil
}
func listTemplates(t *table.Table, filters []string) error {
zoneID, err := getZoneIDByName(gCurrentAccount.DefaultZone)
if err != nil {
return err
}
templates, err := findTemplates(zoneID, filters...)
if err != nil {
return err
}
t.SetHeader([]string{"Operating System", "Disk", "Release Date", "ID"})
for _, template := range templates {
sz := strconv.FormatInt(template.Size>>30, 10)
if sz == "10" && strings.HasPrefix(template.Name, "Linux") {
sz = ""
}
t.Append([]string{template.Name, sz, template.Created, template.ID.String()})
}
return nil
}