Parcourir la source

experimental support for image pushing

so experimental that i can't build this on my own laptop for some
reason.
Alex Suraci il y a 7 ans
Parent
commit
678235b764
4 fichiers modifiés avec 78 ajouts et 11 suppressions
  1. 68 2
      cmd/out/main.go
  2. 1 2
      go.mod
  3. 2 7
      go.sum
  4. 7 0
      types.go

+ 68 - 2
cmd/out/main.go

@@ -1,17 +1,83 @@
 package main
 
 import (
+	"encoding/json"
+	"net/http"
 	"os"
 
+	"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/remote"
+	"github.com/google/go-containerregistry/pkg/v1/tarball"
 	"github.com/sirupsen/logrus"
+
+	resource "github.com/concourse/registry-image-resource"
 )
 
+type OutRequest struct {
+	Source resource.Source    `json:"source"`
+	Params resource.PutParams `json:"params"`
+}
+
+type OutResponse struct {
+	Version  resource.Version         `json:"version"`
+	Metadata []resource.MetadataField `json:"metadata"`
+}
+
 func main() {
 	logrus.SetOutput(os.Stderr)
 	logrus.SetFormatter(&logrus.TextFormatter{
 		ForceColors: true,
 	})
 
-	logrus.Error("not implemented")
-	os.Exit(1)
+	color.NoColor = false
+
+	var req OutRequest
+	decoder := json.NewDecoder(os.Stdin)
+	decoder.DisallowUnknownFields()
+	err := decoder.Decode(&req)
+	if err != nil {
+		logrus.Errorf("invalid payload: %s", err)
+		os.Exit(1)
+		return
+	}
+
+	if req.Source.Debug {
+		logrus.SetLevel(logrus.DebugLevel)
+	}
+
+	logrus.Warnln("'put' is experimental, untested, and subject to change!")
+
+	ref := req.Source.Repository + ":" + req.Source.Tag()
+
+	n, err := name.ParseReference(ref, name.WeakValidation)
+	if err != nil {
+		logrus.Errorf("could not resolve repository/tag reference: %s", err)
+		os.Exit(1)
+		return
+	}
+
+	img, err := tarball.ImageFromPath(req.Params.Image, nil)
+	if err != nil {
+		logrus.Errorf("could not load image from path '%s': %s", req.Params.Image, err)
+		os.Exit(1)
+		return
+	}
+
+	auth := &authn.Basic{
+		Username: req.Source.Username,
+		Password: req.Source.Password,
+	}
+
+	logrus.Infof("pushing to %s", ref)
+
+	err = remote.Write(n, img, auth, http.DefaultTransport, remote.WriteOptions{})
+	if err != nil {
+		logrus.Errorf("failed to upload image: %s", err)
+		os.Exit(1)
+		return
+	}
+
+	logrus.Info("pushed")
 }

+ 1 - 2
go.mod

@@ -8,10 +8,9 @@ require (
 	github.com/fsnotify/fsnotify v1.4.7 // indirect
 	github.com/golang/protobuf v1.1.0 // indirect
 	github.com/google/go-cmp v0.2.0 // indirect
-	github.com/google/go-containerregistry v0.0.0-20180801194910-5f7b0e489541
+	github.com/google/go-containerregistry v0.0.0-20180919161554-52f3c54ec23c
 	github.com/hpcloud/tail v1.0.0 // indirect
 	github.com/kr/pretty v0.1.0 // indirect
-	github.com/kr/pty v1.1.2 // indirect
 	github.com/mattn/go-colorable v0.0.9 // indirect
 	github.com/mattn/go-isatty v0.0.3 // indirect
 	github.com/onsi/ginkgo v1.6.0

+ 2 - 7
go.sum

@@ -12,14 +12,13 @@ github.com/golang/protobuf v1.1.0 h1:0iH4Ffd/meGoXqF2lSAhZHt8X+cPgkfn/cb6Cce5Vpc
 github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
 github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
-github.com/google/go-containerregistry v0.0.0-20180801194910-5f7b0e489541 h1:smMOjJQk5KXcf5S0WXoKgcR0oQVdZS/7tb57VAl9+tQ=
-github.com/google/go-containerregistry v0.0.0-20180801194910-5f7b0e489541/go.mod h1:yZAFP63pRshzrEYLXLGPmUt0Ay+2zdjmMN1loCnRLUk=
+github.com/google/go-containerregistry v0.0.0-20180919161554-52f3c54ec23c h1:cFhR+IyTBe1xfu/gVA04ZjgvC37fDOgdeROZ7u9ekjM=
+github.com/google/go-containerregistry v0.0.0-20180919161554-52f3c54ec23c/go.mod h1:yZAFP63pRshzrEYLXLGPmUt0Ay+2zdjmMN1loCnRLUk=
 github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
 github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
 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/pty v1.1.2/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=
@@ -38,16 +37,12 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
 github.com/vbauerster/mpb v3.3.1+incompatible h1:895YxWn6TBP+leAp2kml6H+Rccyo6kXcqKZpbig1wuI=
 github.com/vbauerster/mpb v3.3.1+incompatible/go.mod h1:zAHG26FUhVKETRu+MWqYXcI70POlC6N8up9p1dID7SU=
-golang.org/x/crypto v0.0.0-20180802221240-56440b844dfe h1:APBCFlxGVQi3YDSHtTbNXRZhDEuz9rrnVPXZA4YbUx8=
-golang.org/x/crypto v0.0.0-20180802221240-56440b844dfe/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
 golang.org/x/crypto v0.0.0-20180807104621-f027049dab0a h1:PulT0Y50PcfTWomfsD39bSQyVrjjWdIuJKfyR4nOCJw=
 golang.org/x/crypto v0.0.0-20180807104621-f027049dab0a/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
 golang.org/x/net v0.0.0-20180801234040-f4c29de78a2a h1:8fCF9zjAir2SP3N+axz9xs+0r4V8dqPzqsWO10t8zoo=
 golang.org/x/net v0.0.0-20180801234040-f4c29de78a2a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
-golang.org/x/sys v0.0.0-20180802203216-0ffbfd41fbef h1:ESfhYoBNk2UQGmavscFPKfwmc4ZTB2+UdQYsVw6Bq9M=
-golang.org/x/sys v0.0.0-20180802203216-0ffbfd41fbef/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20180806192500-2be389f392cd h1:KFYUs6SCkSktZ+xJWb5YbuSCJLLphbTsg0kvyirtlQ8=
 golang.org/x/sys v0.0.0-20180806192500-2be389f392cd/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=

+ 7 - 0
types.go

@@ -6,6 +6,9 @@ type Source struct {
 	Repository string `json:"repository"`
 	RawTag     string `json:"tag"`
 
+	Username string `json:"username"`
+	Password string `json:"password"`
+
 	Debug bool `json:"debug"`
 }
 
@@ -37,3 +40,7 @@ func (p GetParams) Format() string {
 
 	return p.RawFormat
 }
+
+type PutParams struct {
+	Image string `json:"image"`
+}