Skip to content

Commit

Permalink
Homedir backstop (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeCooper authored Oct 5, 2020
1 parent 981b2bc commit 6b7b90d
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions configs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ func (c *Configs) marshalConfig(config *Config, cfg interface{}) error {
return err
}

func fetchHomeDir() (homeDir *string, err error) {
// In OSX BigSur, or terminals where the user isn't set
// user.Current cannot function
// As a backstop (We should be crosscompiling using clang)
// hoist the dirhir from env
defer func() {
if r := recover(); r != nil {
home := os.Getenv("HOME")
homeDir = &home
err = nil
}
}()
user, err := user.Current()
if err != nil {
return nil, err
}
return &user.HomeDir, nil
}

func New() *Configs {
// Configs stored in projects (<project>/.railway)
// Includes projectId, environmentId, etc
Expand All @@ -78,7 +97,7 @@ func New() *Configs {
projectPath := path.Join(projectDir, "./config.json")
projectViper.SetConfigFile(projectPath)
projectViper.ReadInConfig()
user, err := user.Current()

if err != nil {
panic(err)
}
Expand All @@ -91,7 +110,11 @@ func New() *Configs {
// Configs stored in root (~/.railway)
// Includes token, etc
userViper := viper.New()
userPath := path.Join(user.HomeDir, ".railway/config.json")
homeDir, err := fetchHomeDir()
if err != nil {
panic(err)
}
userPath := path.Join(*homeDir, ".railway/config.json")
userViper.SetConfigFile(userPath)
userViper.ReadInConfig()

Expand Down

0 comments on commit 6b7b90d

Please sign in to comment.