Skip to content

Commit aecae7e

Browse files
committedDec 20, 2024··
Replace go-homedir with stdlib calls
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.
1 parent b51fb30 commit aecae7e

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed
 

‎go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.22
55
require (
66
github.com/gofrs/uuid/v5 v5.3.0
77
github.com/gophercloud/gophercloud/v2 v2.4.0
8-
github.com/mitchellh/go-homedir v1.1.0
98
golang.org/x/sys v0.28.0
109
golang.org/x/text v0.21.0
1110
gopkg.in/yaml.v3 v3.0.1

‎go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ github.com/gofrs/uuid/v5 v5.3.0 h1:m0mUMr+oVYUdxpMLgSYCZiXe7PuVPnI94+OMeVBNedk=
22
github.com/gofrs/uuid/v5 v5.3.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
33
github.com/gophercloud/gophercloud/v2 v2.4.0 h1:XhP5tVEH3ni66NSNK1+0iSO6kaGPH/6srtx6Cr+8eCg=
44
github.com/gophercloud/gophercloud/v2 v2.4.0/go.mod h1:uJWNpTgJPSl2gyzJqcU/pIAhFUWvIkp8eE8M15n9rs4=
5-
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
6-
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
75
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
86
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
97
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=

‎internal/util.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"crypto/x509"
77
"fmt"
88
"os"
9+
"os/user"
10+
"path/filepath"
911
"reflect"
1012
"strings"
11-
12-
"github.com/mitchellh/go-homedir"
1313
)
1414

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

9292
path := poc
9393
if path[0] == '~' {
94-
var err error
95-
path, err = homedir.Expand(path)
94+
usr, err := user.Current()
9695
if err != nil {
9796
return []byte(path), true, err
9897
}
98+
99+
if len(path) == 1 {
100+
path = usr.HomeDir
101+
} else if strings.HasPrefix(path, "~/") {
102+
path = filepath.Join(usr.HomeDir, path[2:])
103+
}
99104
}
100105

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

0 commit comments

Comments
 (0)
Please sign in to comment.