Skip to content

Commit f24ae3f

Browse files
committed
dev: remove archived mitchellh/go-homedir
1 parent 97987f9 commit f24ae3f

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ require (
7777
github.com/matoous/godox v0.0.0-20230222163458-006bad1f9d26
7878
github.com/mattn/go-colorable v0.1.13
7979
github.com/mgechev/revive v1.5.1
80-
github.com/mitchellh/go-homedir v1.1.0
8180
github.com/mitchellh/go-ps v1.0.0
8281
github.com/moricho/tparallel v0.3.2
8382
github.com/nakabonne/nestif v0.3.1

go.sum

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/loader.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"os"
88
"path/filepath"
99
"slices"
10+
"strings"
1011

1112
"github.com/go-viper/mapstructure/v2"
12-
"github.com/mitchellh/go-homedir"
1313
"github.com/spf13/pflag"
1414
"github.com/spf13/viper"
1515

@@ -129,9 +129,9 @@ func (l *Loader) evaluateOptions() (string, error) {
129129
return "", errConfigDisabled
130130
}
131131

132-
configFile, err := homedir.Expand(l.opts.Config)
132+
configFile, err := expandHomeDir(l.opts.Config)
133133
if err != nil {
134-
return "", errors.New("failed to expand configuration path")
134+
return "", fmt.Errorf("failed to expand configuration path: %w", err)
135135
}
136136

137137
return configFile, nil
@@ -184,7 +184,7 @@ func (l *Loader) getConfigSearchPaths() []string {
184184
}
185185

186186
// find home directory for global config
187-
if home, err := homedir.Dir(); err != nil {
187+
if home, err := os.UserHomeDir(); err != nil {
188188
l.log.Warnf("Can't get user's home directory: %v", err)
189189
} else if !slices.Contains(searchPaths, home) {
190190
searchPaths = append(searchPaths, home)
@@ -473,3 +473,25 @@ func customDecoderHook() viper.DecoderConfigOption {
473473
mapstructure.TextUnmarshallerHookFunc(),
474474
))
475475
}
476+
477+
// expandHomeDir expands file paths relative to the user's home directory (~) into absolute paths.
478+
func expandHomeDir(path string) (string, error) {
479+
if !strings.HasPrefix(path, "~") {
480+
return path, nil
481+
}
482+
483+
homeDir, err := os.UserHomeDir()
484+
if err != nil {
485+
return "", err
486+
}
487+
488+
if path == "~" {
489+
return homeDir, nil
490+
}
491+
492+
if !strings.HasPrefix(path, "~"+string(filepath.Separator)) {
493+
return "", errors.New("cannot expand user-specific home dir")
494+
}
495+
496+
return filepath.Join(homeDir, strings.TrimPrefix(path, "~")), nil
497+
}

pkg/config/loader_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package config
2+
3+
import (
4+
"os/user"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func Test_expandHomeDir(t *testing.T) {
13+
u, err := user.Current()
14+
require.NoError(t, err)
15+
16+
testCases := []struct {
17+
path string
18+
expected string
19+
expectedErr bool
20+
}{
21+
{path: "", expected: ""},
22+
{path: "~", expected: u.HomeDir},
23+
{path: "/foo", expected: "/foo"},
24+
{path: "\\foo", expected: "\\foo"},
25+
{path: "C:\foo", expected: "C:\foo"},
26+
{path: "~/foo/bar", expected: filepath.Join(u.HomeDir, "foo", "bar")},
27+
{path: "~foo/foo", expectedErr: true},
28+
}
29+
30+
for _, tc := range testCases {
31+
actual, err := expandHomeDir(tc.path)
32+
33+
if tc.expectedErr {
34+
require.Error(t, err)
35+
return
36+
}
37+
require.NoError(t, err)
38+
assert.Equal(t, tc.expected, actual)
39+
}
40+
}

0 commit comments

Comments
 (0)