Skip to content

Get the endpoint dynamically and set the setting file to get the environments #112

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 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/common/api_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func FetchApiResourcesCmd(serviceName string) *cobra.Command {
}

func ListAPIResources(serviceName string) error {
setting, err := configs.LoadSetting()
setting, err := configs.SetSettingFile()
if err != nil {
return fmt.Errorf("failed to load setting: %v", err)
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func loadShortNames() (map[string]string, error) {
return shortNamesMap, nil
}

func FetchServiceResources(serviceName, endpoint string, shortNamesMap map[string]string, config *configs.Setting) ([][]string, error) {
func FetchServiceResources(serviceName, endpoint string, shortNamesMap map[string]string, config *configs.Environments) ([][]string, error) {
parts := strings.Split(endpoint, "://")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid endpoint format: %s", endpoint)
Expand Down
2 changes: 1 addition & 1 deletion cmd/other/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ func updateSetting(envName, endpoint, envSuffix string) {
v.Set(envKey, endpoint)

proxyKey := fmt.Sprintf("environments.%s.proxy", envName)
if strings.HasPrefix(endpoint, "grpc://") || strings.HasPrefix(endpoint, "grpc+ssl://") {
if strings.HasPrefix(endpoint, "grpc+ssl://") {
isProxy, err := transport.CheckIdentityProxyAvailable(endpoint)
if err != nil {
pterm.Warning.Printf("Failed to check gRPC endpoint: %v\n", err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func addDynamicServiceCommands() error {
pterm.DefaultBox.WithTitle("Local gRPC Server Not Found").
WithTitleTopCenter().
WithBoxStyle(pterm.NewStyle(pterm.FgYellow)).
Printfln("Unable to connect to local gRPC server.\nPlease make sure your gRPC server is running on %s", config.Endpoint)
Printfln("Current environment: %s\nUnable to connect to local gRPC server.\nPlease make sure your gRPC server is running on %s", config.Environment, config.Endpoint)
return nil
}
defer func(conn *grpc.ClientConn) {
Expand Down Expand Up @@ -376,7 +376,7 @@ func addDynamicServiceCommands() error {
// If no cached endpoints, show progress with detailed messages
progressbar, _ := pterm.DefaultProgressbar.
WithTotal(4).
WithTitle(fmt.Sprintf("Setting up %s environment", config.Environment)).
WithTitle(fmt.Sprintf("Environments up %s environment", config.Environment)).
Start()

progressbar.UpdateTitle("Fetching available service endpoints from the API server")
Expand Down
2 changes: 1 addition & 1 deletion pkg/configs/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func GetIdentityEndpoint(apiEndpoint string) (string, bool, error) {
return "", false, nil
}

func GetServiceEndpoint(config *Setting, serviceName string) (string, error) {
func GetServiceEndpoint(config *Environments, serviceName string) (string, error) {
envConfig := config.Environments[config.Environment]
if envConfig.Endpoint == "" {
return "", fmt.Errorf("endpoint not found in environment config")
Expand Down
151 changes: 151 additions & 0 deletions pkg/configs/setting_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package configs

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/viper"
)

// Environments represents the complete configuration structure
type Environments struct {
Environment string `yaml:"environment"` // Current active environment
Environments map[string]Environment `yaml:"environments"` // Map of available environments
}

// Environment represents a single environment configuration
type Environment struct {
Endpoint string `yaml:"endpoint"` // gRPC or HTTP endpoint URL
Proxy string `yaml:"proxy"` // Proxy server address if required
Token string `yaml:"token"` // Authentication token
}

// SetSettingFile loads the setting from the default location (~/.cfctl/setting.yaml)
func SetSettingFile() (*Environments, error) {
settingPath, err := GetSettingFilePath()
if err != nil {
return nil, err
}

currentEnvName, err := getCurrentEnvName(settingPath)
if err != nil {
return nil, err
}

currentEnvValues, err := getCurrentEnvValues(currentEnvName.Environment)
if err != nil {
return nil, err
}

return &Environments{
Environment: currentEnvName.Environment,
Environments: map[string]Environment{
currentEnvName.Environment: *currentEnvValues,
},
}, nil
}

// GetSettingFilePath returns the path to the setting file in the .cfctl directory
func GetSettingFilePath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get home directory: %v", err)
}

return filepath.Join(home, ".cfctl", "setting.yaml"), nil
}

// getCurrentEnvName loads the main setting file using viper
func getCurrentEnvName(settingPath string) (*Environments, error) {
v, err := setViperWithSetting(settingPath)
if err != nil {
return nil, err
}

currentEnv := v.GetString("environment")
if currentEnv == "" {
return nil, fmt.Errorf("no environment set in settings.yaml")
}

return &Environments{Environment: currentEnv}, nil
}

// getCurrentEnvValues loads environment-specific setting
func getCurrentEnvValues(env string) (*Environment, error) {
settingPath, err := GetSettingFilePath()
if err != nil {
return nil, err
}

v, err := setViperWithSetting(settingPath)
if err != nil {
return nil, err
}

envSetting := &Environment{
Endpoint: v.GetString(fmt.Sprintf("environments.%s.endpoint", env)),
Proxy: v.GetString(fmt.Sprintf("environments.%s.proxy", env)),
}

if err := loadToken(env, envSetting); err != nil {
return nil, err
}

return envSetting, nil
}

// loadToken loads the appropriate token based on environment type
func loadToken(env string, envSetting *Environment) error {
if strings.HasSuffix(env, "-user") {
return loadUserToken(env, envSetting)
}

return loadAppToken(env, envSetting)
}

// loadUserToken loads token for user environments from access_token file
func loadUserToken(env string, envSetting *Environment) error {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("failed to get home directory: %v", err)
}

tokenPath := filepath.Join(home, ".cfctl", "cache", env, "access_token")
tokenBytes, err := os.ReadFile(tokenPath)
if err == nil {
envSetting.Token = strings.TrimSpace(string(tokenBytes))
}

return nil
}

// loadAppToken loads token for app environments from main setting
func loadAppToken(env string, envSetting *Environment) error {
settingPath, err := GetSettingFilePath()
if err != nil {
return err
}

v, err := setViperWithSetting(settingPath)
if err != nil {
return err
}

envSetting.Token = v.GetString(fmt.Sprintf("environments.%s.token", env))

return nil
}

// setViperWithSetting creates a new viper instance with the given config file
func setViperWithSetting(settingPath string) (*viper.Viper, error) {
v := viper.New()
v.SetConfigFile(settingPath)
v.SetConfigType("yaml")
if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("failed to read config file: %v", err)
}

return v, nil
}
188 changes: 0 additions & 188 deletions pkg/configs/settings.go

This file was deleted.

Loading
Loading