Skip to content

Commit

Permalink
Implement UserHomeDir() based on the archived go-homedir package
Browse files Browse the repository at this point in the history
  • Loading branch information
folliehiyuki committed Jan 8, 2025
1 parent 8f37086 commit 744b32d
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 18 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
3 changes: 2 additions & 1 deletion cmd/pro/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,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 @@ -1692,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 := os.UserHomeDir()
home, err := util.UserHomeDir()
if err != nil {
home = "error"
if log != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/agent/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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/moby/patternmatcher/ignorefile"
)
Expand Down Expand Up @@ -49,7 +50,7 @@ func findDir(agentFolder string, validate func(path string) bool) string {
}

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

import (
"os"
"os/user"

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

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

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

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

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

homeDir, err := os.UserHomeDir()
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 @@ -5,11 +5,11 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"strings"

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

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 := os.UserHomeDir()
home, err := util.UserHomeDir()
if err != nil {
home = "error"
if log != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/ide/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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/pkg/errors"
Expand Down Expand Up @@ -214,7 +215,7 @@ func prepareFleetServerLocation(userName string) (string, error) {
if userName != "" {
homeFolder, err = command.GetHome(userName)
} else {
homeFolder, err = os.UserHomeDir()
homeFolder, err = util.UserHomeDir()
}
if err != nil {
return "", err
Expand Down
3 changes: 2 additions & 1 deletion pkg/ide/jetbrains/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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/pkg/errors"
"github.com/skratchdot/open-golang/open"
Expand Down Expand Up @@ -133,7 +134,7 @@ func getBaseFolder(userName string) (string, error) {
if userName != "" {
homeFolder, err = command.GetHome(userName)
} else {
homeFolder, err = os.UserHomeDir()
homeFolder, err = util.UserHomeDir()
}
if err != nil {
return "", err
Expand Down
3 changes: 2 additions & 1 deletion pkg/ide/openvscode/openvscode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -279,7 +280,7 @@ func prepareOpenVSCodeServerLocation(userName string) (string, error) {
if userName != "" {
homeFolder, err = command.GetHome(userName)
} else {
homeFolder, err = os.UserHomeDir()
homeFolder, err = util.UserHomeDir()
}
if err != nil {
return "", err
Expand Down
3 changes: 2 additions & 1 deletion pkg/ide/vscode/vscode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -420,7 +421,7 @@ func prepareServerLocation(userName string, create bool, flavor Flavor) (string,
if userName != "" {
homeFolder, err = command.GetHome(userName)
} else {
homeFolder, err = os.UserHomeDir()
homeFolder, err = util.UserHomeDir()
}
if err != nil {
return "", err
Expand Down
3 changes: 2 additions & 1 deletion pkg/platform/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ 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"
perrors "github.com/pkg/errors"
Expand All @@ -47,7 +48,7 @@ const (
)

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

"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
"github.com/loft-sh/log/scanner"
"github.com/pkg/errors"
Expand Down Expand Up @@ -185,7 +186,7 @@ func writeSSHConfig(path, content string, log log.Logger) error {
}

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

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

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

func GetDevPodKeysDir() string {
dir, err := os.UserHomeDir()
dir, err := util.UserHomeDir()
if err == nil {
tempDir := filepath.Join(dir, ".devpod", "keys")
err = os.MkdirAll(tempDir, 0755)
Expand Down
3 changes: 2 additions & 1 deletion pkg/ssh/ssh_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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"
"golang.org/x/crypto/ssh"
)
Expand Down Expand Up @@ -40,7 +41,7 @@ func AddPrivateKeysToAgent(ctx context.Context, log log.Logger) error {
}

func FindPrivateKeys() ([]string, error) {
homeDir, err := os.UserHomeDir()
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 @@ -4,9 +4,9 @@ import (
"crypto/hmac"
"crypto/sha256"
"fmt"
"os"

"github.com/denisbrodbeck/machineid"
"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 := os.UserHomeDir()
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 err != exec.ErrNotFound {

Check failure on line 58 in pkg/util/homedir.go

View workflow job for this annotation

GitHub Actions / lint

comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
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 744b32d

Please sign in to comment.