Skip to content

Commit

Permalink
Output artifact name when creating a new artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
bronzdoc committed Dec 7, 2017
1 parent 4ec1896 commit 5cdaaad
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
7 changes: 5 additions & 2 deletions api/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

// Build builds an artifact from a Pakfile
func Build(pakfile *pak.PakFile) {
func Build(pakfile *pak.PakFile) string {
artifact := new(archivex.TarFile)
artifact.Create(fmt.Sprintf("%s.tar", pakfile.ArtifactName))
fullArtifactName := fmt.Sprintf("%s.tar", pakfile.ArtifactName)
artifact.Create(fullArtifactName)

var metadataContent []byte

Expand Down Expand Up @@ -40,4 +41,6 @@ func Build(pakfile *pak.PakFile) {
artifact.AddAll(pakfile.Path, true)

artifact.Close()

return fullArtifactName
}
4 changes: 3 additions & 1 deletion api/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ var _ = Describe("Build", func() {
})

It("should build a pak package", func() {
api.Build(pakFile)
out := api.Build(pakFile)
Expect(out).To(Equal(fmt.Sprintf("%s.tar", artifactName)))

_, err := os.Stat(fmt.Sprintf("%s.tar", artifactName))
Expect(os.IsNotExist(err)).To(Equal(false))
})
Expand Down
17 changes: 9 additions & 8 deletions api/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
)

// Promote will promote the artifact to a given promote label
func Promote(artifactName string, options map[string]interface{}) error {
func Promote(artifactName string, options map[string]interface{}) (string, error) {
inspectOptions := map[string]interface{}{}

// get metadata
jsonMetadata, err := Inspect(artifactName, inspectOptions)
if err != nil {
return errors.Wrap(err, "failed to inspect artifact metadata")
return "", errors.Wrap(err, "failed to inspect artifact metadata")
}

var metadata map[string]interface{}
Expand Down Expand Up @@ -54,7 +54,7 @@ func Promote(artifactName string, options map[string]interface{}) error {
}

if err != nil {
return errors.Wrap(err, "failed to resolve env var")
return "", errors.Wrap(err, "failed to resolve env var")
}
}

Expand All @@ -66,8 +66,9 @@ func Promote(artifactName string, options map[string]interface{}) error {
newArtifactname = value.(string)
} // generate random name if name empty?

fullArtifactName := fmt.Sprintf("%s.tar", newArtifactname)
newArtifact := new(archivex.TarFile)
newArtifact.Create(fmt.Sprintf("%s.tar", newArtifactname))
newArtifact.Create(fullArtifactName)

// create new pak.metadata with the old data and the new one
jsonString, err := json.MarshalIndent(metadata, "", " ")
Expand All @@ -81,7 +82,7 @@ func Promote(artifactName string, options map[string]interface{}) error {
// Add packaged files from the old artifact to the new one
pkg, err := os.Open(artifactName)
if err != nil {
return errors.Wrapf(err, "failed to open artifact %s", artifactName)
return "", errors.Wrapf(err, "failed to open artifact %s", artifactName)
}

tr := tar.NewReader(pkg)
Expand All @@ -94,12 +95,12 @@ func Promote(artifactName string, options map[string]interface{}) error {
break
}

return errors.Wrap(err, "could not get tar header")
return "", errors.Wrap(err, "could not get tar header")
}

fileContent, err := ioutil.ReadAll(tr)
if err != nil {
return errors.Wrapf(err, "could not read artifact %s", artifactName)
return "", errors.Wrapf(err, "could not read artifact %s", artifactName)
}

// Don't add metadata file since we will be adding a new one with new metadata
Expand All @@ -111,5 +112,5 @@ func Promote(artifactName string, options map[string]interface{}) error {

newArtifact.Close()

return nil
return fullArtifactName, nil
}
5 changes: 3 additions & 2 deletions api/promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,21 @@ var _ = Describe("Promote", func() {
})

It("should create a new artifact", func() {
err := api.Promote(
out, err := api.Promote(
fmt.Sprintf("%s.tar", artifactName),
map[string]interface{}{
"label": "rc",
})

Expect(out).To(Equal(fmt.Sprintf("%s.tar", promoteArtifactName)))
Expect(err).To(BeNil())

_, err = os.Stat(fmt.Sprintf("%s.tar", promoteArtifactName))
Expect(os.IsNotExist(err)).To(Equal(false))
})

It("should create a pak.metadata inside the artifact", func() {
err := api.Promote(
_, err := api.Promote(
fmt.Sprintf("%s.tar", artifactName),
map[string]interface{}{
"label": "rc",
Expand Down
3 changes: 2 additions & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ var buildCmd = &cobra.Command{
os.Exit(1)
}

api.Build(pakfile)
fmt.Println(api.Build(pakfile))

},
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ var promoteCmd = &cobra.Command{
"label": label,
}

err := api.Promote(artifactName, options)
artifactName, err := api.Promote(artifactName, options)
if err != nil {
fmt.Printf("failed to promote %s: %s", artifactName, err)
os.Exit(1)
}

fmt.Println(artifactName)
},
}

Expand Down

0 comments on commit 5cdaaad

Please sign in to comment.