Skip to content

Commit

Permalink
change the output file for download-product
Browse files Browse the repository at this point in the history
product entry is changed to product_path
stemcell entry is changed to stemcell_path

Signed-off-by: Kira Boyle <[email protected]>
  • Loading branch information
fredwangwang authored and kcboyle committed Nov 26, 2018
1 parent 62345fa commit 8ccbec3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
34 changes: 19 additions & 15 deletions commands/download_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import (
"strings"
)

const DownloadListFilename = "download-file.json"
const DownloadProductOutputFilename = "download-file.json"

type downloadList struct {
Product string `json:"product,omitempty"`
Stemcell string `json:"stemcell,omitempty"`
type outputList struct {
ProductPath string `json:"product_path,omitempty"`
ProductSlug string `json:"product_slug,omitempty"`
StemcellPath string `json:"stemcell_path,omitempty"`
StemcellVersion string `json:"stemcell_version,omitempty"`
}

//go:generate counterfeiter -o ./fakes/pivnet_downloader_service.go --fake-name PivnetDownloader . PivnetDownloader
Expand Down Expand Up @@ -93,7 +95,7 @@ func (c DownloadProduct) Execute(args []string) error {
}

if !c.Options.Stemcell {
return c.writerDownloadedFileList(productFileName, stemcellFileName)
return c.writeOutputFile(productFileName, stemcellFileName, "")
}

c.logger.Info("Downloading stemcell")
Expand All @@ -119,23 +121,25 @@ func (c DownloadProduct) Execute(args []string) error {
return fmt.Errorf("could not download stemcell: %s", err)
}

return c.writerDownloadedFileList(productFileName, stemcellFileName)
return c.writeOutputFile(productFileName, stemcellFileName, stemcellVersion)
}

func (c DownloadProduct) writerDownloadedFileList(productFileName string, stemcellFileName string) error {
c.logger.Info(fmt.Sprintf("Writing a list of downloaded artifact to %s", DownloadListFilename))
downloadList := downloadList{
Product: productFileName,
Stemcell: stemcellFileName,
func (c DownloadProduct) writeOutputFile(productFileName string, stemcellFileName string, stemcellVersion string) error {
c.logger.Info(fmt.Sprintf("Writing a list of downloaded artifact to %s", DownloadProductOutputFilename))
outputList := outputList{
ProductPath: productFileName,
StemcellPath: stemcellFileName,
ProductSlug: c.Options.ProductSlug,
StemcellVersion: stemcellVersion,
}

downloadListFile, err := os.Create(path.Join(c.Options.OutputDir, DownloadListFilename))
outputFile, err := os.Create(path.Join(c.Options.OutputDir, DownloadProductOutputFilename))
if err != nil {
return fmt.Errorf("could not create %s: %s", DownloadListFilename, err)
return fmt.Errorf("could not create %s: %s", DownloadProductOutputFilename, err)
}
defer downloadListFile.Close()
defer outputFile.Close()

return json.NewEncoder(downloadListFile).Encode(downloadList)
return json.NewEncoder(outputFile).Encode(outputList)
}

func (c *DownloadProduct) init() {
Expand Down
22 changes: 18 additions & 4 deletions commands/download_product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ var _ = Describe("DownloadProduct", func() {
Expect(releaseID).To(Equal(12345))
Expect(productFileID).To(Equal(54321))

fileName := path.Join(tempDir, commands.DownloadListFilename)
fileName := path.Join(tempDir, commands.DownloadProductOutputFilename)
fileContent, err := ioutil.ReadFile(fileName)
Expect(err).NotTo(HaveOccurred())
Expect(fileName).To(BeAnExistingFile())
Expect(string(fileContent)).To(MatchJSON(fmt.Sprintf(`{"product": "%s" }`, file.Name())))
Expect(string(fileContent)).To(MatchJSON(fmt.Sprintf(`{"product_path": "%s", "product_slug": "elastic-runtime" }`, file.Name())))
})

Context("when the globs returns multiple files", func() {
Expand Down Expand Up @@ -215,11 +215,25 @@ var _ = Describe("DownloadProduct", func() {
Expect(version).To(Equal("97.19"))
Expect(str).To(Equal("stemcells-ubuntu-xenial"))

file, slug, releaseID, fileID, _ := fakePivnetDownloader.DownloadProductFileArgsForCall(1)
Expect(file.Name()).To(Equal(path.Join(tempDir, "light-bosh-stemcell-97.19-google-kvm-ubuntu-xenial-go_agent.tgz")))
productFile, _, _, _, _:= fakePivnetDownloader.DownloadProductFileArgsForCall(0)

stemcellFile, slug, releaseID, fileID, _ := fakePivnetDownloader.DownloadProductFileArgsForCall(1)
Expect(stemcellFile.Name()).To(Equal(path.Join(tempDir, "light-bosh-stemcell-97.19-google-kvm-ubuntu-xenial-go_agent.tgz")))
Expect(slug).To(Equal("stemcells-ubuntu-xenial"))
Expect(releaseID).To(Equal(9999))
Expect(fileID).To(Equal(5678))

fileName := path.Join(tempDir, commands.DownloadProductOutputFilename)
fileContent, err := ioutil.ReadFile(fileName)
Expect(err).NotTo(HaveOccurred())
Expect(fileName).To(BeAnExistingFile())
Expect(string(fileContent)).To(MatchJSON(fmt.Sprintf(`
{
"product_path": "%s",
"product_slug": "elastic-runtime",
"stemcell_path": "%s",
"stemcell_version": "97.19"
}`, productFile.Name(), stemcellFile.Name())))
})

Context("when the product is not a tile", func() {
Expand Down

0 comments on commit 8ccbec3

Please sign in to comment.