...

Source file src/github.com/google/go-containerregistry/pkg/v1/static/layer.go

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

     1  // Copyright 2021 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 static
    16  
    17  import (
    18  	"bytes"
    19  	"io"
    20  	"sync"
    21  
    22  	v1 "github.com/google/go-containerregistry/pkg/v1"
    23  	"github.com/google/go-containerregistry/pkg/v1/types"
    24  )
    25  
    26  // NewLayer returns a layer containing the given bytes, with the given mediaType.
    27  //
    28  // Contents will not be compressed.
    29  func NewLayer(b []byte, mt types.MediaType) v1.Layer {
    30  	return &staticLayer{b: b, mt: mt}
    31  }
    32  
    33  type staticLayer struct {
    34  	b  []byte
    35  	mt types.MediaType
    36  
    37  	once sync.Once
    38  	h    v1.Hash
    39  }
    40  
    41  func (l *staticLayer) Digest() (v1.Hash, error) {
    42  	var err error
    43  	// Only calculate digest the first time we're asked.
    44  	l.once.Do(func() {
    45  		l.h, _, err = v1.SHA256(bytes.NewReader(l.b))
    46  	})
    47  	return l.h, err
    48  }
    49  
    50  func (l *staticLayer) DiffID() (v1.Hash, error) {
    51  	return l.Digest()
    52  }
    53  
    54  func (l *staticLayer) Compressed() (io.ReadCloser, error) {
    55  	return io.NopCloser(bytes.NewReader(l.b)), nil
    56  }
    57  
    58  func (l *staticLayer) Uncompressed() (io.ReadCloser, error) {
    59  	return io.NopCloser(bytes.NewReader(l.b)), nil
    60  }
    61  
    62  func (l *staticLayer) Size() (int64, error) {
    63  	return int64(len(l.b)), nil
    64  }
    65  
    66  func (l *staticLayer) MediaType() (types.MediaType, error) {
    67  	return l.mt, nil
    68  }
    69  

View as plain text