/
get_instance.go
100 lines (84 loc) · 2.45 KB
/
get_instance.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 get
import (
"context"
"github.com/0xch4z/linodectl/internal/cli/genericoptions"
cmdutil "github.com/0xch4z/linodectl/internal/cmd/util"
"github.com/0xch4z/linodectl/internal/printer"
"github.com/0xch4z/linodectl/internal/resource/instance"
"github.com/0xch4z/linodectl/internal/resource/resourceref"
"github.com/linode/linodego"
"github.com/spf13/cobra"
)
type GetInstanceOptions struct {
refs resourceref.List
genericoptions.PaginationFlags
genericoptions.ProfileFlags
genericoptions.PrinterFlags
instance.FilterFlags
cmdutil.IOStreams
}
func NewGetInstanceOptions(ioStreams cmdutil.IOStreams) *GetInstanceOptions {
return &GetInstanceOptions{
IOStreams: ioStreams,
}
}
func NewCmdGetInstance(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command {
o := NewGetInstanceOptions(ioStreams)
cmd := &cobra.Command{
Use: "instance [NAME] [args...]",
Aliases: []string{"instances", "linode", "linodes"},
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Complete(f, ioStreams, args); err != nil {
return err
}
return o.Run(f, cmd)
},
}
o.PaginationFlags.AddFlags(cmd)
o.ProfileFlags.AddFlags(cmd)
o.PrinterFlags.AddFlags(cmd)
o.FilterFlags.AddFlags(cmd)
return cmd
}
func (o *GetInstanceOptions) Complete(f cmdutil.Factory, ioStreams cmdutil.IOStreams, args []string) (err error) {
if o.refs, err = resourceref.ListFromArgs(args); err != nil {
return err
}
return nil
}
func (o *GetInstanceOptions) Run(f cmdutil.Factory, cmd *cobra.Command) error {
filter := o.Filter(o.refs.Label())
filterBytes, err := filter.MarshalJSON()
if err != nil {
return err
}
client, err := f.Client(o.ProfileName())
if err != nil {
return err
}
ctx := context.Background()
instances, err := client.ListInstances(ctx, &linodego.ListOptions{
PageOptions: o.PageOptions(),
PageSize: o.PageOptions().Results,
Filter: string(filterBytes),
})
if err != nil {
return err
}
if o.LKECluster() != 0 {
if instances, err = instance.FilterLKECluster(ctx, client, o.LKECluster(), instances); err != nil {
return err
}
}
if len(o.refs) > 0 {
instances = instance.FilterByRefs(instances, o.refs)
}
resourceList := instance.NewList(instances)
p := printer.New(o.Out)
return p.PrintResources(context.Background(), resourceList, printer.ResourcePrintOptions{
Columns: o.Fields(),
SortBy: o.SortBy(),
OmitHeader: o.NoHeader(),
DescendingOrder: o.Descending(),
})
}