summaryrefslogtreecommitdiffstats
path: root/pkg/gcrane/options.go
blob: 9d34c7dbf0404db0f064cac035749c06b4ae1fba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2019 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gcrane

import (
	"context"
	"net/http"
	"runtime"

	"github.com/google/go-containerregistry/pkg/authn"
	"github.com/google/go-containerregistry/pkg/crane"
	"github.com/google/go-containerregistry/pkg/v1/google"
	"github.com/google/go-containerregistry/pkg/v1/remote"
)

// Option is a functional option for gcrane operations.
type Option func(*options)

type options struct {
	jobs   int
	remote []remote.Option
	google []google.Option
	crane  []crane.Option
}

func makeOptions(opts ...Option) *options {
	o := &options{
		jobs: runtime.GOMAXPROCS(0),
		remote: []remote.Option{
			remote.WithAuthFromKeychain(Keychain),
		},
		google: []google.Option{
			google.WithAuthFromKeychain(Keychain),
		},
		crane: []crane.Option{
			crane.WithAuthFromKeychain(Keychain),
		},
	}

	for _, option := range opts {
		option(o)
	}

	return o
}

// WithJobs sets the number of concurrent jobs to run.
//
// The default number of jobs is GOMAXPROCS.
func WithJobs(jobs int) Option {
	return func(o *options) {
		o.jobs = jobs
	}
}

// WithTransport is a functional option for overriding the default transport
// for remote operations.
func WithTransport(t http.RoundTripper) Option {
	return func(o *options) {
		o.remote = append(o.remote, remote.WithTransport(t))
		o.google = append(o.google, google.WithTransport(t))
		o.crane = append(o.crane, crane.WithTransport(t))
	}
}

// WithUserAgent adds the given string to the User-Agent header for any HTTP
// requests.
func WithUserAgent(ua string) Option {
	return func(o *options) {
		o.remote = append(o.remote, remote.WithUserAgent(ua))
		o.google = append(o.google, google.WithUserAgent(ua))
		o.crane = append(o.crane, crane.WithUserAgent(ua))
	}
}

// WithContext is a functional option for setting the context.
func WithContext(ctx context.Context) Option {
	return func(o *options) {
		o.remote = append(o.remote, remote.WithContext(ctx))
		o.google = append(o.google, google.WithContext(ctx))
		o.crane = append(o.crane, crane.WithContext(ctx))
	}
}

// WithKeychain is a functional option for overriding the default
// authenticator for remote operations, using an authn.Keychain to find
// credentials.
//
// By default, gcrane will use gcrane.Keychain.
func WithKeychain(keys authn.Keychain) Option {
	return func(o *options) {
		// Replace the default keychain at position 0.
		o.remote[0] = remote.WithAuthFromKeychain(keys)
		o.google[0] = google.WithAuthFromKeychain(keys)
		o.crane[0] = crane.WithAuthFromKeychain(keys)
	}
}

// WithAuth is a functional option for overriding the default authenticator
// for remote operations.
//
// By default, gcrane will use gcrane.Keychain.
func WithAuth(auth authn.Authenticator) Option {
	return func(o *options) {
		// Replace the default keychain at position 0.
		o.remote[0] = remote.WithAuth(auth)
		o.google[0] = google.WithAuth(auth)
		o.crane[0] = crane.WithAuth(auth)
	}
}