Skip to content

Commit

Permalink
Merge pull request openshift-online#302 from vkareh/SDA-3085/ocm-conf…
Browse files Browse the repository at this point in the history
…ig-location

config: Use standard config path for ocm.json
  • Loading branch information
vkareh authored Aug 9, 2021
2 parents 634023c + 700329b commit ba8d1e7
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func Save(cfg *Config) error {
if err != nil {
return err
}
dir := filepath.Dir(file)
err = os.MkdirAll(dir, os.FileMode(0755))
if err != nil {
return fmt.Errorf("can't create directory %s: %v", dir, err)
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return fmt.Errorf("can't marshal config: %v", err)
Expand All @@ -107,17 +112,34 @@ func Save(cfg *Config) error {
return nil
}

// Location returns the location of the configuration file.
// Location returns the location of the configuration file. If a configuration file
// already exists in the HOME directory, it uses that, otherwise it prefers to
// use the XDG config directory.
func Location() (path string, err error) {
if ocmconfig := os.Getenv("OCM_CONFIG"); ocmconfig != "" {
path = ocmconfig
} else {
home, err := homedir.Dir()
return ocmconfig, nil
}

// Determine home directory to use for the legacy file path
home, err := homedir.Dir()
if err != nil {
return "", err
}

path = filepath.Join(home, ".ocm.json")

_, err = os.Stat(path)
if os.IsNotExist(err) {
// Determine standard config directory
configDir, err := os.UserConfigDir()
if err != nil {
return "", err
return path, err
}
path = filepath.Join(home, ".ocm.json")

// Use standard config directory
path = filepath.Join(configDir, "/ocm/ocm.json")
}

return path, nil
}

Expand Down

0 comments on commit ba8d1e7

Please sign in to comment.