-
Notifications
You must be signed in to change notification settings - Fork 20
/
template_show.go
99 lines (82 loc) · 2.63 KB
/
template_show.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
package cmd
import (
"fmt"
"strings"
humanize "github.com/dustin/go-humanize"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type templateShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
OSType string `json:"os_type" outputLabel:"OS Type"`
CreationDate string `json:"creation_date"`
Zone string `json:"zone"`
DiskSize string `json:"disk_size"`
Username string `json:"username"`
Password bool `json:"password" outputLabel:"Password?"`
BootMode string `json:"boot_mode"`
}
func (o *templateShowOutput) Type() string { return "Template" }
func (o *templateShowOutput) toJSON() { outputJSON(o) }
func (o *templateShowOutput) toText() { outputText(o) }
func (o *templateShowOutput) toTable() { outputTable(o) }
func init() {
templateShowCmd := &cobra.Command{
Use: "show NAME|ID",
Short: "Show a template details",
Long: fmt.Sprintf(`This command shows a Compute instance template details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&templateShowOutput{}), ", ")),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{"zone"})
},
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
templateFilterCmd, err := cmd.Flags().GetString("template-filter")
if err != nil {
return err
}
templateFilter, err := validateTemplateFilter(templateFilterCmd)
if err != nil {
return err
}
zoneName, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
zone, err := getZoneByNameOrID(zoneName)
if err != nil {
return err
}
template, err := getTemplateByNameOrID(zone.ID, name, templateFilter)
if err != nil {
return err
}
return output(showTemplate(template))
},
}
templateShowCmd.Flags().StringP("template-filter", "", "featured", templateFilterHelp)
templateShowCmd.Flags().StringP("zone", "z", "", zoneHelp)
templateCmd.AddCommand(templateShowCmd)
}
func showTemplate(template *egoscale.Template) (outputter, error) {
out := templateShowOutput{
ID: template.ID.String(),
Name: template.Name,
OSType: template.OsTypeName,
CreationDate: template.Created,
Zone: template.ZoneName,
DiskSize: humanize.IBytes(uint64(template.Size)),
Password: template.PasswordEnabled,
BootMode: template.BootMode,
}
if username, ok := template.Details["username"]; ok {
out.Username = username
}
return &out, nil
}