Skip to content

Commit

Permalink
Replace the usage of go-homedir with os.UserHomeDir() (#1521)
Browse files Browse the repository at this point in the history
* Replace the usage of go-homedir with os.UserHomeDir()

Ref: mitchellh/go-homedir#34

* Implement UserHomeDir() based on the archived go-homedir package

* chore(cli): address ci linting issue

---------

Co-authored-by: Pascal Breuninger <[email protected]>
  • Loading branch information
folliehiyuki and pascalbreuninger authored Jan 8, 2025
1 parent dd3c94d commit 3d19e41
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 30 deletions.
3 changes: 2 additions & 1 deletion cmd/agent/workspace/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/loft-sh/devpod/pkg/dockercredentials"
"github.com/loft-sh/devpod/pkg/extract"
provider2 "github.com/loft-sh/devpod/pkg/provider"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/devpod/scripts"
"github.com/loft-sh/log"
"github.com/pkg/errors"
Expand Down Expand Up @@ -451,7 +452,7 @@ func configureDockerDaemon(ctx context.Context, log log.Logger) (err error) {
}
}`)
// Check rootless docker
homeDir, err := os.UserHomeDir()
homeDir, err := util.UserHomeDir()
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/pro/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/denisbrodbeck/machineid"
jsonpatch "github.com/evanphx/json-patch"
"github.com/mgutz/ansi"
"github.com/mitchellh/go-homedir"
"github.com/skratchdot/open-golang/open"

storagev1 "github.com/loft-sh/api/v4/pkg/apis/storage/v1"
Expand All @@ -30,6 +29,7 @@ import (
proflags "github.com/loft-sh/devpod/cmd/pro/flags"
"github.com/loft-sh/devpod/pkg/platform"
"github.com/loft-sh/devpod/pkg/platform/client"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
"github.com/loft-sh/log/hash"
"github.com/loft-sh/log/scanner"
Expand Down Expand Up @@ -1693,7 +1693,7 @@ func getMachineUID(log log.Logger) string {
}
// get $HOME to distinguish two users on the same machine
// will be hashed later together with the ID
home, err := homedir.Dir()
home, err := util.UserHomeDir()
if err != nil {
home = "error"
if log != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ require (
github.com/loft-sh/ssh v0.0.5
github.com/mattn/go-isatty v0.0.20
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
github.com/mitchellh/go-homedir v1.1.0
github.com/moby/buildkit v0.18.0
github.com/onsi/ginkgo/v2 v2.20.2
github.com/onsi/gomega v1.34.2
Expand Down Expand Up @@ -160,6 +159,7 @@ require (
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/loft-sh/devpod/pkg/git"
"github.com/loft-sh/devpod/pkg/gitcredentials"
provider2 "github.com/loft-sh/devpod/pkg/provider"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
"github.com/mitchellh/go-homedir"
"github.com/moby/patternmatcher/ignorefile"
)

Expand Down Expand Up @@ -50,7 +50,7 @@ func findDir(agentFolder string, validate func(path string) bool) string {
}

// check home folder first
homeDir, _ := homedir.Dir()
homeDir, _ := util.UserHomeDir()
if homeDir != "" {
homeDir = filepath.Join(homeDir, ".devpod", "agent")
if validate(homeDir) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/command/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package command
import (
"os/user"

"github.com/mitchellh/go-homedir"
"github.com/loft-sh/devpod/pkg/util"
)

func GetHome(userName string) (string, error) {
if userName == "" {
return homedir.Dir()
return util.UserHomeDir()
}

u, err := user.Lookup(userName)
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"path/filepath"

homedir "github.com/mitchellh/go-homedir"
"github.com/loft-sh/devpod/pkg/util"
)

// Override devpod home
Expand All @@ -19,7 +19,7 @@ func GetConfigDir() (string, error) {
return homeDir, nil
}

homeDir, err := homedir.Dir()
homeDir, err := util.UserHomeDir()
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/denisbrodbeck/machineid"
"github.com/google/uuid"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
"github.com/mitchellh/go-homedir"
)

const (
Expand Down Expand Up @@ -81,7 +81,7 @@ func GetMachineUID(log log.Logger) string {
}
// get $HOME to distinguish two users on the same machine
// will be hashed later together with the ID
home, err := homedir.Dir()
home, err := util.UserHomeDir()
if err != nil {
home = "error"
if log != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/ide/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
devpodhttp "github.com/loft-sh/devpod/pkg/http"
"github.com/loft-sh/devpod/pkg/ide"
"github.com/loft-sh/devpod/pkg/single"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
"github.com/loft-sh/log/scanner"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -215,7 +215,7 @@ func prepareFleetServerLocation(userName string) (string, error) {
if userName != "" {
homeFolder, err = command.GetHome(userName)
} else {
homeFolder, err = homedir.Dir()
homeFolder, err = util.UserHomeDir()
}
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions pkg/ide/jetbrains/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"github.com/loft-sh/devpod/pkg/extract"
devpodhttp "github.com/loft-sh/devpod/pkg/http"
"github.com/loft-sh/devpod/pkg/ide"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/skratchdot/open-golang/open"
)
Expand Down Expand Up @@ -134,7 +134,7 @@ func getBaseFolder(userName string) (string, error) {
if userName != "" {
homeFolder, err = command.GetHome(userName)
} else {
homeFolder, err = homedir.Dir()
homeFolder, err = util.UserHomeDir()
}
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions pkg/ide/openvscode/openvscode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/loft-sh/devpod/pkg/ide"
"github.com/loft-sh/devpod/pkg/ide/vscode"
"github.com/loft-sh/devpod/pkg/single"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -280,7 +280,7 @@ func prepareOpenVSCodeServerLocation(userName string) (string, error) {
if userName != "" {
homeFolder, err = command.GetHome(userName)
} else {
homeFolder, err = homedir.Dir()
homeFolder, err = util.UserHomeDir()
}
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions pkg/ide/vscode/vscode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/loft-sh/devpod/pkg/config"
copy2 "github.com/loft-sh/devpod/pkg/copy"
"github.com/loft-sh/devpod/pkg/ide"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -421,7 +421,7 @@ func prepareServerLocation(userName string, create bool, flavor Flavor) (string,
if userName != "" {
homeFolder, err = command.GetHome(userName)
} else {
homeFolder, err = homedir.Dir()
homeFolder, err = util.UserHomeDir()
}
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions pkg/platform/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (

"github.com/loft-sh/devpod/pkg/platform/kube"
"github.com/loft-sh/devpod/pkg/platform/project"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/devpod/pkg/version"
"github.com/loft-sh/log"
"github.com/mitchellh/go-homedir"
perrors "github.com/pkg/errors"
"github.com/skratchdot/open-golang/open"
"k8s.io/client-go/rest"
Expand All @@ -48,7 +48,7 @@ const (
)

func init() {
hd, _ := homedir.Dir()
hd, _ := util.UserHomeDir()
if folder, ok := os.LookupEnv("LOFT_CACHE_FOLDER"); ok {
CacheFolder = filepath.Join(hd, folder)
} else {
Expand Down
4 changes: 2 additions & 2 deletions pkg/ssh/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"strings"
"sync"

"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
"github.com/loft-sh/log/scanner"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -186,7 +186,7 @@ func writeSSHConfig(path, content string, log log.Logger) error {
}

func ResolveSSHConfigPath(sshConfigPath string) (string, error) {
homeDir, err := homedir.Dir()
homeDir, err := util.UserHomeDir()
if err != nil {
return "", errors.Wrap(err, "get home dir")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ssh/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"sync"

"github.com/loft-sh/devpod/pkg/provider"
"github.com/mitchellh/go-homedir"
"github.com/loft-sh/devpod/pkg/util"

"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
Expand Down Expand Up @@ -80,7 +80,7 @@ func GetPrivateKeyRaw(context, workspaceID string) ([]byte, error) {
}

func GetDevPodKeysDir() string {
dir, err := homedir.Dir()
dir, err := util.UserHomeDir()
if err == nil {
tempDir := filepath.Join(dir, ".devpod", "keys")
err = os.MkdirAll(tempDir, 0755)
Expand Down
4 changes: 2 additions & 2 deletions pkg/ssh/ssh_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/loft-sh/devpod/pkg/command"
devsshagent "github.com/loft-sh/devpod/pkg/ssh/agent"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
"github.com/mitchellh/go-homedir"
"golang.org/x/crypto/ssh"
)

Expand Down Expand Up @@ -41,7 +41,7 @@ func AddPrivateKeysToAgent(ctx context.Context, log log.Logger) error {
}

func FindPrivateKeys() ([]string, error) {
homeDir, err := homedir.Dir()
homeDir, err := util.UserHomeDir()
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/telemetry/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"

"github.com/denisbrodbeck/machineid"
"github.com/mitchellh/go-homedir"
"github.com/loft-sh/devpod/pkg/util"
)

// GetMachineID retrieves machine ID and encodes it together with users $HOME path and
Expand All @@ -19,7 +19,7 @@ func GetMachineID() string {

// get $HOME to distinguish two users on the same machine
// will be hashed later together with the ID
home, err := homedir.Dir()
home, err := util.UserHomeDir()
if err != nil {
home = "error"
}
Expand Down
90 changes: 90 additions & 0 deletions pkg/util/homedir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package util

import (
"bytes"
"errors"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
)

// UserHomeDir returns the home directory for the executing user.
//
// This extends the logic of os.UserHomeDir() with the now archived package
// github.com/mitchellh/go-homedir for compatibility.
func UserHomeDir() (string, error) {
// Always try the HOME environment variable first
homeEnv := "HOME"
if runtime.GOOS == "plan9" {
homeEnv = "home"
}
if home := os.Getenv(homeEnv); home != "" {
return home, nil
}

// Rely on os.UserHomeDir() here, as it's the standard method moving forward
if home, _ := os.UserHomeDir(); home != "" {
return home, nil
}

var stdout bytes.Buffer

// Finally, handle cases existed in go-homedir but not in the current
// os.UserHomeDir() implementation
switch runtime.GOOS {
case "windows":
drive := os.Getenv("HOMEDRIVE")
path := os.Getenv("HOMEPATH")
if drive == "" || path == "" {
return "", errors.New("HOMEDRIVE, HOMEPATH, or USERPROFILE are blank")
}
return drive + path, nil
case "darwin":
cmd := exec.Command("sh", "-c", `dscl -q . -read /Users/"$(whoami)" NFSHomeDirectory | sed 's/^[^ ]*: //'`)
cmd.Stdout = &stdout
if err := cmd.Run(); err == nil {
result := strings.TrimSpace(stdout.String())
if result != "" {
return result, nil
}
}
default:
cmd := exec.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
// If the error is ErrNotFound, we ignore it. Otherwise, return it.
if errors.Is(err, exec.ErrNotFound) {
return "", err
}
} else {
if passwd := strings.TrimSpace(stdout.String()); passwd != "" {
// username:password:uid:gid:gecos:home:shell
passwdParts := strings.SplitN(passwd, ":", 7)
if len(passwdParts) > 5 {
return passwdParts[5], nil
}
}
}
}

// If all else fails, try the shell
if runtime.GOOS != "windows" {
stdout.Reset()
cmd := exec.Command("sh", "-c", "cd && pwd")
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", err
}

result := strings.TrimSpace(stdout.String())
if result == "" {
return "", errors.New("blank output when reading home directory")
}

return result, nil
}

return "", errors.New("can't determine the home directory")
}
Loading

0 comments on commit 3d19e41

Please sign in to comment.