Skip to content

Commit

Permalink
Merge pull request #57 from supreeth7/config-name-fix
Browse files Browse the repository at this point in the history
Change config file name
  • Loading branch information
openshift-merge-robot authored Mar 30, 2023
2 parents d802596 + 322aace commit 0040057
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
29 changes: 20 additions & 9 deletions pkg/cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/openshift/backplane-cli/pkg/info"
)
Expand All @@ -13,31 +14,41 @@ type BackplaneConfiguration struct {
ProxyURL string `json:"proxy-url"`
}

func GetBackplaneConfigFile() string {
func GetBackplaneConfigFile() (string, error) {
path, bpConfigFound := os.LookupEnv(info.BACKPLANE_CONFIG_PATH_ENV_NAME)
if bpConfigFound {
return path
return path, nil
}

return getConfiDefaultPath(info.BACKPLANE_CONFIG_DEFAULT_FILE_NAME)
configFile, err := getDefaultConfigPath()
if err != nil {
return "", err
}

return configFile, nil
}

// Get Backplane config default path
func getConfiDefaultPath(fileName string) string {
configDir, err := os.UserConfigDir()

func getDefaultConfigPath() (string, error) {
UserHomeDir, err := os.UserHomeDir()
if err != nil {
return fileName
return "", err
}

return configDir + "/" + fileName
configFilePath := filepath.Join(UserHomeDir, info.BACKPLANE_CONFIG_DEFAULT_FILE_PATH, info.BACKPLANE_CONFIG_DEFAULT_FILE_NAME)

return configFilePath, nil
}

// Get Backplane ProxyUrl from config
func GetBackplaneConfiguration() (bpConfig BackplaneConfiguration, err error) {

// Check proxy url from the config file
filePath := GetBackplaneConfigFile()
filePath, err := GetBackplaneConfigFile()
if err != nil {
return bpConfig, err
}

if _, err := os.Stat(filePath); err == nil {
file, err := os.Open(filePath)

Expand Down
16 changes: 13 additions & 3 deletions pkg/cli/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@ import (
func TestGetBackplaneConfigFile(t *testing.T) {
t.Run("it returns the Backplane configuration file path if it exists in the user's env", func(t *testing.T) {
t.Setenv(info.BACKPLANE_CONFIG_PATH_ENV_NAME, "~/.backplane.stg.env.json")
path := GetBackplaneConfigFile()
path, err := GetBackplaneConfigFile()
if err != nil {
t.Error(err)
}
if path != "~/.backplane.stg.env.json" {
t.Errorf("expected path to be %v, got %v", "~/.backplane.stg.env.json", path)
}
})

t.Run("it returns the default configuration file path if it does not exist in the user's env", func(t *testing.T) {
path := GetBackplaneConfigFile()
expectedPath := getConfiDefaultPath(info.BACKPLANE_CONFIG_DEFAULT_FILE_NAME)
path, err := GetBackplaneConfigFile()
if err != nil {
t.Error(err)
}

expectedPath, err := getDefaultConfigPath()
if err != nil {
t.Error(err)
}
if path != expectedPath {
t.Errorf("expected path to be %v, got %v", expectedPath, path)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const (
BACKPLANE_URL_ENV_NAME = "BACKPLANE_URL"
BACKPLANE_PROXY_ENV_NAME = "HTTPS_PROXY"
BACKPLANE_CONFIG_PATH_ENV_NAME = "BACKPLANE_CONFIG"
BACKPLANE_CONFIG_DEFAULT_FILE_NAME = ".backplane.json"
BACKPLANE_CONFIG_DEFAULT_FILE_PATH = ".config/backplane"
BACKPLANE_CONFIG_DEFAULT_FILE_NAME = "config.json"

// GitHub API get fetch the latest tag
UpstreamReleaseAPI = "https://api.github.com/repos/openshift/backplane-cli/releases/latest"
Expand Down

0 comments on commit 0040057

Please sign in to comment.