Skip to content

Commit

Permalink
Replace go-homedir with stdlib calls
Browse files Browse the repository at this point in the history
The purpose of github.com/mitchellh/go-homedir was to expand the user
home directory without CGO. Recent versions of `os/user` can retrieve
the user home directory without CGO and thus enable cross-compilation.

This change does not add the missing functionality of expanding
non-current-user home directories in a path (e.g. `~user2/dir/subdir`).
The reason for this is that the input of `pathOrContents` could be a
blob and parsing every byte slice matching `^~.*\/.*` as a path
considerably increases the chances that a binary blob is mistakenly
parsed as a path.
  • Loading branch information
pierreprinetti committed Dec 20, 2024
1 parent f67ecca commit 3d15577
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.22
require (
github.com/gofrs/uuid/v5 v5.3.0
github.com/gophercloud/gophercloud/v2 v2.3.0
github.com/mitchellh/go-homedir v1.1.0
golang.org/x/sys v0.28.0
golang.org/x/text v0.21.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/gofrs/uuid/v5 v5.3.0 h1:m0mUMr+oVYUdxpMLgSYCZiXe7PuVPnI94+OMeVBNedk=
github.com/gofrs/uuid/v5 v5.3.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
github.com/gophercloud/gophercloud/v2 v2.3.0 h1:5ipI2Mgxee0TwQxqnOIUdTbzL4ZBB8GORyZko+yGXI0=
github.com/gophercloud/gophercloud/v2 v2.3.0/go.mod h1:uJWNpTgJPSl2gyzJqcU/pIAhFUWvIkp8eE8M15n9rs4=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
Expand Down
13 changes: 9 additions & 4 deletions internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"crypto/x509"
"fmt"
"os"
"os/user"
"path/filepath"
"reflect"
"strings"

"github.com/mitchellh/go-homedir"
)

// RemainingKeys will inspect a struct and compare it to a map. Any struct
Expand Down Expand Up @@ -91,11 +91,16 @@ func pathOrContents(poc string) ([]byte, bool, error) {

path := poc
if path[0] == '~' {
var err error
path, err = homedir.Expand(path)
usr, err := user.Current()
if err != nil {
return []byte(path), true, err
}

if len(path) == 1 {
path = usr.HomeDir
} else if strings.HasPrefix(path, "~/") {
path = filepath.Join(usr.HomeDir, path[2:])
}
}

if _, err := os.Stat(path); err == nil {
Expand Down

0 comments on commit 3d15577

Please sign in to comment.