Skip to content

Commit

Permalink
OCM-4184 | Feat | Convert relative path containing tilde for service …
Browse files Browse the repository at this point in the history
…account file (#568)
  • Loading branch information
tirthct authored Oct 11, 2023
1 parent fcc6851 commit 455f27e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/arguments/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package arguments

import (
"fmt"
"os/user"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 455f27e

Please sign in to comment.