...

Source file src/github.com/google/go-containerregistry/pkg/v1/daemon/options.go

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

     1  // Copyright 2018 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 daemon
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  
    21  	"github.com/docker/docker/api/types"
    22  	api "github.com/docker/docker/api/types/image"
    23  	"github.com/docker/docker/client"
    24  )
    25  
    26  // ImageOption is an alias for Option.
    27  // Deprecated: Use Option instead.
    28  type ImageOption Option
    29  
    30  // Option is a functional option for daemon operations.
    31  type Option func(*options)
    32  
    33  type options struct {
    34  	ctx      context.Context
    35  	client   Client
    36  	buffered bool
    37  }
    38  
    39  var defaultClient = func() (Client, error) {
    40  	return client.NewClientWithOpts(client.FromEnv)
    41  }
    42  
    43  func makeOptions(opts ...Option) (*options, error) {
    44  	o := &options{
    45  		buffered: true,
    46  		ctx:      context.Background(),
    47  	}
    48  	for _, opt := range opts {
    49  		opt(o)
    50  	}
    51  
    52  	if o.client == nil {
    53  		client, err := defaultClient()
    54  		if err != nil {
    55  			return nil, err
    56  		}
    57  		o.client = client
    58  	}
    59  	o.client.NegotiateAPIVersion(o.ctx)
    60  
    61  	return o, nil
    62  }
    63  
    64  // WithBufferedOpener buffers the image.
    65  func WithBufferedOpener() Option {
    66  	return func(o *options) {
    67  		o.buffered = true
    68  	}
    69  }
    70  
    71  // WithUnbufferedOpener streams the image to avoid buffering.
    72  func WithUnbufferedOpener() Option {
    73  	return func(o *options) {
    74  		o.buffered = false
    75  	}
    76  }
    77  
    78  // WithClient is a functional option to allow injecting a docker client.
    79  //
    80  // By default, github.com/docker/docker/client.FromEnv is used.
    81  func WithClient(client Client) Option {
    82  	return func(o *options) {
    83  		o.client = client
    84  	}
    85  }
    86  
    87  // WithContext is a functional option to pass through a context.Context.
    88  //
    89  // By default, context.Background() is used.
    90  func WithContext(ctx context.Context) Option {
    91  	return func(o *options) {
    92  		o.ctx = ctx
    93  	}
    94  }
    95  
    96  // Client represents the subset of a docker client that the daemon
    97  // package uses.
    98  type Client interface {
    99  	NegotiateAPIVersion(ctx context.Context)
   100  	ImageSave(context.Context, []string) (io.ReadCloser, error)
   101  	ImageLoad(context.Context, io.Reader, bool) (types.ImageLoadResponse, error)
   102  	ImageTag(context.Context, string, string) error
   103  	ImageInspectWithRaw(context.Context, string) (types.ImageInspect, []byte, error)
   104  	ImageHistory(context.Context, string) ([]api.HistoryResponseItem, error)
   105  }
   106  

View as plain text