github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/client/example_buildpack_downloader_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/pack/pkg/buildpack"
    13  	"github.com/buildpacks/pack/pkg/client"
    14  )
    15  
    16  // This example shows how to replace the buildpack downloader component
    17  func Example_buildpack_downloader() {
    18  	// create a context object
    19  	context := context.Background()
    20  
    21  	// initialize a pack client
    22  	pack, err := client.NewClient(client.WithBuildpackDownloader(&bpDownloader{}))
    23  	if err != nil {
    24  		panic(err)
    25  	}
    26  
    27  	// replace this with the location of a sample application
    28  	// For a list of prepared samples see the 'apps' folder at
    29  	// https://github.com/buildpacks/samples.
    30  	appPath := filepath.Join("testdata", "some-app")
    31  
    32  	// initialize our options
    33  	buildOpts := client.BuildOptions{
    34  		Image:        "pack-lib-test-image:0.0.1",
    35  		Builder:      "cnbs/sample-builder:bionic",
    36  		AppPath:      appPath,
    37  		Buildpacks:   []string{"some-buildpack:1.2.3"},
    38  		TrustBuilder: func(string) bool { return true },
    39  	}
    40  
    41  	// build an image
    42  	_ = pack.Build(context, buildOpts)
    43  
    44  	// Output: custom buildpack downloader called
    45  }
    46  
    47  var _ client.BuildpackDownloader = (*bpDownloader)(nil)
    48  
    49  type bpDownloader struct{}
    50  
    51  func (f *bpDownloader) Download(ctx context.Context, buildpackURI string, opts buildpack.DownloadOptions) (buildpack.BuildModule, []buildpack.BuildModule, error) {
    52  	fmt.Println("custom buildpack downloader called")
    53  	return nil, nil, errors.New("not implemented")
    54  }