Fix relative path handling for file:// links

This commit is contained in:
Łukasz Mierzwa
2017-11-26 10:45:39 -08:00
parent b796e0cde2
commit b52ab532e6

View File

@@ -5,6 +5,9 @@ import (
"fmt"
"io"
"net/url"
"os"
"path"
"strings"
"time"
)
@@ -19,7 +22,18 @@ func ReadJSON(uri string, timeout time.Duration, target interface{}) error {
case "http", "https":
reader, err = newHTTPReader(u.String(), timeout)
case "file":
reader, err = newFileReader(u.Path)
// if we have a file URI with relative path we need to expand it into an
// absolute path, url.Parse doesn't support relative file paths
if strings.HasPrefix(uri, "file:///") {
reader, err = newFileReader(u.Path)
} else {
wd, e := os.Getwd()
if e != nil {
return e
}
absolutePath := path.Join(wd, strings.TrimPrefix(uri, "file://"))
reader, err = newFileReader(absolutePath)
}
default:
return fmt.Errorf("Unsupported URI scheme '%s' in '%s'", u.Scheme, u)
}