From b51fb3098fe34f21a5644facffb3c84a095a52ac Mon Sep 17 00:00:00 2001 From: Pierre Prinetti Date: Fri, 20 Dec 2024 10:53:20 +0100 Subject: [PATCH 1/2] Remove deprecated ioutil --- internal/util.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/util.go b/internal/util.go index 887bb8f3..97205ef5 100644 --- a/internal/util.go +++ b/internal/util.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" "os" "reflect" "strings" @@ -100,7 +99,7 @@ func pathOrContents(poc string) ([]byte, bool, error) { } if _, err := os.Stat(path); err == nil { - contents, err := ioutil.ReadFile(path) + contents, err := os.ReadFile(path) if err != nil { return contents, true, err } From aecae7e742b6b42a7d3b815ea6e69ae61a28f043 Mon Sep 17 00:00:00 2001 From: Pierre Prinetti Date: Fri, 20 Dec 2024 11:17:10 +0100 Subject: [PATCH 2/2] 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. --- go.mod | 1 - go.sum | 2 -- internal/util.go | 13 +++++++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index a91756e7..da7855ab 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.22 require ( github.com/gofrs/uuid/v5 v5.3.0 github.com/gophercloud/gophercloud/v2 v2.4.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 diff --git a/go.sum b/go.sum index 3029d895..bc565351 100644 --- a/go.sum +++ b/go.sum @@ -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.4.0 h1:XhP5tVEH3ni66NSNK1+0iSO6kaGPH/6srtx6Cr+8eCg= github.com/gophercloud/gophercloud/v2 v2.4.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= diff --git a/internal/util.go b/internal/util.go index 97205ef5..029c7013 100644 --- a/internal/util.go +++ b/internal/util.go @@ -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 @@ -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 {