Make ExpandPath public.

This commit is contained in:
Alec Thomas
2018-11-06 11:37:26 +11:00
parent e474873f84
commit 072357f8ba
3 changed files with 9 additions and 6 deletions
+1 -1
View File
@@ -336,7 +336,7 @@ func (k *Kong) FatalIfErrorf(err error, args ...interface{}) {
// //
// "path" will have ~/ expanded. // "path" will have ~/ expanded.
func (k *Kong) LoadConfig(path string) (Resolver, error) { func (k *Kong) LoadConfig(path string) (Resolver, error) {
path = expandPath(path) path = ExpandPath(path)
r, err := os.Open(path) // nolint: gas r, err := os.Open(path) // nolint: gas
if err != nil { if err != nil {
return nil, err return nil, err
+4 -4
View File
@@ -372,7 +372,7 @@ func pathMapper(r *Registry) MapperFunc {
return fmt.Errorf("\"path\" type must be applied to a string not %s", target.Type()) return fmt.Errorf("\"path\" type must be applied to a string not %s", target.Type())
} }
path := ctx.Scan.PopValue("file") path := ctx.Scan.PopValue("file")
path = expandPath(path) path = ExpandPath(path)
target.SetString(path) target.SetString(path)
return nil return nil
} }
@@ -387,7 +387,7 @@ func existingFileMapper(r *Registry) MapperFunc {
return fmt.Errorf("\"existingfile\" type must be applied to a string not %s", target.Type()) return fmt.Errorf("\"existingfile\" type must be applied to a string not %s", target.Type())
} }
path := ctx.Scan.PopValue("file") path := ctx.Scan.PopValue("file")
path = expandPath(path) path = ExpandPath(path)
stat, err := os.Stat(path) stat, err := os.Stat(path)
if err != nil { if err != nil {
return err return err
@@ -409,7 +409,7 @@ func existingDirMapper(r *Registry) MapperFunc {
return fmt.Errorf("\"existingdir\" must be applied to a string not %s", target.Type()) return fmt.Errorf("\"existingdir\" must be applied to a string not %s", target.Type())
} }
path := ctx.Scan.PopValue("file") path := ctx.Scan.PopValue("file")
path = expandPath(path) path = ExpandPath(path)
stat, err := os.Stat(path) stat, err := os.Stat(path)
if err != nil { if err != nil {
return err return err
@@ -477,7 +477,7 @@ func JoinEscaped(s []string, sep rune) string {
type FileContentFlag []byte type FileContentFlag []byte
func (f *FileContentFlag) Decode(ctx *DecodeContext) error { // nolint: golint func (f *FileContentFlag) Decode(ctx *DecodeContext) error { // nolint: golint
filename := expandPath(ctx.Scan.PopValue("filename")) filename := ExpandPath(ctx.Scan.PopValue("filename"))
data, err := ioutil.ReadFile(filename) // nolint: gosec data, err := ioutil.ReadFile(filename) // nolint: gosec
if err != nil { if err != nil {
return fmt.Errorf("failed to open %q: %s", filename, err) return fmt.Errorf("failed to open %q: %s", filename, err)
+4 -1
View File
@@ -211,7 +211,10 @@ func Configuration(loader ConfigurationLoader, paths ...string) OptionFunc {
} }
} }
func expandPath(path string) string { // ExpandPath is a helper function to expand a relative or home-relative path to an absolute path.
//
// eg. ~/.someconf -> /home/alec/.someconf
func ExpandPath(path string) string {
if filepath.IsAbs(path) { if filepath.IsAbs(path) {
return path return path
} }