github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/client/example_fetcher_test.go (about)

     1  //go:build !windows && example
     2  // +build !windows,example
     3  
     4  package client_test
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  	"fmt"
    10  	"path/filepath"
    11  
    12  	"github.com/buildpacks/imgutil"
    13  
    14  	"github.com/buildpacks/pack/pkg/client"
    15  	"github.com/buildpacks/pack/pkg/image"
    16  )
    17  
    18  // This example shows how to replace the image fetcher component
    19  func Example_fetcher() {
    20  	// create a context object
    21  	context := context.Background()
    22  
    23  	// initialize a pack client
    24  	pack, err := client.NewClient(client.WithFetcher(&fetcher{}))
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  
    29  	// replace this with the location of a sample application
    30  	// For a list of prepared samples see the 'apps' folder at
    31  	// https://github.com/buildpacks/samples.
    32  	appPath := filepath.Join("testdata", "some-app")
    33  
    34  	// initialize our options
    35  	buildOpts := client.BuildOptions{
    36  		Image:        "pack-lib-test-image:0.0.1",
    37  		Builder:      "cnbs/sample-builder:bionic",
    38  		AppPath:      appPath,
    39  		TrustBuilder: func(string) bool { return true },
    40  	}
    41  
    42  	// build an image
    43  	_ = pack.Build(context, buildOpts)
    44  
    45  	// Output: custom fetcher called
    46  }
    47  
    48  var _ client.ImageFetcher = (*fetcher)(nil)
    49  
    50  type fetcher struct{}
    51  
    52  func (f *fetcher) Fetch(_ context.Context, imageName string, _ image.FetchOptions) (imgutil.Image, error) {
    53  	fmt.Println("custom fetcher called")
    54  	return nil, errors.New("not implemented")
    55  }
    56  
    57  func (f *fetcher) CheckReadAccess(_ string, _ FetchOptions) bool {
    58  	return true
    59  }