| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package main
- import (
- "encoding/json"
- "net/http"
- "os"
- "path/filepath"
- "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,
- })
- 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)
- }
- if len(os.Args) < 2 {
- logrus.Errorf("destination path not specified")
- os.Exit(1)
- return
- }
- src := os.Args[1]
- 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
- }
- imagePath := filepath.Join(src, req.Params.Image)
- img, err := tarball.ImageFromPath(imagePath, 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")
- }
|