Skip to content

Commit

Permalink
Updated to use correct config file
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Kosiewski <[email protected]>
  • Loading branch information
Thomas Kosiewski committed Sep 22, 2023
1 parent 1fb3fe3 commit e583d73
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewLoginCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
loftctlGlobalFlags.Config = globalFlags.Config
} else {
var err error
loftctlGlobalFlags.Config, err = pro.GetLoftConfigFilePath()
loftctlGlobalFlags.Config, err = pro.LoftctlConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewLogoutCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
loftctlGlobalFlags.Config = globalFlags.Config
} else {
var err error
loftctlGlobalFlags.Config, err = pro.GetLoftConfigFilePath()
loftctlGlobalFlags.Config, err = pro.LoftctlConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/pro/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewProCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
loftctlGlobalFlags.Config = globalFlags.Config
} else {
var err error
loftctlGlobalFlags.Config, err = pro.GetLoftConfigFilePath()
loftctlGlobalFlags.Config, err = pro.LoftctlConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/pro/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ type VirtualClusterInstanceProject struct {
Project *managementv1.Project
}

var ErrConfigNotFound = errors.New("couldn't find vCluster.Pro config, please make sure to run 'vcluster login' to connect to an existing instance or 'vcluster pro start' to deploy a new instance")
var ErrConfigNotFound = errors.New("couldn't find vCluster.Pro config")

func CreateProClient() (client.Client, error) {
configPath, err := GetConfigFilePath()
configPath, err := LoftctlConfigFilePath()
if err != nil {
return nil, err
}

_, err = os.Stat(configPath)
if err != nil {
return nil, ErrConfigNotFound
return nil, fmt.Errorf("%w: please make sure to run 'vcluster login' to connect to an existing instance or 'vcluster pro start' to deploy a new instance", ErrConfigNotFound)
}

proClient, err := client.NewClientFromPath(configPath)
Expand Down
31 changes: 15 additions & 16 deletions pkg/pro/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
)

const (
VclusterProFolder = "pro"

VclusterProFolder = "pro"
LoftctlConfigFileName = "creds.json"
)

Expand All @@ -30,13 +29,13 @@ type CLIConfig struct {
LatestCheckAt time.Time `json:"latestCheck,omitempty"`
}

// getDefaultCLIConfig returns the default config
func getDefaultCLIConfig() *CLIConfig {
// defaultCLIConfig returns the default config
func defaultCLIConfig() *CLIConfig {
return &CLIConfig{}
}

// GetLoftConfigFilePath returns the path to the loft config file
func GetLoftConfigFilePath() (string, error) {
// LoftctlConfigFilePath returns the path to the loft config file
func LoftctlConfigFilePath() (string, error) {
home, err := homedir.Dir()
if err != nil {
return "", fmt.Errorf("failed to open vcluster pro configuration file from, unable to detect $HOME directory, falling back to default configuration, following error occurred: %w", err)
Expand All @@ -45,8 +44,8 @@ func GetLoftConfigFilePath() (string, error) {
return filepath.Join(home, cliconfig.VclusterFolder, VclusterProFolder, LoftctlConfigFileName), nil
}

// getConfigFilePath returns the path to the config file
func getConfigFilePath() (string, error) {
// configFilePath returns the path to the config file
func configFilePath() (string, error) {
home, err := homedir.Dir()
if err != nil {
return "", fmt.Errorf("failed to open vcluster pro configuration file from, unable to detect $HOME directory, falling back to default configuration, following error occurred: %w", err)
Expand All @@ -57,39 +56,39 @@ func getConfigFilePath() (string, error) {

// GetConfig returns the config from the config file
func GetConfig() (*CLIConfig, error) {
path, err := getConfigFilePath()
path, err := configFilePath()
if err != nil {
return getDefaultCLIConfig(), fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
return defaultCLIConfig(), fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}

// check if the file exists
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return getDefaultCLIConfig(), nil
return defaultCLIConfig(), nil
}
return getDefaultCLIConfig(), fmt.Errorf("failed to load vcluster configuration file from %s, falling back to default configuration, following error occurred: %w", path, err)
return defaultCLIConfig(), fmt.Errorf("failed to load vcluster configuration file from %s, falling back to default configuration, following error occurred: %w", path, err)
}
if fi.IsDir() {
return getDefaultCLIConfig(), fmt.Errorf("failed to load vcluster configuration file %s, falling back to default configuration, this path is a directory", path)
return defaultCLIConfig(), fmt.Errorf("failed to load vcluster configuration file %s, falling back to default configuration, this path is a directory", path)
}
file, err := os.Open(path)
if err != nil {
return getDefaultCLIConfig(), fmt.Errorf("failed to open vcluster configuration file from %s, falling back to default configuration, following error occurred: %w", path, err)
return defaultCLIConfig(), fmt.Errorf("failed to open vcluster configuration file from %s, falling back to default configuration, following error occurred: %w", path, err)
}
defer file.Close()
bytes, _ := io.ReadAll(file)
c := &CLIConfig{}
err = json.Unmarshal(bytes, &c)
if err != nil {
return getDefaultCLIConfig(), fmt.Errorf("failed to unmarshall vcluster configuration from %s file, falling back to default configuration, following error occurred: %w", path, err)
return defaultCLIConfig(), fmt.Errorf("failed to unmarshall vcluster configuration from %s file, falling back to default configuration, following error occurred: %w", path, err)
}
return c, nil
}

// WriteConfig writes the given config to the config file
func WriteConfig(c *CLIConfig) error {
path, err := getConfigFilePath()
path, err := configFilePath()
if err != nil {
return fmt.Errorf("failed to get vcluster configuration file path: %w", err)
}
Expand Down

0 comments on commit e583d73

Please sign in to comment.