From d31d2e72fd15be2300ac467cace4f824daf11ad3 Mon Sep 17 00:00:00 2001 From: Joshua Benjamin Date: Mon, 8 Jul 2019 15:10:27 -0700 Subject: [PATCH] (+semver: fix) update pull to remove version from path (#4) Signed-off-by: Joshua Benjamin --- pkg/client/pull.go | 14 ++++++++++++-- pkg/config/configuration.go | 17 +++++++++++++++++ pkg/config/configuration_test.go | 22 ++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/pkg/client/pull.go b/pkg/client/pull.go index 516064a..1cc60ea 100644 --- a/pkg/client/pull.go +++ b/pkg/client/pull.go @@ -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), @@ -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") } diff --git a/pkg/config/configuration.go b/pkg/config/configuration.go index 5a5743c..00c10b5 100644 --- a/pkg/config/configuration.go +++ b/pkg/config/configuration.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "io" "github.com/pkg/errors" @@ -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 +} diff --git a/pkg/config/configuration_test.go b/pkg/config/configuration_test.go index 2559445..eabb8c5 100644 --- a/pkg/config/configuration_test.go +++ b/pkg/config/configuration_test.go @@ -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")) + }) + }) })