From b08d1ad591ac7258cc60a3b86090940122c079fd Mon Sep 17 00:00:00 2001 From: Jake Cooper Date: Mon, 5 Oct 2020 15:55:59 -0700 Subject: [PATCH] Homedir backstop --- configs/main.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/configs/main.go b/configs/main.go index aaa4019c3..a2725ec98 100644 --- a/configs/main.go +++ b/configs/main.go @@ -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 (/.railway) // Includes projectId, environmentId, etc @@ -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) } @@ -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()