diff --git a/kong.go b/kong.go index 408cf44..771b820 100644 --- a/kong.go +++ b/kong.go @@ -336,7 +336,7 @@ func (k *Kong) FatalIfErrorf(err error, args ...interface{}) { // // "path" will have ~/ expanded. func (k *Kong) LoadConfig(path string) (Resolver, error) { - path = expandPath(path) + path = ExpandPath(path) r, err := os.Open(path) // nolint: gas if err != nil { return nil, err diff --git a/mapper.go b/mapper.go index 8bd7d00..d99eda9 100644 --- a/mapper.go +++ b/mapper.go @@ -372,7 +372,7 @@ func pathMapper(r *Registry) MapperFunc { return fmt.Errorf("\"path\" type must be applied to a string not %s", target.Type()) } path := ctx.Scan.PopValue("file") - path = expandPath(path) + path = ExpandPath(path) target.SetString(path) 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()) } path := ctx.Scan.PopValue("file") - path = expandPath(path) + path = ExpandPath(path) stat, err := os.Stat(path) if err != nil { 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()) } path := ctx.Scan.PopValue("file") - path = expandPath(path) + path = ExpandPath(path) stat, err := os.Stat(path) if err != nil { return err @@ -477,7 +477,7 @@ func JoinEscaped(s []string, sep rune) string { type FileContentFlag []byte 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 if err != nil { return fmt.Errorf("failed to open %q: %s", filename, err) diff --git a/options.go b/options.go index bcadf16..7cdcfbe 100644 --- a/options.go +++ b/options.go @@ -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) { return path }