-
Notifications
You must be signed in to change notification settings - Fork 44
/
get.go
133 lines (113 loc) · 3.39 KB
/
get.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package get
import (
"context"
"errors"
"fmt"
"io"
"strconv"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/konpyutaika/nifikop/api/v1alpha1"
"github.com/konpyutaika/nifikop/pkg/plugin/common"
)
var getExample = `
# view all NifiRegistryClient in the current namespace
%[1]s get
# view NifiUserGroup foo
%[1]s get foo
`
// options provides information required by Datadog get command.
type options struct {
genericclioptions.IOStreams
common.Options
args []string
name string
}
// newOptions provides an instance of getOptions with default values.
func newOptions(streams genericclioptions.IOStreams) *options {
o := &options{
IOStreams: streams,
}
o.SetConfigFlags()
return o
}
// New provides a cobra command wrapping options for "get" sub command.
func New(streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(streams)
cmd := &cobra.Command{
Use: "get [NifiRegistryClient name]",
Short: "Get NifiRegistryClient",
Example: fmt.Sprintf(getExample, "kubectl nifikop nifiregistryclient"),
SilenceUsage: true,
RunE: func(c *cobra.Command, args []string) error {
if err := o.complete(c, args); err != nil {
return err
}
if err := o.validate(); err != nil {
return err
}
return o.run()
},
}
o.ConfigFlags.AddFlags(cmd.Flags())
return cmd
}
// complete sets all information required for processing the command.
func (o *options) complete(cmd *cobra.Command, args []string) error {
o.args = args
if len(args) > 0 {
o.name = args[0]
}
return o.Init(cmd)
}
// validate ensures that all required arguments and flag values are provided.
func (o *options) validate() error {
if len(o.args) > 1 {
return errors.New("either one or no arguments are allowed")
}
return nil
}
// run runs the get command.
func (o *options) run() error {
list := &v1alpha1.NifiRegistryClientList{}
if o.name == "" {
if err := o.Client.List(context.TODO(), list, &client.ListOptions{Namespace: o.UserNamespace}); err != nil {
return fmt.Errorf("unable to list NifiRegistryClient: %w", err)
}
} else {
item := &v1alpha1.NifiRegistryClient{}
err := o.Client.Get(context.TODO(), client.ObjectKey{Namespace: o.UserNamespace, Name: o.name}, item)
if err != nil && apierrors.IsNotFound(err) {
return fmt.Errorf("NifiRegistryClient %s/%s not found", o.UserNamespace, o.name)
} else if err != nil {
return fmt.Errorf("unable to get NifiRegistryClient: %w", err)
}
list.Items = append(list.Items, *item)
}
table := newTable(o.Out)
for _, item := range list.Items {
data := []string{item.Namespace, item.Name}
data = append(data, string(item.Status.Id))
data = append(data, strconv.Itoa(int(item.Status.Version)))
table.Append(data)
}
// Send output.
table.Render()
return nil
}
func newTable(out io.Writer) *tablewriter.Table {
table := tablewriter.NewWriter(out)
table.SetHeader([]string{"Namespace", "Name", "Id", "Version"})
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetRowLine(false)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderLine(false)
return table
}