Просмотр исходного кода

Merge branch 'int-tt-feat-support-private-repository'

Alex Suraci 7 лет назад
Родитель
Сommit
950b6da0a8
6 измененных файлов с 113 добавлено и 17 удалено
  1. 4 4
      check_test.go
  2. 20 5
      cmd/check/main.go
  3. 13 2
      cmd/in/main.go
  4. 1 1
      cmd/out/main.go
  5. 25 5
      types.go
  6. 50 0
      types_test.go

+ 4 - 4
check_test.go

@@ -42,7 +42,7 @@ var _ = Describe("Check", func() {
 		BeforeEach(func() {
 			req.Source = resource.Source{
 				Repository: "concourse/test-image-static",
-				RawTag:     "latest",
+				Tag:        "latest",
 			}
 
 			req.Version = nil
@@ -59,7 +59,7 @@ var _ = Describe("Check", func() {
 		BeforeEach(func() {
 			req.Source = resource.Source{
 				Repository: "concourse/test-image-static",
-				RawTag:     "latest",
+				Tag:        "latest",
 			}
 
 			req.Version = &resource.Version{
@@ -78,7 +78,7 @@ var _ = Describe("Check", func() {
 		BeforeEach(func() {
 			req.Source = resource.Source{
 				Repository: "concourse/test-image-static",
-				RawTag:     "latest",
+				Tag:        "latest",
 			}
 
 			req.Version = &resource.Version{
@@ -99,7 +99,7 @@ var _ = Describe("Check", func() {
 		BeforeEach(func() {
 			req.Source = resource.Source{
 				Repository: "concourse/test-image-static",
-				RawTag:     "latest",
+				Tag:        "latest",
 			}
 
 			req.Version = &resource.Version{

+ 20 - 5
cmd/check/main.go

@@ -5,7 +5,9 @@ import (
 	"os"
 
 	resource "github.com/concourse/registry-image-resource"
+	"github.com/google/go-containerregistry/pkg/authn"
 	"github.com/google/go-containerregistry/pkg/name"
+	"github.com/google/go-containerregistry/pkg/v1"
 	"github.com/google/go-containerregistry/pkg/v1/remote"
 	"github.com/sirupsen/logrus"
 )
@@ -33,16 +35,24 @@ func main() {
 		return
 	}
 
-	ref := req.Source.Repository + ":" + req.Source.Tag()
-
-	n, err := name.ParseReference(ref, name.WeakValidation)
+	n, err := name.ParseReference(req.Source.Name(), name.WeakValidation)
 	if err != nil {
 		logrus.Errorf("could not resolve repository/tag reference: %s", err)
 		os.Exit(1)
 		return
 	}
 
-	image, err := remote.Image(n)
+	auth := &authn.Basic{
+		Username: req.Source.Username,
+		Password: req.Source.Password,
+	}
+
+	var image v1.Image
+	if auth.Username != "" && auth.Password != "" {
+		image, err = remote.Image(n, remote.WithAuth(auth))
+	} else {
+		image, err = remote.Image(n)
+	}
 	if err != nil {
 		logrus.Errorf("failed to get remote image: %s", err)
 		os.Exit(1)
@@ -64,8 +74,13 @@ func main() {
 			os.Exit(1)
 			return
 		}
+		var digestImage v1.Image
+		if auth.Username != "" && auth.Password != "" {
+			digestImage, err = remote.Image(digestRef, remote.WithAuth(auth))
 
-		digestImage, err := remote.Image(digestRef)
+		} else {
+			digestImage, err = remote.Image(digestRef)
+		}
 		if err != nil {
 			logrus.Errorf("failed to get remote image: %s", err)
 			os.Exit(1)

+ 13 - 2
cmd/in/main.go

@@ -10,6 +10,7 @@ import (
 
 	resource "github.com/concourse/registry-image-resource"
 	color "github.com/fatih/color"
+	"github.com/google/go-containerregistry/pkg/authn"
 	"github.com/google/go-containerregistry/pkg/name"
 	"github.com/google/go-containerregistry/pkg/v1"
 	"github.com/google/go-containerregistry/pkg/v1/remote"
@@ -74,7 +75,17 @@ func main() {
 
 	fmt.Fprintf(os.Stderr, "fetching %s@%s\n", color.GreenString(req.Source.Repository), color.YellowString(req.Version.Digest))
 
-	image, err := remote.Image(n)
+	auth := &authn.Basic{
+		Username: req.Source.Username,
+		Password: req.Source.Password,
+	}
+	var image v1.Image
+	if auth.Username != "" && auth.Password != "" {
+		image, err = remote.Image(n, remote.WithAuth(auth))
+
+	} else {
+		image, err = remote.Image(n)
+	}
 	if err != nil {
 		logrus.Errorf("failed to locate remote image: %s", err)
 		os.Exit(1)
@@ -112,7 +123,7 @@ func saveDigest(dest string, image v1.Image) error {
 }
 
 func ociFormat(dest string, req InRequest, image v1.Image) {
-	tag, err := name.NewTag(req.Source.Repository+":"+req.Source.Tag(), name.WeakValidation)
+	tag, err := name.NewTag(req.Source.Name(), name.WeakValidation)
 	if err != nil {
 		logrus.Errorf("failed to construct tag reference: %s", err)
 		os.Exit(1)

+ 1 - 1
cmd/out/main.go

@@ -58,7 +58,7 @@ func main() {
 
 	logrus.Warnln("'put' is experimental, untested, and subject to change!")
 
-	ref := req.Source.Repository + ":" + req.Source.Tag()
+	ref := req.Source.Name()
 
 	n, err := name.ParseReference(ref, name.WeakValidation)
 	if err != nil {

+ 25 - 5
types.go

@@ -1,10 +1,15 @@
 package resource
 
+import (
+	"encoding/json"
+	"fmt"
+)
+
 const DefaultTag = "latest"
 
 type Source struct {
 	Repository string `json:"repository"`
-	RawTag     string `json:"tag"`
+	Tag        Tag    `json:"tag"`
 
 	Username string `json:"username"`
 	Password string `json:"password"`
@@ -12,12 +17,27 @@ type Source struct {
 	Debug bool `json:"debug"`
 }
 
-func (s Source) Tag() string {
-	if s.RawTag == "" {
-		return DefaultTag
+func (source *Source) Name() string {
+	return fmt.Sprintf("%s:%s", source.Repository, source.Tag)
+}
+
+type Tag string
+
+func (tag *Tag) UnmarshalJSON(b []byte) error {
+	var n json.Number
+
+	err := json.Unmarshal(b, &n)
+	if err != nil {
+		return err
+	}
+
+	if n.String() == "" {
+		*tag = Tag(DefaultTag)
+		return nil
 	}
 
-	return s.RawTag
+	*tag = Tag(n.String())
+	return nil
 }
 
 type Version struct {

+ 50 - 0
types_test.go

@@ -0,0 +1,50 @@
+package resource_test
+
+import (
+	"encoding/json"
+	"strings"
+
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+
+	resource "github.com/concourse/registry-image-resource"
+)
+
+var _ = Describe("Source", func() {
+	It("should unmarshal tag int value into a string", func() {
+		var source resource.Source
+		raw := []byte(`{ "tag": 0 }`)
+
+		err := json.Unmarshal(raw, &source)
+		Expect(err).ToNot(HaveOccurred())
+		Expect(source.Tag).To(Equal(resource.Tag("0")))
+	})
+
+	It("should unmarshal tag string value into a string", func() {
+		var source resource.Source
+		raw := []byte(`{ "tag": "foo" }`)
+
+		err := json.Unmarshal(raw, &source)
+		Expect(err).ToNot(HaveOccurred())
+		Expect(source.Tag).To(Equal(resource.Tag("foo")))
+	})
+
+	It("should unmarshal tag '' value to latest", func() {
+		var source resource.Source
+		raw := []byte(`{ "tag": "" }`)
+
+		err := json.Unmarshal(raw, &source)
+		Expect(err).ToNot(HaveOccurred())
+		Expect(source.Tag).To(Equal(resource.Tag("latest")))
+	})
+
+	It("should marshal a tag back out to a string", func() {
+		source := resource.Source{
+			Tag: "0",
+		}
+
+		json, err := json.Marshal(source)
+		Expect(err).ToNot(HaveOccurred())
+		Expect(strings.Contains(string(json[:]), `"tag":"0"`)).To(BeTrue())
+	})
+})