...

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

Documentation: github.com/google/go-containerregistry/pkg/gcrane

     1  // Copyright 2019 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 gcrane
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  	"runtime"
    21  
    22  	"github.com/google/go-containerregistry/pkg/authn"
    23  	"github.com/google/go-containerregistry/pkg/crane"
    24  	"github.com/google/go-containerregistry/pkg/v1/google"
    25  	"github.com/google/go-containerregistry/pkg/v1/remote"
    26  )
    27  
    28  // Option is a functional option for gcrane operations.
    29  type Option func(*options)
    30  
    31  type options struct {
    32  	jobs   int
    33  	remote []remote.Option
    34  	google []google.Option
    35  	crane  []crane.Option
    36  }
    37  
    38  func makeOptions(opts ...Option) *options {
    39  	o := &options{
    40  		jobs: runtime.GOMAXPROCS(0),
    41  		remote: []remote.Option{
    42  			remote.WithAuthFromKeychain(Keychain),
    43  		},
    44  		google: []google.Option{
    45  			google.WithAuthFromKeychain(Keychain),
    46  		},
    47  		crane: []crane.Option{
    48  			crane.WithAuthFromKeychain(Keychain),
    49  		},
    50  	}
    51  
    52  	for _, option := range opts {
    53  		option(o)
    54  	}
    55  
    56  	return o
    57  }
    58  
    59  // WithJobs sets the number of concurrent jobs to run.
    60  //
    61  // The default number of jobs is GOMAXPROCS.
    62  func WithJobs(jobs int) Option {
    63  	return func(o *options) {
    64  		o.jobs = jobs
    65  	}
    66  }
    67  
    68  // WithTransport is a functional option for overriding the default transport
    69  // for remote operations.
    70  func WithTransport(t http.RoundTripper) Option {
    71  	return func(o *options) {
    72  		o.remote = append(o.remote, remote.WithTransport(t))
    73  		o.google = append(o.google, google.WithTransport(t))
    74  		o.crane = append(o.crane, crane.WithTransport(t))
    75  	}
    76  }
    77  
    78  // WithUserAgent adds the given string to the User-Agent header for any HTTP
    79  // requests.
    80  func WithUserAgent(ua string) Option {
    81  	return func(o *options) {
    82  		o.remote = append(o.remote, remote.WithUserAgent(ua))
    83  		o.google = append(o.google, google.WithUserAgent(ua))
    84  		o.crane = append(o.crane, crane.WithUserAgent(ua))
    85  	}
    86  }
    87  
    88  // WithContext is a functional option for setting the context.
    89  func WithContext(ctx context.Context) Option {
    90  	return func(o *options) {
    91  		o.remote = append(o.remote, remote.WithContext(ctx))
    92  		o.google = append(o.google, google.WithContext(ctx))
    93  		o.crane = append(o.crane, crane.WithContext(ctx))
    94  	}
    95  }
    96  
    97  // WithKeychain is a functional option for overriding the default
    98  // authenticator for remote operations, using an authn.Keychain to find
    99  // credentials.
   100  //
   101  // By default, gcrane will use gcrane.Keychain.
   102  func WithKeychain(keys authn.Keychain) Option {
   103  	return func(o *options) {
   104  		// Replace the default keychain at position 0.
   105  		o.remote[0] = remote.WithAuthFromKeychain(keys)
   106  		o.google[0] = google.WithAuthFromKeychain(keys)
   107  		o.crane[0] = crane.WithAuthFromKeychain(keys)
   108  	}
   109  }
   110  
   111  // WithAuth is a functional option for overriding the default authenticator
   112  // for remote operations.
   113  //
   114  // By default, gcrane will use gcrane.Keychain.
   115  func WithAuth(auth authn.Authenticator) Option {
   116  	return func(o *options) {
   117  		// Replace the default keychain at position 0.
   118  		o.remote[0] = remote.WithAuth(auth)
   119  		o.google[0] = google.WithAuth(auth)
   120  		o.crane[0] = crane.WithAuth(auth)
   121  	}
   122  }
   123  

View as plain text