Skip to content

Refactor for grpc and rest #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 3, 2025
99 changes: 40 additions & 59 deletions cmd/common/apiResources.go → cmd/commands/api_resources.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// common/apiResources.go

package common
package commands

import (
"context"
Expand All @@ -12,8 +10,10 @@ import (
"sort"
"strings"

"github.com/cloudforet-io/cfctl/pkg/configs"
"github.com/jhump/protoreflect/grpcreflect"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand All @@ -22,35 +22,24 @@ import (
"gopkg.in/yaml.v3"
)

func loadShortNames() (map[string]string, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("unable to find home directory: %v", err)
}
shortNamesFile := filepath.Join(home, ".cfctl", "short_names.yaml")
shortNamesMap := make(map[string]string)
if _, err := os.Stat(shortNamesFile); err == nil {
file, err := os.Open(shortNamesFile)
if err != nil {
return nil, fmt.Errorf("failed to open short_names.yaml file: %v", err)
}
defer file.Close()

err = yaml.NewDecoder(file).Decode(&shortNamesMap)
if err != nil {
return nil, fmt.Errorf("failed to decode short_names.yaml: %v", err)
}
// FetchApiResourcesCmd provides api-resources command for the given service
func FetchApiResourcesCmd(serviceName string) *cobra.Command {
return &cobra.Command{
Use: "api_resources",
Short: fmt.Sprintf("Displays supported API resources for the %s service", serviceName),
RunE: func(cmd *cobra.Command, args []string) error {
return listAPIResources(serviceName)
},
}
return shortNamesMap, nil
}

func ListAPIResources(serviceName string) error {
config, err := loadConfig()
func listAPIResources(serviceName string) error {
setting, err := configs.LoadSetting()
if err != nil {
return fmt.Errorf("failed to load config: %v", err)
return fmt.Errorf("failed to load setting: %v", err)
}

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

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

func getServiceEndpoint(config *Config, serviceName string) (string, error) {
func getServiceEndpoint(config *configs.Setting, serviceName string) (string, error) {
envConfig := config.Environments[config.Environment]
if envConfig.URL == "" {
return "", fmt.Errorf("URL not found in environment config")
Expand Down Expand Up @@ -102,7 +91,29 @@ func getServiceEndpoint(config *Config, serviceName string) (string, error) {
return endpoint, nil
}

func fetchServiceResources(serviceName, endpoint string, shortNamesMap map[string]string, config *Config) ([][]string, error) {
func loadShortNames() (map[string]string, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("unable to find home directory: %v", err)
}
shortNamesFile := filepath.Join(home, ".cfctl", "short_names.yaml")
shortNamesMap := make(map[string]string)
if _, err := os.Stat(shortNamesFile); err == nil {
file, err := os.Open(shortNamesFile)
if err != nil {
return nil, fmt.Errorf("failed to open short_names.yaml file: %v", err)
}
defer file.Close()

err = yaml.NewDecoder(file).Decode(&shortNamesMap)
if err != nil {
return nil, fmt.Errorf("failed to decode short_names.yaml: %v", err)
}
}
return shortNamesMap, nil
}

func fetchServiceResources(serviceName, endpoint string, shortNamesMap map[string]string, config *configs.Setting) ([][]string, error) {
parts := strings.Split(endpoint, "://")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid endpoint format: %s", endpoint)
Expand Down Expand Up @@ -239,33 +250,3 @@ func renderAPITable(data [][]string) {
// Render the table
pterm.DefaultTable.WithHasHeader().WithData(table).Render()
}

// wordWrap function remains the same
func wordWrap(text string, width int) string {
var wrappedText string
var line string
words := strings.Fields(text) // Split on spaces only

for _, word := range words {
if len(line)+len(word)+1 > width {
if wrappedText != "" {
wrappedText += "\n"
}
wrappedText += line
line = word
} else {
if line != "" {
line += " "
}
line += word
}
}
if line != "" {
if wrappedText != "" {
wrappedText += "\n"
}
wrappedText += line
}

return wrappedText
}
20 changes: 0 additions & 20 deletions cmd/common/fetchApiResources.go

This file was deleted.

Loading
Loading