Skip to content

Commit

Permalink
fix FileExistsLocally
Browse files Browse the repository at this point in the history
  • Loading branch information
SwampDragons committed Jan 9, 2018
1 parent 4f3b470 commit 216c44b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion builder/virtualbox/ovf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
if err != nil {
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 {
packer.MultiErrorAppend(errs,
fmt.Errorf("Source file needs to exist at time of config validation: %s", err))
Expand Down
26 changes: 14 additions & 12 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func ChooseString(vals ...string) string {
// 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) {
fmt.Printf("Swampy: user input was %s\n", original)
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.
Expand Down Expand Up @@ -89,7 +90,7 @@ func DownloadableURL(original string) (string, error) {
return "", err
}

url.Path = filepath.Clean(url.Path)
// url.Path = filepath.Clean(url.Path)
}

if runtime.GOOS == "windows" {
Expand All @@ -116,7 +117,7 @@ func DownloadableURL(original string) (string, error) {
if !found {
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
}

Expand All @@ -136,20 +137,21 @@ func DownloadableURL(original string) (string, error) {
// file is not present when it should be.

func FileExistsLocally(original string) (bool, error) {
fileURL, _ := url.Parse(original)
fileExists := false
err := nil
// original should be something like file://C:/my/path.iso
// on windows, c drive will be parsed as host if it's file://c instead of file:///c
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" {
// Remove forward slash on absolute Windows file URLs before processing
if runtime.GOOS == "windows" && len(fileURL.Path) > 0 && fileURL.Path[0] == '/' {
filePath := fileURL.Path[1:]
}
if _, err := os.Stat(filePath); err != nil {
err = fmt.Errorf("source file needs to exist at time of config validation: %s", err)
_, err := os.Stat(filePath)
if err != nil {
err = fmt.Errorf("could not stat file: %s\n", err)
return fileExists, err
} else {
fileExists = true
}
}
return fileExists, err
return fileExists, nil
}

0 comments on commit 216c44b

Please sign in to comment.