Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-resolve-home-v1 #4519

Merged
merged 4 commits into from Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 1 addition & 8 deletions internal/shareddefaults/shared_config.go
@@ -1,9 +1,7 @@
package shareddefaults

import (
"os"
"path/filepath"
"runtime"
)

// SharedCredentialsFilename returns the SDK's default file path
Expand Down Expand Up @@ -31,10 +29,5 @@ func SharedConfigFilename() string {
// UserHomeDir returns the home directory for the user the process is
// running under.
func UserHomeDir() string {
if runtime.GOOS == "windows" { // Windows
return os.Getenv("USERPROFILE")
}

// *nix
return os.Getenv("HOME")
return userHomeDir()
RanVaknin marked this conversation as resolved.
Show resolved Hide resolved
}
18 changes: 18 additions & 0 deletions internal/shareddefaults/shared_config_resolve_home.go
@@ -0,0 +1,18 @@
//go:build !go1.12
// +build !go1.12

package shareddefaults

import (
"os"
"runtime"
)

func userHomeDir() string {
if runtime.GOOS == "windows" { // Windows
return os.Getenv("USERPROFILE")
}

// *nix
return os.Getenv("HOME")
}
24 changes: 24 additions & 0 deletions internal/shareddefaults/shared_config_resolve_home_go1.12.go
@@ -0,0 +1,24 @@
//go:build go1.12
// +build go1.12

package shareddefaults

import (
"os"
"os/user"
)

func userHomeDir() string {
home, _ := os.UserHomeDir()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not fix the problem with #4444, since os.UserHomeDir is also based on $HOME.
Test program to demonstrate:

// go build test.go
// env -i ./test
package main                                                                    
                                                                                
import (                                                                        
        "fmt"                                                                   
        "os"                                                                    
)                                                                               
                                                                                
func main() {                                                                   
        home, err := os.UserHomeDir()                                           
        fmt.Println(home,  err)                                            // prints "$HOME not defined" if variable is empty
}  

Better solution:

package main                                                                    
                                                                                
import (                                                                        
        "fmt"                                                                   
        "os/user"                                                               
)                                                                               
                                                                                
func main() {                                                                   
        user, err := user.Current()                                             
        fmt.Println(user.HomeDir, err)                  // Prints home directory even when running under env -i or $HOME unset.
}                                                                               

The user.Current() is based on getpwent and thus is independent of the $HOME variable.


if len(home) > 0 {
return home
}

currUser, _ := user.Current()
if currUser != nil {
home = currUser.HomeDir
}

return home
RanVaknin marked this conversation as resolved.
Show resolved Hide resolved
}