Skip to content

Commit 4b05e94

Browse files
get all also list gateway resource (#96)
* get all also list gateway resource * Update cmd/get.go Co-authored-by: Aurélie Marcuzzo <[email protected]> * Update cmd/get.go Co-authored-by: Aurélie Marcuzzo <[email protected]> --------- Co-authored-by: Aurélie Marcuzzo <[email protected]>
1 parent adf360f commit 4b05e94

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

cmd/get.go

+34-7
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,22 @@ func printResource(result interface{}, format OutputFormat) error {
9494
return nil
9595
}
9696

97+
func isGateway(kind schema.Kind) bool {
98+
_, isGatewayKind := kind.GetLatestKindVersion().(*schema.GatewayKindVersion)
99+
return isGatewayKind
100+
}
101+
102+
func isConsole(kind schema.Kind) bool {
103+
_, isConsoleKind := kind.GetLatestKindVersion().(*schema.ConsoleKindVersion)
104+
return isConsoleKind
105+
}
106+
97107
func initGet(kinds schema.KindCatalog) {
98108
rootCmd.AddCommand(getCmd)
99109
var format OutputFormat = YAML
100110

111+
var onlyGateway *bool
112+
var onlyConsole *bool
101113
var allCmd = &cobra.Command{
102114
Use: "all",
103115
Short: "Get all global resources",
@@ -106,23 +118,35 @@ func initGet(kinds schema.KindCatalog) {
106118
var allResources []resource.Resource
107119

108120
kindsByName := sortedKeys(kinds)
121+
if gatewayApiClientError != nil {
122+
if *debug || *onlyGateway {
123+
fmt.Fprintf(os.Stderr, "Cannot create Gateway client: %s\n", gatewayApiClientError)
124+
}
125+
}
126+
if consoleApiClientError != nil {
127+
if *debug || *onlyConsole {
128+
fmt.Fprintf(os.Stderr, "Cannot create Console client: %s\n", consoleApiClientError)
109129

130+
}
131+
}
110132
for _, key := range kindsByName {
111133
kind := kinds[key]
112-
113-
// keep only the Kinds where kind.GetParentFlag() is empty and not of GatewayKind (demands extra configuration, TODO fix if config is provided)
114-
if len(kind.GetParentFlag())+len(kind.GetParentQueryFlag()) > 0 {
134+
// keep only the Kinds where listing is provided TODO fix if config is provided
135+
if !kind.IsRootKind() {
115136
continue
116137
}
117-
if _, isGatewayKind := kind.GetLatestKindVersion().(*schema.GatewayKindVersion); isGatewayKind {
118-
continue
138+
var resources []resource.Resource
139+
var err error
140+
if isGateway(kind) && !*onlyConsole && gatewayApiClientError == nil {
141+
resources, err = gatewayApiClient().Get(&kind, []string{}, []string{}, map[string]string{})
142+
} else if isConsole(kind) && !*onlyGateway && consoleApiClientError == nil {
143+
resources, err = consoleApiClient().Get(&kind, []string{}, []string{}, map[string]string{})
119144
}
120-
121-
resources, err := consoleApiClient().Get(&kind, []string{}, []string{}, map[string]string{})
122145
if err != nil {
123146
fmt.Fprintf(os.Stderr, "Error fetching resource %s: %s\n", kind.GetName(), err)
124147
continue
125148
}
149+
126150
allResources = append(allResources, resources...)
127151
}
128152
err := printResource(allResources, format)
@@ -132,6 +156,9 @@ func initGet(kinds schema.KindCatalog) {
132156
}
133157
},
134158
}
159+
onlyGateway = allCmd.Flags().BoolP("gateway", "g", false, "Only show gateway resources")
160+
onlyConsole = allCmd.Flags().BoolP("console", "c", false, "Only show console resources")
161+
allCmd.MarkFlagsMutuallyExclusive("gateway", "console")
135162
allCmd.Flags().VarP(enumflag.New(&format, "output", OutputFormatIds, enumflag.EnumCaseInsensitive), "output", "o", "Output format. One of: json|yaml|name")
136163
getCmd.AddCommand(allCmd)
137164

cmd/root.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111

1212
var debug *bool
1313
var apiClient_ *client.Client
14-
var apiClientError error
14+
var consoleApiClientError error
1515
var gatewayApiClient_ *client.GatewayClient
1616
var gatewayApiClientError error
1717

1818
func consoleApiClient() *client.Client {
19-
if apiClientError != nil {
20-
fmt.Fprintf(os.Stderr, "Cannot create client: %s", apiClientError)
19+
if consoleApiClientError != nil {
20+
fmt.Fprintf(os.Stderr, "Cannot create client: %s", consoleApiClientError)
2121
os.Exit(1)
2222
}
2323
return apiClient_
@@ -42,7 +42,7 @@ For server TLS authentication, you can ignore the certificate by setting CDK_INS
4242
if *debug {
4343
// ActivateDebug() will enable debug mode for the resty client.
4444
// Doesn't need to be set if the client was not initialised correctly.
45-
if apiClientError == nil {
45+
if consoleApiClientError == nil {
4646
consoleApiClient().ActivateDebug()
4747
}
4848
if gatewayApiClientError == nil {
@@ -66,9 +66,9 @@ func Execute() {
6666
}
6767

6868
func init() {
69-
apiClient_, apiClientError = client.MakeFromEnv()
69+
apiClient_, consoleApiClientError = client.MakeFromEnv()
7070
var consoleKinds *schema.Catalog
71-
if apiClientError == nil {
71+
if consoleApiClientError == nil {
7272
consoleKinds = apiClient_.GetCatalog()
7373
} else {
7474
consoleKinds = schema.ConsoleDefaultCatalog()

schema/kind.go

+16
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ func (kind *Kind) GetParentFlag() []string {
4646
return kindVersion.GetParentPathParam()
4747
}
4848

49+
func (kind *Kind) IsRootKind() bool {
50+
kindVersion := kind.GetLatestKindVersion()
51+
if len(kindVersion.GetParentPathParam()) != 0 {
52+
return false
53+
}
54+
if len(kindVersion.GetParentQueryParam()) != 0 {
55+
return false
56+
}
57+
for _, queryParam := range kindVersion.GetListQueryParameter() {
58+
if queryParam.Required {
59+
return false
60+
}
61+
}
62+
return true
63+
}
64+
4965
func (kind *Kind) GetParentQueryFlag() []string {
5066
kindVersion := kind.GetLatestKindVersion()
5167
return kindVersion.GetParentQueryParam()

0 commit comments

Comments
 (0)