1
- // common/apiResources.go
2
-
3
- package common
1
+ package commands
4
2
5
3
import (
6
4
"context"
@@ -12,8 +10,10 @@ import (
12
10
"sort"
13
11
"strings"
14
12
13
+ "github.com/cloudforet-io/cfctl/pkg/configs"
15
14
"github.com/jhump/protoreflect/grpcreflect"
16
15
"github.com/pterm/pterm"
16
+ "github.com/spf13/cobra"
17
17
"github.com/spf13/viper"
18
18
"google.golang.org/grpc"
19
19
"google.golang.org/grpc/credentials"
@@ -22,35 +22,24 @@ import (
22
22
"gopkg.in/yaml.v3"
23
23
)
24
24
25
- func loadShortNames () (map [string ]string , error ) {
26
- home , err := os .UserHomeDir ()
27
- if err != nil {
28
- return nil , fmt .Errorf ("unable to find home directory: %v" , err )
29
- }
30
- shortNamesFile := filepath .Join (home , ".cfctl" , "short_names.yaml" )
31
- shortNamesMap := make (map [string ]string )
32
- if _ , err := os .Stat (shortNamesFile ); err == nil {
33
- file , err := os .Open (shortNamesFile )
34
- if err != nil {
35
- return nil , fmt .Errorf ("failed to open short_names.yaml file: %v" , err )
36
- }
37
- defer file .Close ()
38
-
39
- err = yaml .NewDecoder (file ).Decode (& shortNamesMap )
40
- if err != nil {
41
- return nil , fmt .Errorf ("failed to decode short_names.yaml: %v" , err )
42
- }
25
+ // FetchApiResourcesCmd provides api-resources command for the given service
26
+ func FetchApiResourcesCmd (serviceName string ) * cobra.Command {
27
+ return & cobra.Command {
28
+ Use : "api_resources" ,
29
+ Short : fmt .Sprintf ("Displays supported API resources for the %s service" , serviceName ),
30
+ RunE : func (cmd * cobra.Command , args []string ) error {
31
+ return listAPIResources (serviceName )
32
+ },
43
33
}
44
- return shortNamesMap , nil
45
34
}
46
35
47
- func ListAPIResources (serviceName string ) error {
48
- config , err := loadConfig ()
36
+ func listAPIResources (serviceName string ) error {
37
+ setting , err := configs . LoadSetting ()
49
38
if err != nil {
50
- return fmt .Errorf ("failed to load config : %v" , err )
39
+ return fmt .Errorf ("failed to load setting : %v" , err )
51
40
}
52
41
53
- endpoint , err := getServiceEndpoint (config , serviceName )
42
+ endpoint , err := getServiceEndpoint (setting , serviceName )
54
43
if err != nil {
55
44
return fmt .Errorf ("failed to get endpoint for service %s: %v" , serviceName , err )
56
45
}
@@ -60,7 +49,7 @@ func ListAPIResources(serviceName string) error {
60
49
return fmt .Errorf ("failed to load short names: %v" , err )
61
50
}
62
51
63
- data , err := fetchServiceResources (serviceName , endpoint , shortNamesMap , config )
52
+ data , err := fetchServiceResources (serviceName , endpoint , shortNamesMap , setting )
64
53
if err != nil {
65
54
return fmt .Errorf ("failed to fetch resources for service %s: %v" , serviceName , err )
66
55
}
@@ -74,7 +63,7 @@ func ListAPIResources(serviceName string) error {
74
63
return nil
75
64
}
76
65
77
- func getServiceEndpoint (config * Config , serviceName string ) (string , error ) {
66
+ func getServiceEndpoint (config * configs. Setting , serviceName string ) (string , error ) {
78
67
envConfig := config .Environments [config .Environment ]
79
68
if envConfig .URL == "" {
80
69
return "" , fmt .Errorf ("URL not found in environment config" )
@@ -102,7 +91,29 @@ func getServiceEndpoint(config *Config, serviceName string) (string, error) {
102
91
return endpoint , nil
103
92
}
104
93
105
- func fetchServiceResources (serviceName , endpoint string , shortNamesMap map [string ]string , config * Config ) ([][]string , error ) {
94
+ func loadShortNames () (map [string ]string , error ) {
95
+ home , err := os .UserHomeDir ()
96
+ if err != nil {
97
+ return nil , fmt .Errorf ("unable to find home directory: %v" , err )
98
+ }
99
+ shortNamesFile := filepath .Join (home , ".cfctl" , "short_names.yaml" )
100
+ shortNamesMap := make (map [string ]string )
101
+ if _ , err := os .Stat (shortNamesFile ); err == nil {
102
+ file , err := os .Open (shortNamesFile )
103
+ if err != nil {
104
+ return nil , fmt .Errorf ("failed to open short_names.yaml file: %v" , err )
105
+ }
106
+ defer file .Close ()
107
+
108
+ err = yaml .NewDecoder (file ).Decode (& shortNamesMap )
109
+ if err != nil {
110
+ return nil , fmt .Errorf ("failed to decode short_names.yaml: %v" , err )
111
+ }
112
+ }
113
+ return shortNamesMap , nil
114
+ }
115
+
116
+ func fetchServiceResources (serviceName , endpoint string , shortNamesMap map [string ]string , config * configs.Setting ) ([][]string , error ) {
106
117
parts := strings .Split (endpoint , "://" )
107
118
if len (parts ) != 2 {
108
119
return nil , fmt .Errorf ("invalid endpoint format: %s" , endpoint )
@@ -239,33 +250,3 @@ func renderAPITable(data [][]string) {
239
250
// Render the table
240
251
pterm .DefaultTable .WithHasHeader ().WithData (table ).Render ()
241
252
}
242
-
243
- // wordWrap function remains the same
244
- func wordWrap (text string , width int ) string {
245
- var wrappedText string
246
- var line string
247
- words := strings .Fields (text ) // Split on spaces only
248
-
249
- for _ , word := range words {
250
- if len (line )+ len (word )+ 1 > width {
251
- if wrappedText != "" {
252
- wrappedText += "\n "
253
- }
254
- wrappedText += line
255
- line = word
256
- } else {
257
- if line != "" {
258
- line += " "
259
- }
260
- line += word
261
- }
262
- }
263
- if line != "" {
264
- if wrappedText != "" {
265
- wrappedText += "\n "
266
- }
267
- wrappedText += line
268
- }
269
-
270
- return wrappedText
271
- }
0 commit comments