diff --git a/post-processor/vagrant-cloud/post-processor.go b/post-processor/vagrant-cloud/post-processor.go index 610c402b8..bc63debbc 100644 --- a/post-processor/vagrant-cloud/post-processor.go +++ b/post-processor/vagrant-cloud/post-processor.go @@ -5,8 +5,13 @@ package vagrantcloud import ( + "archive/tar" + "compress/gzip" "context" "fmt" + "io" + "io/ioutil" + "log" "os" "strings" @@ -232,5 +237,34 @@ func providerFromVagrantBox(boxfile string) (providerName string, err error) { } defer f.Close() + // Vagrant boxes are gzipped tar archives + ar, err := gzip.NewReader(f) + if err != nil { + return "", fmt.Errorf("Error unzipping box archive: %s", err) + } + tr := tar.NewReader(ar) + + // Loop through the files in the archive and read the provider + // information from the boxes metadata.json file + for { + hdr, err := tr.Next() + if err == io.EOF { + break + } + if err != nil { + return "", fmt.Errorf("%s", err) + } + + if hdr.Name == "metadata.json" { + contents, err := ioutil.ReadAll(tr) + if err != nil { + return "", fmt.Errorf("Error reading contents of metadata.json file from box file: %s", err) + } + // TODO: Parse the json for the provider + log.Printf("Contents: %s", contents) + break + } + } + return "", nil } diff --git a/post-processor/vagrant-cloud/post-processor_test.go b/post-processor/vagrant-cloud/post-processor_test.go index 439f580df..f42f0c63b 100644 --- a/post-processor/vagrant-cloud/post-processor_test.go +++ b/post-processor/vagrant-cloud/post-processor_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "io/ioutil" "net/http" "net/http/httptest" "os" @@ -199,6 +200,7 @@ func TestProviderFromBuilderName(t *testing.T) { } func TestProviderFromVagrantBox_missing_box(t *testing.T) { + // Bad: Box does not exist boxfile := "i_dont_exist.box" _, err := providerFromVagrantBox(boxfile) if err == nil { @@ -206,3 +208,18 @@ func TestProviderFromVagrantBox_missing_box(t *testing.T) { } t.Logf("%s", err) } + +func TestProviderFromVagrantBox_empty_box(t *testing.T) { + // Bad: Empty box file + boxfile, err := ioutil.TempFile(os.TempDir(), "test*.box") + if err != nil { + t.Fatalf("Error creating test box file: %s", err) + } + defer os.Remove(boxfile.Name()) + + _, err = providerFromVagrantBox(boxfile.Name()) + if err == nil { + t.Fatal("Should have error as box file is empty") + } + t.Logf("%s", err) +}