From 455f27e548a425549c05277aff7ba2a449748353 Mon Sep 17 00:00:00 2001 From: tirthct Date: Wed, 11 Oct 2023 07:45:21 -0700 Subject: [PATCH] OCM-4184 | Feat | Convert relative path containing tilde for service account file (#568) --- pkg/arguments/interactive.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pkg/arguments/interactive.go b/pkg/arguments/interactive.go index f7cf9d08..d3b56298 100644 --- a/pkg/arguments/interactive.go +++ b/pkg/arguments/interactive.go @@ -20,6 +20,7 @@ package arguments import ( "fmt" + "os/user" "path/filepath" "strconv" "strings" @@ -280,12 +281,32 @@ func PromptFilePath(fs *pflag.FlagSet, flagName string, required bool) error { if err != nil { return err } + response, err = resolveRelativePath(response) + if err != nil { + return err + } fs.Set(flagName, response) } return nil }) } +// Golang does not support tilde file paths https://github.com/golang/go/issues/57569 +// However, we try to resolve this by manually so user can proceed further +func resolveRelativePath(path string) (string, error) { + if !strings.Contains(path, "~") { + return path, nil + } + usr, err := user.Current() + if err != nil { + return "", err + } + if strings.HasPrefix(path, "~/") { + path = strings.Replace(path, "~", usr.HomeDir, 1) + } + return path, nil +} + // PromptIPNet sets an optional IPNet flag value interactively, unless already set. // Does nothing in non-interactive mode. func PromptIPNet(fs *pflag.FlagSet, flagName string) error {