2013-07-19 14:59:04 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2013-07-19 15:09:13 -04:00
|
|
|
"fmt"
|
2013-07-29 02:51:21 -04:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2013-07-29 03:09:48 -04:00
|
|
|
"path/filepath"
|
2013-08-02 17:06:06 -04:00
|
|
|
"runtime"
|
2013-07-19 15:09:13 -04:00
|
|
|
"strings"
|
2013-07-19 14:59:04 -04:00
|
|
|
)
|
|
|
|
|
2013-10-11 06:50:08 -04:00
|
|
|
// ScrubConfig is a helper that returns a string representation of
|
|
|
|
// any struct with the given values stripped out.
|
|
|
|
func ScrubConfig(target interface{}, values ...string) string {
|
|
|
|
conf := fmt.Sprintf("Config: %+v", target)
|
|
|
|
for _, value := range values {
|
2015-05-18 18:13:01 -04:00
|
|
|
if value == "" {
|
|
|
|
continue
|
|
|
|
}
|
2013-10-11 06:50:08 -04:00
|
|
|
conf = strings.Replace(conf, value, "<Filtered>", -1)
|
|
|
|
}
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
2014-04-22 00:30:49 -04:00
|
|
|
// ChooseString returns the first non-empty value.
|
|
|
|
func ChooseString(vals ...string) string {
|
2014-04-26 14:12:43 -04:00
|
|
|
for _, el := range vals {
|
|
|
|
if el != "" {
|
|
|
|
return el
|
|
|
|
}
|
|
|
|
}
|
2014-04-22 00:30:49 -04:00
|
|
|
|
2014-04-26 14:12:43 -04:00
|
|
|
return ""
|
2014-04-22 00:30:49 -04:00
|
|
|
}
|
|
|
|
|
2013-07-29 02:51:21 -04:00
|
|
|
// DownloadableURL processes a URL that may also be a file path and returns
|
|
|
|
// a completely valid URL. For example, the original URL might be "local/file.iso"
|
|
|
|
// which isn't a valid URL. DownloadableURL will return "file:///local/file.iso"
|
|
|
|
func DownloadableURL(original string) (string, error) {
|
2013-08-15 23:16:05 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// If the distance to the first ":" is just one character, assume
|
|
|
|
// we're dealing with a drive letter and thus a file path.
|
|
|
|
idx := strings.Index(original, ":")
|
|
|
|
if idx == 1 {
|
|
|
|
original = "file:///" + original
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 02:51:21 -04:00
|
|
|
url, err := url.Parse(original)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if url.Scheme == "" {
|
|
|
|
url.Scheme = "file"
|
|
|
|
}
|
|
|
|
|
|
|
|
if url.Scheme == "file" {
|
2014-04-22 00:28:47 -04:00
|
|
|
// Windows file handling is all sorts of tricky...
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// If the path is using Windows-style slashes, URL parses
|
|
|
|
// it into the host field.
|
|
|
|
if url.Path == "" && strings.Contains(url.Host, `\`) {
|
|
|
|
url.Path = url.Host
|
|
|
|
url.Host = ""
|
|
|
|
}
|
2013-08-03 16:38:27 -04:00
|
|
|
|
2014-04-22 00:28:47 -04:00
|
|
|
// For Windows absolute file paths, remove leading / prior to processing
|
|
|
|
// since net/url turns "C:/" into "/C:/"
|
|
|
|
if len(url.Path) > 0 && url.Path[0] == '/' {
|
|
|
|
url.Path = url.Path[1:len(url.Path)]
|
|
|
|
}
|
2013-08-02 17:06:06 -04:00
|
|
|
}
|
2013-08-02 16:59:19 -04:00
|
|
|
|
2013-12-06 21:31:45 -05:00
|
|
|
// Only do the filepath transformations if the file appears
|
|
|
|
// to actually exist.
|
|
|
|
if _, err := os.Stat(url.Path); err == nil {
|
|
|
|
url.Path, err = filepath.Abs(url.Path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-07-29 03:06:21 -04:00
|
|
|
|
2013-12-06 21:31:45 -05:00
|
|
|
url.Path, err = filepath.EvalSymlinks(url.Path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-07-29 03:06:21 -04:00
|
|
|
|
2013-12-06 21:31:45 -05:00
|
|
|
url.Path = filepath.Clean(url.Path)
|
2013-07-29 03:06:21 -04:00
|
|
|
}
|
2014-04-22 00:28:47 -04:00
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Also replace all backslashes with forwardslashes since Windows
|
|
|
|
// users are likely to do this but the URL should actually only
|
|
|
|
// contain forward slashes.
|
|
|
|
url.Path = strings.Replace(url.Path, `\`, `/`, -1)
|
|
|
|
}
|
2013-07-29 02:51:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it is lowercased
|
|
|
|
url.Scheme = strings.ToLower(url.Scheme)
|
|
|
|
|
2015-06-13 10:20:54 -04:00
|
|
|
// This is to work around issue #5927. This can safely be removed once
|
|
|
|
// we distribute with a version of Go that fixes that bug.
|
|
|
|
//
|
|
|
|
// See: https://code.google.com/p/go/issues/detail?id=5927
|
|
|
|
if url.Path != "" && url.Path[0] != '/' {
|
|
|
|
url.Path = "/" + url.Path
|
|
|
|
}
|
|
|
|
|
2013-07-29 02:51:21 -04:00
|
|
|
// Verify that the scheme is something we support in our common downloader.
|
|
|
|
supported := []string{"file", "http", "https"}
|
|
|
|
found := false
|
|
|
|
for _, s := range supported {
|
|
|
|
if url.Scheme == s {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return "", fmt.Errorf("Unsupported URL scheme: %s", url.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.String(), nil
|
|
|
|
}
|