Skip to content

Commit ff462c8

Browse files
authored
feat: get cluster config (#143)
* feat: get cluster config * fix: add context specific options
1 parent 9b2d03b commit ff462c8

File tree

11 files changed

+677
-44
lines changed

11 files changed

+677
-44
lines changed

cmd/create.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,10 @@
1515
package cmd
1616

1717
import (
18-
"encoding/json"
1918
"errors"
20-
"fmt"
21-
"log"
2219

2320
"github.com/equinor/radix-cli/pkg/flagnames"
24-
"github.com/equinor/radix-cli/pkg/flagvalues"
25-
"github.com/equinor/radix-cli/pkg/utils/completion"
2621
"github.com/spf13/cobra"
27-
"sigs.k8s.io/yaml"
2822
)
2923

3024
var outputFormat = "text"
@@ -40,25 +34,9 @@ var createCmd = &cobra.Command{
4034
}
4135

4236
func init() {
43-
createCmd.PersistentFlags().StringVarP(&outputFormat, flagnames.Output, "o", "text", "(Optional) Output format. json or not set (plain text)")
44-
_ = createCmd.RegisterFlagCompletionFunc(flagnames.Application, completion.Output)
37+
createCmd.PersistentFlags().StringVarP(&outputFormat, flagnames.Output, "o", "text", "(Deprecated) Output format. json")
38+
_ = createCmd.PersistentFlags().MarkDeprecated(flagnames.Output, "Everything created is printed in JSON format by default.")
39+
_ = createCmd.PersistentFlags().MarkHidden(flagnames.Output)
4540

4641
rootCmd.AddCommand(createCmd)
4742
}
48-
49-
func printPayload(payload any) {
50-
if outputFormat == flagvalues.OutputFormatJson {
51-
jsonData, err := json.MarshalIndent(payload, "", " ")
52-
if err != nil {
53-
log.Fatalf("failed to print payload as json: %v", err)
54-
}
55-
fmt.Println(string(jsonData))
56-
return
57-
}
58-
59-
yamlData, err := yaml.Marshal(payload)
60-
if err != nil {
61-
log.Fatalf("failed to print payload as yaml: %v", err)
62-
}
63-
fmt.Println(string(yamlData))
64-
}

cmd/getApplication.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
19-
2018
"github.com/equinor/radix-cli/generated/radixapi/client/application"
2119
"github.com/equinor/radix-cli/generated/radixapi/client/platform"
2220
"github.com/equinor/radix-cli/pkg/client"
2321
"github.com/equinor/radix-cli/pkg/config"
2422
"github.com/equinor/radix-cli/pkg/flagnames"
2523
"github.com/equinor/radix-cli/pkg/utils/completion"
26-
"github.com/equinor/radix-cli/pkg/utils/json"
2724
"github.com/spf13/cobra"
2825
)
2926

27+
type GetApplicationResponse struct {
28+
Applications []string `json:"applications,omitempty"`
29+
}
30+
3031
// getApplicationCmd represents the getApplicationCmd command
3132
var getApplicationCmd = &cobra.Command{
3233
Use: "application",
@@ -49,32 +50,27 @@ var getApplicationCmd = &cobra.Command{
4950
// List applications
5051
showApplicationParams := platform.NewShowApplicationsParams()
5152
resp, err := apiClient.Platform.ShowApplications(showApplicationParams, nil)
53+
if err != nil {
54+
return err
55+
}
5256

5357
var appNames []string
54-
55-
if err == nil {
56-
for _, application := range resp.Payload {
57-
fmt.Println(*application.Name)
58-
appNames = append(appNames, *application.Name)
59-
}
60-
completion.UpdateAppNamesCache(appNames)
61-
62-
return nil
58+
for _, application := range resp.Payload {
59+
appNames = append(appNames, *application.Name)
6360
}
61+
completion.UpdateAppNamesCache(appNames)
62+
printPayload(GetApplicationResponse{Applications: appNames})
6463

65-
return err
64+
return nil
6665
}
66+
6767
getApplicationParams := application.NewGetApplicationParams()
6868
getApplicationParams.SetAppName(appName)
6969
resp, err := apiClient.Application.GetApplication(getApplicationParams, nil)
7070
if err != nil {
7171
return err
7272
}
73-
prettyJSON, err := json.Pretty(resp.Payload)
74-
if err != nil {
75-
return err
76-
}
77-
fmt.Println(*prettyJSON)
73+
printPayload(resp.Payload)
7874
return nil
7975
},
8076
}

cmd/getClusterConfig.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright © 2023
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"github.com/equinor/radix-cli/generated/radixapi/client/configuration"
19+
"github.com/equinor/radix-cli/pkg/client"
20+
"github.com/spf13/cobra"
21+
)
22+
23+
// getClusterConfigCmd represents the get-cluster-config command
24+
var getClusterConfigCmd = &cobra.Command{
25+
Use: "cluster-config",
26+
Short: "Gets setting from Radix cluster config",
27+
Long: `Helper functionality to get data from radix cluster config.`,
28+
RunE: func(cmd *cobra.Command, args []string) error {
29+
30+
apiClient, err := client.GetRadixApiForCommand(cmd)
31+
if err != nil {
32+
return err
33+
}
34+
35+
payload, err := apiClient.Configuration.GetConfiguration(configuration.NewGetConfigurationParams(), nil)
36+
if err != nil {
37+
return err
38+
}
39+
40+
printPayload(payload.Payload)
41+
return nil
42+
},
43+
}
44+
45+
func init() {
46+
getCmd.AddCommand(getClusterConfigCmd)
47+
setContextSpecificPersistentFlags(getClusterConfigCmd)
48+
}

cmd/root.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"os"
67
"runtime/debug"
@@ -12,8 +13,10 @@ import (
1213
"github.com/equinor/radix-cli/pkg/flagnames"
1314
"github.com/equinor/radix-cli/pkg/settings"
1415
"github.com/equinor/radix-cli/pkg/utils/completion"
16+
"github.com/mattn/go-isatty"
1517
log "github.com/sirupsen/logrus"
1618
"github.com/spf13/cobra"
19+
"github.com/tidwall/pretty"
1720
)
1821

1922
var Version = "dev"
@@ -88,3 +91,16 @@ func awaitReconciliation(checkFunc checkFn) bool {
8891
}
8992
}
9093
}
94+
95+
func printPayload(payload any) {
96+
jsonData, err := json.MarshalIndent(payload, "", " ")
97+
if err != nil {
98+
log.Fatalf("failed to print payload as json: %v", err)
99+
}
100+
101+
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
102+
jsonData = pretty.Color(jsonData, nil)
103+
}
104+
105+
fmt.Println(string(jsonData))
106+
}

generated/radixapi/client/configuration/configuration_client.go

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)