...

Source file src/github.com/google/go-containerregistry/pkg/crane/push.go

Documentation: github.com/google/go-containerregistry/pkg/crane

     1  // Copyright 2018 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package crane
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/google/go-containerregistry/pkg/name"
    21  	v1 "github.com/google/go-containerregistry/pkg/v1"
    22  	"github.com/google/go-containerregistry/pkg/v1/remote"
    23  	"github.com/google/go-containerregistry/pkg/v1/tarball"
    24  )
    25  
    26  // Load reads the tarball at path as a v1.Image.
    27  func Load(path string, opt ...Option) (v1.Image, error) {
    28  	return LoadTag(path, "", opt...)
    29  }
    30  
    31  // LoadTag reads a tag from the tarball at path as a v1.Image.
    32  // If tag is "", will attempt to read the tarball as a single image.
    33  func LoadTag(path, tag string, opt ...Option) (v1.Image, error) {
    34  	if tag == "" {
    35  		return tarball.ImageFromPath(path, nil)
    36  	}
    37  
    38  	o := makeOptions(opt...)
    39  	t, err := name.NewTag(tag, o.Name...)
    40  	if err != nil {
    41  		return nil, fmt.Errorf("parsing tag %q: %w", tag, err)
    42  	}
    43  	return tarball.ImageFromPath(path, &t)
    44  }
    45  
    46  // Push pushes the v1.Image img to a registry as dst.
    47  func Push(img v1.Image, dst string, opt ...Option) error {
    48  	o := makeOptions(opt...)
    49  	tag, err := name.ParseReference(dst, o.Name...)
    50  	if err != nil {
    51  		return fmt.Errorf("parsing reference %q: %w", dst, err)
    52  	}
    53  	return remote.Write(tag, img, o.Remote...)
    54  }
    55  
    56  // Upload pushes the v1.Layer to a given repo.
    57  func Upload(layer v1.Layer, repo string, opt ...Option) error {
    58  	o := makeOptions(opt...)
    59  	ref, err := name.NewRepository(repo, o.Name...)
    60  	if err != nil {
    61  		return fmt.Errorf("parsing repo %q: %w", repo, err)
    62  	}
    63  
    64  	return remote.WriteLayer(ref, layer, o.Remote...)
    65  }
    66  

View as plain text