fix FileExistsLocally

This commit is contained in:
Megan Marsh 2018-01-04 10:51:59 -08:00
parent 4f3b470804
commit 216c44b153
2 changed files with 15 additions and 13 deletions

View File

@ -101,7 +101,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
if err != nil { if err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is invalid: %s", err)) errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is invalid: %s", err))
} }
fileExists, err := common.FileExistsLocally(c.SourcePath) _, err := common.FileExistsLocally(c.SourcePath)
if err != nil { if err != nil {
packer.MultiErrorAppend(errs, packer.MultiErrorAppend(errs,
fmt.Errorf("Source file needs to exist at time of config validation: %s", err)) fmt.Errorf("Source file needs to exist at time of config validation: %s", err))

View File

@ -47,6 +47,7 @@ func ChooseString(vals ...string) string {
// a completely valid URL. For example, the original URL might be "local/file.iso" // 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" // which isn't a valid URL. DownloadableURL will return "file:///local/file.iso"
func DownloadableURL(original string) (string, error) { func DownloadableURL(original string) (string, error) {
fmt.Printf("Swampy: user input was %s\n", original)
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
// If the distance to the first ":" is just one character, assume // If the distance to the first ":" is just one character, assume
// we're dealing with a drive letter and thus a file path. // we're dealing with a drive letter and thus a file path.
@ -89,7 +90,7 @@ func DownloadableURL(original string) (string, error) {
return "", err return "", err
} }
url.Path = filepath.Clean(url.Path) // url.Path = filepath.Clean(url.Path)
} }
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
@ -116,7 +117,7 @@ func DownloadableURL(original string) (string, error) {
if !found { if !found {
return "", fmt.Errorf("Unsupported URL scheme: %s", url.Scheme) return "", fmt.Errorf("Unsupported URL scheme: %s", url.Scheme)
} }
fmt.Printf("Swampy: parsed string after DownloadableURL is %s\n", url.String())
return url.String(), nil return url.String(), nil
} }
@ -136,20 +137,21 @@ func DownloadableURL(original string) (string, error) {
// file is not present when it should be. // file is not present when it should be.
func FileExistsLocally(original string) (bool, error) { func FileExistsLocally(original string) (bool, error) {
fileURL, _ := url.Parse(original) // original should be something like file://C:/my/path.iso
fileExists := false // on windows, c drive will be parsed as host if it's file://c instead of file:///c
err := nil prefix = "file://"
filePath = strings.Replace(original, prefix, "", 1)
fmt.Printf("Swampy: original is %s\n", original)
fmt.Printf("Swampy: filePath is %#v\n", filePath)
if fileURL.Scheme == "file" { if fileURL.Scheme == "file" {
// Remove forward slash on absolute Windows file URLs before processing _, err := os.Stat(filePath)
if runtime.GOOS == "windows" && len(fileURL.Path) > 0 && fileURL.Path[0] == '/' { if err != nil {
filePath := fileURL.Path[1:] err = fmt.Errorf("could not stat file: %s\n", err)
} return fileExists, err
if _, err := os.Stat(filePath); err != nil {
err = fmt.Errorf("source file needs to exist at time of config validation: %s", err)
} else { } else {
fileExists = true fileExists = true
} }
} }
return fileExists, err return fileExists, nil
} }