-
Notifications
You must be signed in to change notification settings - Fork 18
/
instance_type_show.go
100 lines (81 loc) · 2.45 KB
/
instance_type_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
100
package cmd
import (
"fmt"
"os"
"strings"
"github.com/dustin/go-humanize"
"github.com/exoscale/cli/table"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type instanceTypeShowOutput struct {
ID string `json:"id"`
Family string `json:"family"`
Size string `json:"name"`
Memory int64 `json:"memory"`
CPUs int64 `json:"cpus"`
GPUs int64 `json:"gpus"`
Authorized bool `json:"authorized"`
}
func (o *instanceTypeShowOutput) toJSON() { outputJSON(o) }
func (o *instanceTypeShowOutput) toText() { outputText(o) }
func (o *instanceTypeShowOutput) toTable() {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"Instance Type"})
defer t.Render()
t.Append([]string{"ID", o.ID})
t.Append([]string{"Family", o.Family})
t.Append([]string{"Size", o.Size})
t.Append([]string{"Memory", humanize.IBytes(uint64(o.Memory))})
t.Append([]string{"# CPUs", fmt.Sprint(o.CPUs)})
if o.GPUs > 0 {
t.Append([]string{"# GPUs", fmt.Sprint(o.GPUs)})
}
t.Append([]string{"Authorized", fmt.Sprint(o.Authorized)})
}
type instanceTypeShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"show"`
Type string `cli-arg:"#" cli-usage:"[FAMILY.]SIZE"`
}
func (c *instanceTypeShowCmd) cmdAliases() []string { return gShowAlias }
func (c *instanceTypeShowCmd) cmdShort() string {
return "Show a Compute instance type details"
}
func (c *instanceTypeShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows a Compute instance type details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&instanceTypeShowOutput{}), ", "))
}
func (c *instanceTypeShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *instanceTypeShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(gCurrentAccount.Environment, gCurrentAccount.DefaultZone),
)
t, err := cs.FindInstanceType(ctx, gCurrentAccount.DefaultZone, c.Type)
if err != nil {
return err
}
return c.outputFunc(&instanceTypeShowOutput{
ID: *t.ID,
Family: *t.Family,
Size: *t.Size,
Memory: *t.Memory,
CPUs: *t.CPUs,
GPUs: func() (v int64) {
if t.GPUs != nil {
v = *t.GPUs
}
return
}(),
Authorized: *t.Authorized,
}, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(instanceTypeCmd, &instanceTypeShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}