Skip to content

Commit

Permalink
(+semver: fix) update pull to remove version from path (#4)
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Benjamin <[email protected]>
  • Loading branch information
annymsMthd authored Jul 8, 2019
1 parent abf9c53 commit d31d2e7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/client/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func Pull(options PullOptions) error {
return errors.New("nothing to pull")
}

err := options.Configuration.Validate()
if err != nil {
return err
}

for _, dependency := range options.Configuration.Dependencies {
path := fmt.Sprintf("%s/v1/projects/%s/types/%s/versions/%s/data.tar.gz",
options.Configuration.ResolveRepository(dependency),
Expand Down Expand Up @@ -51,9 +56,14 @@ func Pull(options PullOptions) error {
func unPackDependency(configuration *config.Configuration, dependency config.Dependency, file io.ReadCloser) error {
defer file.Close()

pth := path.Join(configuration.IdlDirectory, dependency.Name, dependency.Type, dependency.Version)
pth := path.Join(configuration.IdlDirectory, dependency.Name, dependency.Type)

err := os.RemoveAll(pth)
if err != nil {
return errors.Wrap(err, "failed to clean path")
}

err := os.MkdirAll(pth, os.ModePerm)
err = os.MkdirAll(pth, os.ModePerm)
if err != nil {
return errors.Wrap(err, "failed to create directories")
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/config/configuration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"io"

"github.com/pkg/errors"
Expand Down Expand Up @@ -58,3 +59,19 @@ func (c *Configuration) ResolveRepository(dependency Dependency) string {

return c.Repository
}

func (c *Configuration) Validate() error {
requires := map[string]map[string]bool{}
for _, dep := range c.Dependencies {
_, ok := requires[dep.Name]
if !ok {
requires[dep.Name] = map[string]bool{}
}
_, ok = requires[dep.Name][dep.Type]
if ok {
return errors.New(fmt.Sprintf("the dependency '%s' with type '%s' has more than one entry", dep.Name, dep.Type))
}
requires[dep.Name][dep.Type] = true
}
return nil
}
22 changes: 22 additions & 0 deletions pkg/config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,26 @@ provides:
Expect(actual).To(Equal(expect))
})
})

Context("having multiple entries for the same dependency", func() {
config := config.Configuration{
Dependencies: []config.Dependency{
config.Dependency{
Name: "same-dependency-name",
Type: "protobuf",
},
config.Dependency{
Name: "same-dependency-name",
Type: "protobuf",
},
},
}

err := config.Validate()

It("should have error", func() {
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal("the dependency 'same-dependency-name' with type 'protobuf' has more than one entry"))
})
})
})

0 comments on commit d31d2e7

Please sign in to comment.