...

Source file src/github.com/google/go-containerregistry/pkg/v1/tarball/progress_test.go

Documentation: github.com/google/go-containerregistry/pkg/v1/tarball

     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 tarball_test
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"io"
    21  	"os"
    22  
    23  	v1 "github.com/google/go-containerregistry/pkg/v1"
    24  	"github.com/google/go-containerregistry/pkg/v1/tarball"
    25  )
    26  
    27  func ExampleWithProgress() {
    28  	// buffered channel to make the example test easier
    29  	c := make(chan v1.Update, 200)
    30  	// Make a tempfile for tarball writes.
    31  	fp, err := os.CreateTemp("", "")
    32  	if err != nil {
    33  		fmt.Printf("error creating temp file: %v\n", err)
    34  		return
    35  	}
    36  	defer fp.Close()
    37  	defer os.Remove(fp.Name())
    38  
    39  	img, err := tarball.ImageFromPath("testdata/test_image_1.tar", nil)
    40  	go func() {
    41  		_ = tarball.WriteToFile(fp.Name(), nil, img, tarball.WithProgress(c))
    42  	}()
    43  	for update := range c {
    44  		switch {
    45  		case update.Error != nil && errors.Is(update.Error, io.EOF):
    46  			fmt.Fprintf(os.Stderr, "receive error message: %v\n", err)
    47  			fmt.Printf("%d/%d", update.Complete, update.Total)
    48  			// Output: 4096/4096
    49  			return
    50  		case update.Error != nil:
    51  			fmt.Printf("error writing tarball: %v\n", update.Error)
    52  			return
    53  		default:
    54  			fmt.Fprintf(os.Stderr, "receive update: %#v\n", update)
    55  		}
    56  	}
    57  }
    58  

View as plain text