/
list.go
140 lines (119 loc) · 3.79 KB
/
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
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
134
135
136
137
138
139
140
package list
import (
"errors"
"fmt"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/api/admin"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/service"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/cmdutil"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/dumper"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/request"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
)
const (
CommandName = "list"
CommandAlias = "ls"
)
type options struct {
request.ListOptions
outputFormat string
clusterID string
namespaceID string
f *factory.Factory
}
type connectorDetail struct {
admin.ConnectorAdminView `json:",inline" yaml:",inline"`
ClusterID string `json:"cluster_id,omitempty" yaml:"cluster_id,omitempty"`
PlatformID string `json:"platform_id,omitempty" yaml:"platform_id,omitempty"`
}
func NewListCommand(f *factory.Factory) *cobra.Command {
opts := options{
f: f,
}
sc, err := f.ServiceContext.Load()
if err == nil && sc != nil {
opts.namespaceID = sc.Contexts[sc.CurrentContext].NamespaceID
}
cmd := &cobra.Command{
Use: CommandName,
Aliases: []string{CommandAlias},
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := cmdutil.ValidateOutputs(cmd); err != nil {
return err
}
if opts.clusterID != "" && opts.namespaceID != "" {
return errors.New("set either cluster-id or namespace-id, not both")
}
if opts.clusterID == "" && opts.namespaceID == "" {
return errors.New("either cluster-id or namespace-id are required")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return run(&opts)
},
}
cmdutil.AddOutput(cmd, &opts.outputFormat)
cmdutil.AddPage(cmd, &opts.Page)
cmdutil.AddLimit(cmd, &opts.Limit)
cmdutil.AddAllPages(cmd, &opts.AllPages)
cmdutil.AddOrderBy(cmd, &opts.OrderBy)
cmdutil.AddSearch(cmd, &opts.Search)
cmdutil.AddClusterID(cmd, &opts.clusterID)
cmdutil.AddNamespaceID(cmd, &opts.namespaceID)
return cmd
}
func run(opts *options) error {
c, err := service.NewAdminClient(&service.Config{
F: opts.f,
})
if err != nil {
return err
}
var connectors admin.ConnectorAdminViewList
var cluster *admin.ConnectorClusterAdminView
switch {
case opts.clusterID != "":
connectors, err = service.ListConnectorsForCluster(c, opts.ListOptions, opts.clusterID)
if err == nil {
cluster, err = service.GetClusterByID(c, opts.clusterID)
}
case opts.namespaceID != "":
connectors, err = service.ListConnectorsForNamespace(c, opts.ListOptions, opts.namespaceID)
if err == nil {
cluster, err = service.GetClusterForNamespace(c, opts.namespaceID)
}
}
if err != nil {
return err
}
if len(connectors.Items) == 0 && opts.outputFormat == "" {
_, _ = fmt.Fprint(opts.f.IOStreams.Out, "No result\n")
return nil
}
items := make([]connectorDetail, len(connectors.Items))
for i := range connectors.Items {
items[i] = connectorDetail{
ConnectorAdminView: connectors.Items[i],
ClusterID: cluster.Id,
PlatformID: cluster.Status.Platform.Id,
}
}
switch opts.outputFormat {
case dump.EmptyFormat:
_, _ = fmt.Fprint(opts.f.IOStreams.Out, "\n")
dumpAsTable(opts.f.IOStreams.Out, items, false, dumper.TableStyleDefault)
_, _ = fmt.Fprint(opts.f.IOStreams.Out, "\n")
case cmdutil.OutputFormatWide:
_, _ = fmt.Fprint(opts.f.IOStreams.Out, "\n")
dumpAsTable(opts.f.IOStreams.Out, items, true, dumper.TableStyleDefault)
_, _ = fmt.Fprint(opts.f.IOStreams.Out, "\n")
case cmdutil.OutputFormatCSV:
dumpAsTable(opts.f.IOStreams.Out, items, true, dumper.TableStyleCSV)
default:
return dump.Formatted(opts.f.IOStreams.Out, opts.outputFormat, items)
}
return nil
}