Skip to content

Commit

Permalink
Marman download tile 166145997 (#2)
Browse files Browse the repository at this point in the history
* Add the ability to download anything from pivnet with a file filter

Co-authored-by: Todd Ritchie <[email protected]>

* Add a command to specifically download the pks tile

Co-authored-by: Ernie Billing <[email protected]>

* Create download-pks, download-srt, download-pas subcommands which call download-tile with appropriate patterns
Co-authored-by: Pete Wall <[email protected]>
Co-authored-by: Ernie Billing <[email protected]>

* Fix typo in error for adding download-pas subcommand
  • Loading branch information
Todd Ritchie authored May 24, 2019
1 parent e3692d5 commit 6452f7f
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 75 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ build-all: build-linux build-darwin

build-linux: build/marman-linux

build/marman-linux:
build/marman-linux: $(SRC) deps
GOARCH=amd64 GOOS=linux go build -o build/marman-linux -ldflags ${LDFLAGS} ./cmd/marman/main.go

build-darwin: build/marman-darwin

build/marman-darwin:
build/marman-darwin: $(SRC) deps
GOARCH=amd64 GOOS=darwin go build -o build/marman-darwin -ldflags ${LDFLAGS} ./cmd/marman/main.go

test: deps lint
Expand Down
51 changes: 48 additions & 3 deletions cmd/marman/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,51 @@ func main() {
os.Exit(1)
}

downloadPKSOpts := &downloadtile.Config{
Slug: "pivotal-container-service",
File: ".pivotal$",
}
_, err = parser.AddCommand(
"download-pks",
"Download pks",
"Download pks tile from PivNet",
downloadPKSOpts,
)
if err != nil {
fmt.Println("Could not add download-pks command")
os.Exit(1)
}

downloadSRTOpts := &downloadtile.Config{
Slug: "cf",
File: "srt-(.*)-(.*).pivotal$",
}
_, err = parser.AddCommand(
"download-srt",
"Download SRT",
"Download SRT tile from PivNet",
downloadSRTOpts,
)
if err != nil {
fmt.Println("Could not add download-srt command")
os.Exit(1)
}

downloadPASOpts := &downloadtile.Config{
Slug: "cf",
File: "cf-(.*)-(.*).pivotal$",
}
_, err = parser.AddCommand(
"download-pas",
"Download PAS",
"Download PAS tile from PivNet",
downloadPASOpts,
)
if err != nil {
fmt.Println("Could not add download-pas command")
os.Exit(1)
}

downloadTileOpts := &downloadtile.Config{}
_, err = parser.AddCommand(
"download-tile",
Expand All @@ -62,9 +107,9 @@ func main() {

_, err = parser.AddCommand(
"version",
"print version",
"print marman version",
&version.VersionOpt{})
"print version",
"print marman version",
&version.VersionOpt{})
if err != nil {
fmt.Println("Could not add version command")
os.Exit(1)
Expand Down
61 changes: 25 additions & 36 deletions downloadtile/downloadtile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package downloadtile

import (
"fmt"
"regexp"
"strings"

"github.com/Masterminds/semver"
Expand All @@ -11,63 +13,50 @@ import (
)

type Config struct {
Name string `short:"n" long:"name" description:"Tile name"`
Slug string `short:"s" long:"slug" description:"PivNet slug name override"`
File string `short:"f" long:"file" description:"RegEx pattern to select the specific file to download"`
Version string `short:"v" long:"version" description:"Tile version"`
PivnetClient pivnetClient.Client
PivnetToken string `long:"pivnet-token" description:"Authentication token for PivNet" env:"PIVNET_TOKEN"`
}

func nameToSlug(name string) (string, error) {
switch name {
case "pas":
return "cf", nil
case "srt":
return "cf", nil
default:
return "", errors.Errorf("unknown tile name %s", name)
func filesToString(files []pivnet.ProductFile, filter string) string {
builder := strings.Builder{}
for _, file := range files {
matched, _ := regexp.MatchString(filter, file.AWSObjectKey)
if matched {
builder.WriteString(fmt.Sprintf("\n %s", file.AWSObjectKey))
}
}
return builder.String()

}

func (cmd *Config) FindFile(productFiles []pivnet.ProductFile, id int) (*pivnet.ProductFile, error) {
var (
pattern string
err error
err error
productFile pivnet.ProductFile
)

switch cmd.Name {
case "pas":
pattern = "Pivotal Application Service"
case "srt":
pattern = "Small Footprint PAS"
default:
return nil, errors.Errorf("unable to find tile with name %s", cmd.Name)
}

var productFile *pivnet.ProductFile
productFile.ID = 0
for _, fileUnderConsideration := range productFiles {
if strings.Contains(fileUnderConsideration.Name, pattern) {
productFile = &fileUnderConsideration
break
matched, _ := regexp.MatchString(cmd.File, fileUnderConsideration.AWSObjectKey)
if matched {
if productFile.ID == 0 {
productFile = fileUnderConsideration
} else {
err = fmt.Errorf("too many matching files found with the given file filter \"%s\"%s", cmd.File, filesToString(productFiles, cmd.File))
}
}
}

if productFile == nil {
err = errors.Errorf("unable to find the tile with name %s", cmd.Name)
if productFile.ID == 0 {
err = errors.Errorf("unable to find the tile with name %s", cmd.File)
}

return productFile, err
return &productFile, err
}

func (cmd *Config) DownloadTile() error {
if cmd.Slug == "" {
slug, err := nameToSlug(cmd.Name)
if err != nil {
return errors.Wrapf(err, "could not find slug for tile name %s", cmd.Name)
}
cmd.Slug = slug
}

versionConstraint, err := semver.NewConstraint(cmd.Version)
if err != nil {
return errors.Wrapf(err, "tile version is not valid semver")
Expand Down
40 changes: 15 additions & 25 deletions downloadtile/downloadtile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,32 @@ var _ = Describe("Download Tile", func() {
BeforeEach(func() {
pivnetClient = &pivnetfakes.FakeClient{}
cmd = &downloadtile.Config{
Name: "srt",
Slug: "cf",
File: "pas",
PivnetClient: pivnetClient,
Version: "2.4.2",
}

cmd.Version = "2.4.1"

pivnetClient.FindReleaseByVersionConstraintReturns(&pivnet.Release{
ID: 100,
Version: "2.4.2",
}, nil)

pivnetClient.ListFilesForReleaseReturns([]pivnet.ProductFile{
{
ID: 123,
Name: "Small Footprint PAS",
ID: 123,
Name: "Small Footprint PAS",
AWSObjectKey: "srt-download-link",
Links: &pivnet.Links{
Download: map[string]string{
"href": "srt-download-link",
},
},
},
{
ID: 456,
Name: "Pivotal Application Service",
ID: 456,
Name: "Pivotal Application Service",
AWSObjectKey: "pas-download-link",
Links: &pivnet.Links{
Download: map[string]string{
"href": "pas-download-link",
Expand Down Expand Up @@ -78,29 +80,17 @@ var _ = Describe("Download Tile", func() {
slug, releaseID, file := pivnetClient.DownloadFileArgsForCall(0)
Expect(slug).To(Equal("cf"))
Expect(releaseID).To(Equal(100))
Expect(file.ID).To(Equal(123))
Expect(file.ID).To(Equal(456))
})
})
})

Context("Allows slug to override name", func() {
It("attempts to download the tile using the slug override", func() {
cmd = &downloadtile.Config{
Name: "srt",
Slug: "alternate-cf",
Version: "2.4.1",
PivnetClient: pivnetClient,
}

Context("too many matching files", func() {
It("returns an error", func() {
cmd.File = "download-link"
err := cmd.DownloadTile()
Expect(err).ToNot(HaveOccurred())

By("getting the list of product files from PivNet", func() {
Expect(pivnetClient.ListFilesForReleaseCallCount()).To(Equal(1))
slug, releaseID := pivnetClient.ListFilesForReleaseArgsForCall(0)
Expect(slug).To(Equal("alternate-cf"))
Expect(releaseID).To(Equal(100))
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("too many matching files found with the given file filter \"download-link\"\n srt-download-link\n pas-download-link"))
})
})

Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ module github.com/cf-platform-eng/marman
require (
code.cloudfoundry.org/lager v2.0.0+incompatible
github.com/Masterminds/semver v1.4.2
github.com/cf-platform-eng/isv-ci-toolkit/marman v0.0.0-20190516205738-dcd8ac658cf6 // indirect
github.com/google/go-github/v25 v25.0.2
github.com/jessevdk/go-flags v1.4.0
github.com/onsi/ginkgo v1.6.0
github.com/onsi/gomega v1.4.2
github.com/pivotal-cf/go-pivnet v0.0.52
github.com/pkg/errors v0.8.1
golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09 // indirect
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
golang.org/x/tools v0.0.0-20190501045030-23463209683d
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872 // indirect
golang.org/x/text v0.3.2 // indirect
)
18 changes: 11 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ code.cloudfoundry.org/lager v2.0.0+incompatible h1:WZwDKDB2PLd/oL+USK4b4aEjUymIe
code.cloudfoundry.org/lager v2.0.0+incompatible/go.mod h1:O2sS7gKP3HM2iemG+EnwvyNQK7pTSC6Foi4QiMp9sSk=
github.com/Masterminds/semver v1.4.2 h1:WBLTQ37jOCzSLtXNdoo8bNM8876KhNqOKvrlGITgsTc=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/StackExchange/wmi v0.0.0-20180725035823-b12b22c5341f h1:5ZfJxyXo8KyX8DgGXC5B7ILL8y51fci/qYz2B4j8iLY=
github.com/StackExchange/wmi v0.0.0-20180725035823-b12b22c5341f/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/cf-platform-eng/isv-ci-toolkit/marman v0.0.0-20190516205738-dcd8ac658cf6 h1:O2oy8FZgnc/BSIP5Bk2l+A0A4AlXqb426/2TG6ogPWo=
github.com/cf-platform-eng/isv-ci-toolkit/marman v0.0.0-20190516205738-dcd8ac658cf6/go.mod h1:/Tcdz3XpgrUWjs0tT0+8vX93tOUVKEhzgja5xw+fWXM=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-ole/go-ole v0.0.0-20180625085808-7a0fa49edf48 h1:WRF1REuysYJdbHUefXfrTuwYdeuCYjjKdm0Kvu8n8fk=
github.com/go-ole/go-ole v0.0.0-20180625085808-7a0fa49edf48/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-github/v25 v25.0.2 h1:MqXE7nOlIF91NJ/PXAcvS2dC+XXCDbY7RvJzjyEPAoU=
github.com/google/go-github/v25 v25.0.2/go.mod h1:6z5pC69qHtrPJ0sXPsj4BLnd82b+r6sLB7qcBoRZqpw=
Expand All @@ -17,10 +20,14 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
Expand All @@ -36,12 +43,10 @@ github.com/robdimsdale/sanitizer v0.0.0-20160522134901-ab2334cb7539/go.mod h1:tq
github.com/shirou/gopsutil v0.0.0-20180927124308-a11c78ba2c13 h1:hzFIj+Ky1KX599VGAVY//20nam1rYKwQwNVix1sYhXo=
github.com/shirou/gopsutil v0.0.0-20180927124308-a11c78ba2c13/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181017193950-04a2e542c03f/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09 h1:KaQtG+aDELoNmXYas3TVkGNYRuq8JQ1aa7LJt8EXVyo=
golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
Expand All @@ -54,17 +59,16 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181011152604-fa43e7bc11ba/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872 h1:cGjJzUd8RgBw428LXP65YXni0aiGNA4Bl+ls8SmLOm8=
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190501045030-23463209683d h1:D7DVZUZEUgsSIDTivnUtVeGfN5AvhDIKtdIZAqx0ieE=
golang.org/x/tools v0.0.0-20190501045030-23463209683d/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/cheggaaa/pb.v1 v1.0.26 h1:KbH37VyQGNNrLEz+fflXwuLLxnPNoWwUwBF783VJWUg=
gopkg.in/cheggaaa/pb.v1 v1.0.26/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
Expand Down

0 comments on commit 6452f7f

Please sign in to comment.