@@ -2,13 +2,15 @@ package image
22
33import (
44 "errors"
5+ "fmt"
56 "io"
67
78 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
89
910 "github.com/docker/docker/api/types/build"
1011 "github.com/docker/docker/api/types/image"
1112 "github.com/docker/go-sdk/client"
13+ "github.com/docker/go-sdk/config"
1214)
1315
1416// BuildOption is a function that configures the build options.
@@ -40,9 +42,39 @@ func WithBuildOptions(options build.ImageBuildOptions) BuildOption {
4042type PullOption func (* pullOptions ) error
4143
4244type pullOptions struct {
43- client client.SDKClient
44- pullOptions image.PullOptions
45- pullHandler func (r io.ReadCloser ) error
45+ client client.SDKClient
46+ pullOptions image.PullOptions
47+ pullHandler func (r io.ReadCloser ) error
48+ credentialsFn func (string ) (string , string , error )
49+ }
50+
51+ // WithCredentialsFn sets the function to retrieve credentials for an image to be pulled
52+ func WithCredentialsFn (credentialsFn func (string ) (string , string , error )) PullOption {
53+ return func (opts * pullOptions ) error {
54+ opts .credentialsFn = credentialsFn
55+ return nil
56+ }
57+ }
58+
59+ // WithCredentialsFromConfig configures pull to retrieve credentials from the CLI config
60+ func WithCredentialsFromConfig (opts * pullOptions ) error {
61+ opts .credentialsFn = func (imageName string ) (string , string , error ) {
62+ authConfigs , err := config .AuthConfigs (imageName )
63+ if err != nil {
64+ return "" , "" , err
65+ }
66+
67+ // there must be only one auth config for the image
68+ if len (authConfigs ) > 1 {
69+ return "" , "" , fmt .Errorf ("multiple auth configs found for image %s, expected only one" , imageName )
70+ }
71+
72+ for _ , ac := range authConfigs {
73+ return ac .Username , ac .Password , nil
74+ }
75+ return "" , "" , nil
76+ }
77+ return nil
4678}
4779
4880// WithPullClient sets the pull client used to pull the image.
0 commit comments