Skip to content

Commit 60f6e0b

Browse files
authored
Merge pull request #3385 from kairveeehh/fix
Replace usages of usr.HomeDir with os.UserHomeDir()
2 parents ee3714a + 0654474 commit 60f6e0b

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

pkg/qemu/qemu.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"io/fs"
1414
"os"
1515
"os/exec"
16-
"os/user"
1716
"path/filepath"
1817
"regexp"
1918
"runtime"
@@ -1006,16 +1005,16 @@ func FindVirtiofsd(qemuExe string) (string, error) {
10061005
Binary string `json:"binary"`
10071006
}
10081007

1009-
currentUser, err := user.Current()
1008+
homeDir, err := os.UserHomeDir()
10101009
if err != nil {
10111010
return "", err
10121011
}
10131012

10141013
const relativePath = "share/qemu/vhost-user"
10151014

1016-
binDir := filepath.Dir(qemuExe) // "/usr/local/bin"
1017-
usrDir := filepath.Dir(binDir) // "/usr/local"
1018-
userLocalDir := filepath.Join(currentUser.HomeDir, ".local") // "$HOME/.local"
1015+
binDir := filepath.Dir(qemuExe) // "/usr/local/bin"
1016+
usrDir := filepath.Dir(binDir) // "/usr/local"
1017+
userLocalDir := filepath.Join(homeDir, ".local") // "$HOME/.local"
10191018

10201019
candidates := []string{
10211020
filepath.Join(userLocalDir, relativePath),
@@ -1175,14 +1174,14 @@ func getFirmware(qemuExe string, arch limayaml.Arch) (string, error) {
11751174
return "", fmt.Errorf("unexpected architecture: %q", arch)
11761175
}
11771176

1178-
currentUser, err := user.Current()
1177+
homeDir, err := os.UserHomeDir()
11791178
if err != nil {
11801179
return "", err
11811180
}
11821181

1183-
binDir := filepath.Dir(qemuExe) // "/usr/local/bin"
1184-
localDir := filepath.Dir(binDir) // "/usr/local"
1185-
userLocalDir := filepath.Join(currentUser.HomeDir, ".local") // "$HOME/.local"
1182+
binDir := filepath.Dir(qemuExe) // "/usr/local/bin"
1183+
localDir := filepath.Dir(binDir) // "/usr/local"
1184+
userLocalDir := filepath.Join(homeDir, ".local") // "$HOME/.local"
11861185

11871186
relativePath := fmt.Sprintf("share/qemu/edk2-%s-code.fd", qemuEdk2Arch(arch))
11881187
relativePathWin := fmt.Sprintf("share/edk2-%s-code.fd", qemuEdk2Arch(arch))
@@ -1241,14 +1240,14 @@ func getFirmwareVars(qemuExe string, arch limayaml.Arch) (string, error) {
12411240
return "", fmt.Errorf("unexpected architecture: %q", arch)
12421241
}
12431242

1244-
currentUser, err := user.Current()
1243+
homeDir, err := os.UserHomeDir()
12451244
if err != nil {
12461245
return "", err
12471246
}
12481247

1249-
binDir := filepath.Dir(qemuExe) // "/usr/local/bin"
1250-
localDir := filepath.Dir(binDir) // "/usr/local"
1251-
userLocalDir := filepath.Join(currentUser.HomeDir, ".local") // "$HOME/.local"
1248+
binDir := filepath.Dir(qemuExe) // "/usr/local/bin"
1249+
localDir := filepath.Dir(binDir) // "/usr/local"
1250+
userLocalDir := filepath.Join(homeDir, ".local") // "$HOME/.local"
12521251

12531252
relativePath := fmt.Sprintf("share/qemu/edk2-%s-vars.fd", qemuEdk2Arch(targetArch))
12541253
relativePathWin := fmt.Sprintf("share/edk2-%s-vars.fd", qemuEdk2Arch(targetArch))

pkg/store/instance.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"fmt"
1010
"io"
1111
"os"
12-
"os/user"
1312
"path/filepath"
1413
"runtime"
1514
"strconv"
@@ -355,11 +354,10 @@ func PrintInstances(w io.Writer, instances []*Instance, format string, options *
355354
}
356355
fmt.Fprintln(w)
357356

358-
u, err := user.Current()
357+
homeDir, err := os.UserHomeDir()
359358
if err != nil {
360359
return err
361360
}
362-
homeDir := u.HomeDir
363361

364362
for _, instance := range instances {
365363
dir := instance.Dir

pkg/store/instance_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package store
55

66
import (
77
"bytes"
8-
"os/user"
8+
"os"
99
"path/filepath"
1010
"runtime"
1111
"strings"
@@ -85,10 +85,10 @@ func TestPrintInstanceTableEmu(t *testing.T) {
8585

8686
func TestPrintInstanceTableHome(t *testing.T) {
8787
var buf bytes.Buffer
88-
u, err := user.Current()
88+
homeDir, err := os.UserHomeDir()
8989
assert.NilError(t, err)
9090
instance1 := instance
91-
instance1.Dir = filepath.Join(u.HomeDir, "dir")
91+
instance1.Dir = filepath.Join(homeDir, "dir")
9292
instances := []*Instance{&instance1}
9393
err = PrintInstances(&buf, instances, "table", nil)
9494
assert.NilError(t, err)

0 commit comments

Comments
 (0)