Skip to content

Commit

Permalink
set config default location to xdg
Browse files Browse the repository at this point in the history
  • Loading branch information
Dolev Hadar authored and dlvhdr committed May 28, 2022
1 parent a0aa083 commit 1d6aa72
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

const DashDir = "gh-dash"
const ConfigFileName = "config.yml"
const DEFAULT_XDG_CONFIG_DIRNAME = ".config"

type ViewType string

Expand Down Expand Up @@ -157,23 +158,64 @@ func (parser ConfigParser) createConfigFileIfMissing(configFilePath string) erro
return nil
}

func (parser ConfigParser) getExistingConfigFile() (*string, error) {
var err error
var dashConfigFile string
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}

xdgConfigDir := os.Getenv("XDG_CONFIG_HOME")
if xdgConfigDir == "" {
xdgConfigDir = filepath.Join(homeDir, DEFAULT_XDG_CONFIG_DIRNAME)
}

dashConfigFile = filepath.Join(xdgConfigDir, DashDir, ConfigFileName)
if _, err := os.Stat(dashConfigFile); err == nil {
return &dashConfigFile, nil
}

userConfigDir, err := os.UserConfigDir()
if err != nil {
return nil, err
}

dashConfigFile = filepath.Join(userConfigDir, DashDir, ConfigFileName)
if _, err := os.Stat(dashConfigFile); err == nil {
return &dashConfigFile, nil
}

return nil, nil
}

func (parser ConfigParser) getConfigFileOrCreateIfMissing() (*string, error) {
var err error

existingConfigFile, err := parser.getExistingConfigFile()
if err != nil {
return nil, err
}
if existingConfigFile != nil {
return existingConfigFile, nil
}

configDir := os.Getenv("XDG_CONFIG_HOME")
if configDir == "" {
configDir, err = os.UserConfigDir()
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, configError{parser: parser, configDir: configDir, err: err}
return nil, err
}
configDir = filepath.Join(homeDir, DEFAULT_XDG_CONFIG_DIRNAME)
}

prsConfigDir := filepath.Join(configDir, DashDir)
err = os.MkdirAll(prsConfigDir, os.ModePerm)
dashConfigDir := filepath.Join(configDir, DashDir)
err = os.MkdirAll(dashConfigDir, os.ModePerm)
if err != nil {
return nil, configError{parser: parser, configDir: configDir, err: err}
}

configFilePath := filepath.Join(prsConfigDir, ConfigFileName)
configFilePath := filepath.Join(dashConfigDir, ConfigFileName)
err = parser.createConfigFileIfMissing(configFilePath)
if err != nil {
return nil, configError{parser: parser, configDir: configDir, err: err}
Expand Down

0 comments on commit 1d6aa72

Please sign in to comment.