config/module: fix URL file path handling on Windows

Only adjust the URL Scheme when parsing drive letter file paths on
Windows, don't add a file scheme prefix.
FileDetector is responsible for adding the file scheme prefix.
This commit is contained in:
Emil Hessman 2015-02-02 12:19:26 +01:00
parent 2d9dd25493
commit 5bbfc0d4e2
2 changed files with 56 additions and 57 deletions

View File

@ -2,7 +2,6 @@ package module
import ( import (
"fmt" "fmt"
"net/url"
"path/filepath" "path/filepath"
) )
@ -67,7 +66,7 @@ func Detect(src string, pwd string) (string, error) {
} }
} }
if subDir != "" { if subDir != "" {
u, err := url.Parse(result) u, err := urlParse(result)
if err != nil { if err != nil {
return "", fmt.Errorf("Error parsing URL: %s", err) return "", fmt.Errorf("Error parsing URL: %s", err)
} }

View File

@ -1,55 +1,55 @@
package module package module
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"path/filepath" "path/filepath"
"runtime" "runtime"
) )
func urlParse(rawURL string) (*url.URL, error) { func urlParse(rawURL string) (*url.URL, error) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
if len(rawURL) > 1 && rawURL[1] == ':' { // Make sure we're using "/" on Windows. URLs are "/"-based.
// Assume we're dealing with a file path. rawURL = filepath.ToSlash(rawURL)
rawURL = fmtFileURL(rawURL) }
} else { u, err := url.Parse(rawURL)
// Make sure we're using "/" on Windows. URLs are "/"-based. if err != nil {
rawURL = filepath.ToSlash(rawURL) return nil, err
} }
}
u, err := url.Parse(rawURL) if runtime.GOOS != "windows" {
if err != nil { return u, err
return nil, err }
}
if len(rawURL) > 1 && rawURL[1] == ':' {
if runtime.GOOS != "windows" { // Assume we're dealing with a drive letter file path on Windows.
return u, err // We need to adjust the URL Path for drive letter file paths
} // because url.Parse("c:/users/user") yields URL Scheme = "c"
// and URL path = "/users/user".
if u.Scheme != "file" { u.Path = fmt.Sprintf("%s:%s", u.Scheme, u.Path)
return u, err u.Scheme = ""
} }
// Remove leading slash for absolute file paths on Windows. // Remove leading slash for absolute file paths on Windows.
// For example, url.Parse yields u.Path = "/C:/Users/user" for // For example, url.Parse yields u.Path = "/C:/Users/user" for
// rawurl = "file:///C:/Users/user", which is an incorrect syntax. // rawURL = "file:///C:/Users/user", which is an incorrect syntax.
if len(u.Path) > 2 && u.Path[0] == '/' && u.Path[2] == ':' { if len(u.Path) > 2 && u.Path[0] == '/' && u.Path[2] == ':' {
u.Path = u.Path[1:] u.Path = u.Path[1:]
} }
return u, err return u, err
} }
func fmtFileURL(path string) string { func fmtFileURL(path string) string {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
// Make sure we're using "/" on Windows. URLs are "/"-based. // Make sure we're using "/" on Windows. URLs are "/"-based.
path = filepath.ToSlash(path) path = filepath.ToSlash(path)
} }
// Make sure that we don't start with "/" since we add that below. // Make sure that we don't start with "/" since we add that below.
if path[0] == '/' { if path[0] == '/' {
path = path[1:] path = path[1:]
} }
return fmt.Sprintf("file:///%s", path) return fmt.Sprintf("file:///%s", path)
} }