Skip to content

Commit d93128c

Browse files
authored
Merge pull request #106 from yjinjo/master
Refactor for grpc and rest
2 parents 6bdc2a8 + 263a4b4 commit d93128c

16 files changed

+1247
-914
lines changed

cmd/common/apiResources.go renamed to cmd/commands/api_resources.go

+40-59
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// common/apiResources.go
2-
3-
package common
1+
package commands
42

53
import (
64
"context"
@@ -12,8 +10,10 @@ import (
1210
"sort"
1311
"strings"
1412

13+
"github.com/cloudforet-io/cfctl/pkg/configs"
1514
"github.com/jhump/protoreflect/grpcreflect"
1615
"github.com/pterm/pterm"
16+
"github.com/spf13/cobra"
1717
"github.com/spf13/viper"
1818
"google.golang.org/grpc"
1919
"google.golang.org/grpc/credentials"
@@ -22,35 +22,24 @@ import (
2222
"gopkg.in/yaml.v3"
2323
)
2424

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+
},
4333
}
44-
return shortNamesMap, nil
4534
}
4635

47-
func ListAPIResources(serviceName string) error {
48-
config, err := loadConfig()
36+
func listAPIResources(serviceName string) error {
37+
setting, err := configs.LoadSetting()
4938
if err != nil {
50-
return fmt.Errorf("failed to load config: %v", err)
39+
return fmt.Errorf("failed to load setting: %v", err)
5140
}
5241

53-
endpoint, err := getServiceEndpoint(config, serviceName)
42+
endpoint, err := getServiceEndpoint(setting, serviceName)
5443
if err != nil {
5544
return fmt.Errorf("failed to get endpoint for service %s: %v", serviceName, err)
5645
}
@@ -60,7 +49,7 @@ func ListAPIResources(serviceName string) error {
6049
return fmt.Errorf("failed to load short names: %v", err)
6150
}
6251

63-
data, err := fetchServiceResources(serviceName, endpoint, shortNamesMap, config)
52+
data, err := fetchServiceResources(serviceName, endpoint, shortNamesMap, setting)
6453
if err != nil {
6554
return fmt.Errorf("failed to fetch resources for service %s: %v", serviceName, err)
6655
}
@@ -74,7 +63,7 @@ func ListAPIResources(serviceName string) error {
7463
return nil
7564
}
7665

77-
func getServiceEndpoint(config *Config, serviceName string) (string, error) {
66+
func getServiceEndpoint(config *configs.Setting, serviceName string) (string, error) {
7867
envConfig := config.Environments[config.Environment]
7968
if envConfig.URL == "" {
8069
return "", fmt.Errorf("URL not found in environment config")
@@ -102,7 +91,29 @@ func getServiceEndpoint(config *Config, serviceName string) (string, error) {
10291
return endpoint, nil
10392
}
10493

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) {
106117
parts := strings.Split(endpoint, "://")
107118
if len(parts) != 2 {
108119
return nil, fmt.Errorf("invalid endpoint format: %s", endpoint)
@@ -239,33 +250,3 @@ func renderAPITable(data [][]string) {
239250
// Render the table
240251
pterm.DefaultTable.WithHasHeader().WithData(table).Render()
241252
}
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-
}

cmd/common/fetchApiResources.go

-20
This file was deleted.

0 commit comments

Comments
 (0)