...

Source file src/oras.land/oras-go/pkg/content/multireader.go

Documentation: oras.land/oras-go/pkg/content

     1  /*
     2  Copyright The ORAS Authors.
     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  
    16  package content
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"io"
    22  
    23  	"github.com/containerd/containerd/remotes"
    24  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    25  )
    26  
    27  // MultiReader store to read content from multiple stores. It finds the content by asking each underlying
    28  // store to find the content, which it does based on the hash.
    29  //
    30  // Example:
    31  //        fileStore := NewFileStore(rootPath)
    32  //        memoryStore := NewMemoryStore()
    33  //        // load up content in fileStore and memoryStore
    34  //        multiStore := MultiReader([]content.Provider{fileStore, memoryStore})
    35  //
    36  // You now can use multiStore anywhere that content.Provider is accepted
    37  type MultiReader struct {
    38  	stores []remotes.Fetcher
    39  }
    40  
    41  // AddStore add a store to read from
    42  func (m *MultiReader) AddStore(store ...remotes.Fetcher) {
    43  	m.stores = append(m.stores, store...)
    44  }
    45  
    46  // ReaderAt get a reader
    47  func (m MultiReader) Fetch(ctx context.Context, desc ocispec.Descriptor) (io.ReadCloser, error) {
    48  	for _, store := range m.stores {
    49  		r, err := store.Fetch(ctx, desc)
    50  		if r != nil && err == nil {
    51  			return r, nil
    52  		}
    53  	}
    54  	// we did not find any
    55  	return nil, fmt.Errorf("not found")
    56  }
    57  

View as plain text