Skip to content

Commit 3794ff5

Browse files
authored
Merge pull request #130 from mstandley-tempus/assets-in-separate-dir
optionally put assets in separate dir
2 parents 0331fe3 + 60d4433 commit 3794ff5

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ Fetches and creates versioned GitHub resources.
6565
Selects whether to order releases by version (as extracted by `tag_filter`)
6666
or by time. See `check` behavior described below for details.
6767

68+
* `asset_dir`: *Optional. Default `false`.* When set to `true`, downloaded assets
69+
will be created in a separate directory called `assets`. Otherwise, they will be
70+
created in the same directory as the other files.
71+
6872
### Example
6973

7074
``` yaml
@@ -135,7 +139,8 @@ Otherwise it returns the release with the latest version or time.
135139

136140
### `in`: Fetch assets from a release.
137141

138-
Fetches artifacts from the requested release.
142+
Fetches artifacts from the requested release. If `asset_dir` source param is set to `true`,
143+
artifacts will be created in a subdirectory called `assets`.
139144

140145
Also creates the following files:
141146

in_command.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
3131
return InResponse{}, err
3232
}
3333

34+
// if AssetDir is true, create a separate directory for assets
35+
assetDir := destDir
36+
if request.Source.AssetDir {
37+
assetDir = filepath.Join(destDir, "assets")
38+
err = os.MkdirAll(assetDir, 0755)
39+
if err != nil {
40+
return InResponse{}, err
41+
}
42+
}
43+
3444
var foundRelease *github.RepositoryRelease
3545
var commitSHA string
3646

@@ -121,7 +131,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
121131
continue
122132
}
123133

124-
path := filepath.Join(destDir, *asset.Name)
134+
path := filepath.Join(assetDir, *asset.Name)
125135

126136
var matchFound bool
127137
if len(request.Params.Globs) == 0 {
@@ -158,7 +168,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
158168
return InResponse{}, err
159169
}
160170
fmt.Fprintln(c.writer, "downloading source tarball to source.tar.gz")
161-
if err := c.downloadFile(u.String(), filepath.Join(destDir, "source.tar.gz")); err != nil {
171+
if err := c.downloadFile(u.String(), filepath.Join(assetDir, "source.tar.gz")); err != nil {
162172
return InResponse{}, err
163173
}
164174
}
@@ -169,7 +179,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
169179
return InResponse{}, err
170180
}
171181
fmt.Fprintln(c.writer, "downloading source zip to source.zip")
172-
if err := c.downloadFile(u.String(), filepath.Join(destDir, "source.zip")); err != nil {
182+
if err := c.downloadFile(u.String(), filepath.Join(assetDir, "source.zip")); err != nil {
173183
return InResponse{}, err
174184
}
175185
}

in_command_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,16 @@ var _ = Describe("In Command", func() {
270270
Expect(err).NotTo(HaveOccurred())
271271
Expect(fContents).To(Equal("source-tar-file-contents"))
272272
})
273+
274+
It("saves the source tarball in the assets directory, if desired", func() {
275+
inRequest.Source.AssetDir = true
276+
inResponse, inErr = command.Run(destDir, inRequest)
277+
278+
fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "assets", "source.tar.gz"))
279+
fContents := string(fileContents)
280+
Expect(err).NotTo(HaveOccurred())
281+
Expect(fContents).To(Equal("source-tar-file-contents"))
282+
})
273283
})
274284

275285
Context("when downloading the tarball fails", func() {
@@ -350,6 +360,16 @@ var _ = Describe("In Command", func() {
350360
Expect(err).NotTo(HaveOccurred())
351361
Expect(fContents).To(Equal("source-zip-file-contents"))
352362
})
363+
364+
It("saves the source tarball in the assets directory, if desired", func() {
365+
inRequest.Source.AssetDir = true
366+
inResponse, inErr = command.Run(destDir, inRequest)
367+
368+
fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "assets", "source.zip"))
369+
fContents := string(fileContents)
370+
Expect(err).NotTo(HaveOccurred())
371+
Expect(fContents).To(Equal("source-zip-file-contents"))
372+
})
353373
})
354374

355375
Context("when downloading the zip fails", func() {

resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Source struct {
1919
PreRelease bool `json:"pre_release"`
2020
Release bool `json:"release"`
2121
Insecure bool `json:"insecure"`
22+
AssetDir bool `json:"asset_dir"`
2223

2324
TagFilter string `json:"tag_filter"`
2425
OrderBy string `json:"order_by"`

0 commit comments

Comments
 (0)