diff --git a/cmd/vclusterctl/cmd/login.go b/cmd/vclusterctl/cmd/login.go index f7792d127..a20d009cf 100644 --- a/cmd/vclusterctl/cmd/login.go +++ b/cmd/vclusterctl/cmd/login.go @@ -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) } diff --git a/cmd/vclusterctl/cmd/logout.go b/cmd/vclusterctl/cmd/logout.go index 7d4e20497..95dc7b5fb 100644 --- a/cmd/vclusterctl/cmd/logout.go +++ b/cmd/vclusterctl/cmd/logout.go @@ -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) } diff --git a/cmd/vclusterctl/cmd/pro/pro.go b/cmd/vclusterctl/cmd/pro/pro.go index d91a47452..1967a834f 100644 --- a/cmd/vclusterctl/cmd/pro/pro.go +++ b/cmd/vclusterctl/cmd/pro/pro.go @@ -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) } diff --git a/pkg/pro/client.go b/pkg/pro/client.go index 2f46e6f29..08bc54096 100644 --- a/pkg/pro/client.go +++ b/pkg/pro/client.go @@ -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) diff --git a/pkg/pro/config.go b/pkg/pro/config.go index 3405a06d5..8dda0a67a 100644 --- a/pkg/pro/config.go +++ b/pkg/pro/config.go @@ -14,8 +14,7 @@ import ( ) const ( - VclusterProFolder = "pro" - + VclusterProFolder = "pro" LoftctlConfigFileName = "creds.json" ) @@ -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) @@ -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) @@ -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) }