Skip to content

Commit 5cdaaad

Browse files
committed
Output artifact name when creating a new artifact
1 parent 4ec1896 commit 5cdaaad

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

api/build.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
)
1010

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

1617
var metadataContent []byte
1718

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

4243
artifact.Close()
44+
45+
return fullArtifactName
4346
}

api/build_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ var _ = Describe("Build", func() {
3838
})
3939

4040
It("should build a pak package", func() {
41-
api.Build(pakFile)
41+
out := api.Build(pakFile)
42+
Expect(out).To(Equal(fmt.Sprintf("%s.tar", artifactName)))
43+
4244
_, err := os.Stat(fmt.Sprintf("%s.tar", artifactName))
4345
Expect(os.IsNotExist(err)).To(Equal(false))
4446
})

api/promote.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import (
1414
)
1515

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

2020
// get metadata
2121
jsonMetadata, err := Inspect(artifactName, inspectOptions)
2222
if err != nil {
23-
return errors.Wrap(err, "failed to inspect artifact metadata")
23+
return "", errors.Wrap(err, "failed to inspect artifact metadata")
2424
}
2525

2626
var metadata map[string]interface{}
@@ -54,7 +54,7 @@ func Promote(artifactName string, options map[string]interface{}) error {
5454
}
5555

5656
if err != nil {
57-
return errors.Wrap(err, "failed to resolve env var")
57+
return "", errors.Wrap(err, "failed to resolve env var")
5858
}
5959
}
6060

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

69+
fullArtifactName := fmt.Sprintf("%s.tar", newArtifactname)
6970
newArtifact := new(archivex.TarFile)
70-
newArtifact.Create(fmt.Sprintf("%s.tar", newArtifactname))
71+
newArtifact.Create(fullArtifactName)
7172

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

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

97-
return errors.Wrap(err, "could not get tar header")
98+
return "", errors.Wrap(err, "could not get tar header")
9899
}
99100

100101
fileContent, err := ioutil.ReadAll(tr)
101102
if err != nil {
102-
return errors.Wrapf(err, "could not read artifact %s", artifactName)
103+
return "", errors.Wrapf(err, "could not read artifact %s", artifactName)
103104
}
104105

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

112113
newArtifact.Close()
113114

114-
return nil
115+
return fullArtifactName, nil
115116
}

api/promote_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,21 @@ var _ = Describe("Promote", func() {
5555
})
5656

5757
It("should create a new artifact", func() {
58-
err := api.Promote(
58+
out, err := api.Promote(
5959
fmt.Sprintf("%s.tar", artifactName),
6060
map[string]interface{}{
6161
"label": "rc",
6262
})
6363

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

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

7071
It("should create a pak.metadata inside the artifact", func() {
71-
err := api.Promote(
72+
_, err := api.Promote(
7273
fmt.Sprintf("%s.tar", artifactName),
7374
map[string]interface{}{
7475
"label": "rc",

cmd/build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ var buildCmd = &cobra.Command{
2020
os.Exit(1)
2121
}
2222

23-
api.Build(pakfile)
23+
fmt.Println(api.Build(pakfile))
24+
2425
},
2526
}
2627

cmd/promote.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ var promoteCmd = &cobra.Command{
2929
"label": label,
3030
}
3131

32-
err := api.Promote(artifactName, options)
32+
artifactName, err := api.Promote(artifactName, options)
3333
if err != nil {
3434
fmt.Printf("failed to promote %s: %s", artifactName, err)
3535
os.Exit(1)
3636
}
37+
38+
fmt.Println(artifactName)
3739
},
3840
}
3941

0 commit comments

Comments
 (0)