|
| 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 | + "fmt" |
| 19 | + "io" |
| 20 | + "io/ioutil" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/google/go-containerregistry/pkg/name" |
| 24 | + v1 "github.com/google/go-containerregistry/pkg/v1" |
| 25 | + "github.com/google/go-containerregistry/pkg/v1/remote" |
| 26 | + "github.com/google/go-containerregistry/pkg/v1/tarball" |
| 27 | +) |
| 28 | + |
| 29 | +func ExampleWithProgress() { |
| 30 | + /* calculations for this test: |
| 31 | + The image we are using is docker.io/library/alpine:3.10 |
| 32 | + its size on disk is 2800640 |
| 33 | + The filesizes inside are: |
| 34 | + -rw-r--r-- 0 0 0 1509 Jan 1 1970 sha256:be4e4bea2c2e15b403bb321562e78ea84b501fb41497472e91ecb41504e8a27c |
| 35 | + -rw-r--r-- 0 0 0 2795580 Jan 1 1970 21c83c5242199776c232920ddb58cfa2a46b17e42ed831ca9001c8dbc532d22d.tar.gz |
| 36 | + -rw-r--r-- 0 0 0 216 Jan 1 1970 manifest.json |
| 37 | + when rounding each to a 512-byte block, plus the header, we get: |
| 38 | + 1509 -> 1536 + 512 = 2048 |
| 39 | + 2795580 -> 2796032 + 512 = 2796544 |
| 40 | + 216 -> 512 + 512 = 1024 |
| 41 | + add in 2 blocks of all 0x00 to indicate end of archive |
| 42 | + = 1024 |
| 43 | + ------- |
| 44 | + Total: 2800640 |
| 45 | + */ |
| 46 | + // buffered channel to make the example test easier |
| 47 | + c := make(chan v1.Update, 200) |
| 48 | + // Make a tempfile for tarball writes. |
| 49 | + fp, err := ioutil.TempFile("", "") |
| 50 | + if err != nil { |
| 51 | + fmt.Printf("error creating temp file: %v\n", err) |
| 52 | + return |
| 53 | + } |
| 54 | + defer fp.Close() |
| 55 | + defer os.Remove(fp.Name()) |
| 56 | + |
| 57 | + tag, err := name.NewDigest("docker.io/library/alpine@sha256:f0e9534a598e501320957059cb2a23774b4d4072e37c7b2cf7e95b241f019e35", name.StrictValidation) |
| 58 | + if err != nil { |
| 59 | + fmt.Printf("error creating test tag: %v\n", err) |
| 60 | + return |
| 61 | + } |
| 62 | + desc, err := remote.Get(tag) |
| 63 | + if err != nil { |
| 64 | + fmt.Printf("error getting manifest: %v", err) |
| 65 | + return |
| 66 | + } |
| 67 | + img, err := desc.Image() |
| 68 | + if err != nil { |
| 69 | + fmt.Printf("error image: %v", err) |
| 70 | + return |
| 71 | + } |
| 72 | + go func() { |
| 73 | + _ = tarball.WriteToFile(fp.Name(), tag, img, tarball.WithProgress(c)) |
| 74 | + }() |
| 75 | + for update := range c { |
| 76 | + switch { |
| 77 | + case update.Error != nil && update.Error == io.EOF: |
| 78 | + fmt.Fprintf(os.Stderr, "receive error message: %v\n", err) |
| 79 | + fmt.Printf("%d/%d", update.Complete, update.Total) |
| 80 | + // Output: 2800640/2800640 |
| 81 | + return |
| 82 | + case update.Error != nil: |
| 83 | + fmt.Printf("error writing tarball: %v\n", update.Error) |
| 84 | + return |
| 85 | + default: |
| 86 | + fmt.Fprintf(os.Stderr, "receive update: %#v\n", update) |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments