-
Notifications
You must be signed in to change notification settings - Fork 63
/
pull_options.go
166 lines (139 loc) · 4.09 KB
/
pull_options.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package pack
import (
"kraftkit.sh/config"
)
// PullOptions contains the list of options which can be set for pulling a
// package.
type PullOptions struct {
auths map[string]config.AuthConfig
architecture string
platform string
version string
calculateChecksum bool
onProgress func(progress float64)
workdir string
useCache bool
}
// Auths returns the set authentication config for a given domain or nil if the
// domain was not found.
func (ppo *PullOptions) Auths(domain string) *config.AuthConfig {
if auth, ok := ppo.auths[domain]; ok {
return &auth
}
return nil
}
// Architecture to pull.
func (ppo *PullOptions) Architecture() string {
return ppo.architecture
}
// Platform to pull.
func (ppo *PullOptions) Platform() string {
return ppo.platform
}
// OnProgress calls (if set) an embedded progress function which can be used to
// update an external progress bar, for example.
func (ppo *PullOptions) OnProgress(progress float64) {
if ppo.onProgress != nil {
ppo.onProgress(progress)
}
}
// Workdir returns the set working directory as part of the pull request
func (ppo *PullOptions) Workdir() string {
return ppo.workdir
}
// Version returns
func (ppo *PullOptions) Version() string {
return ppo.version
}
// CalculateChecksum returns whether the pull request should perform a check of
// the resource sum.
func (ppo *PullOptions) CalculateChecksum() bool {
return ppo.calculateChecksum
}
// UseCache returns whether the pull should redirect to using a local cache if
// available.
func (ppo *PullOptions) UseCache() bool {
return ppo.useCache
}
// PullOption is an option function which is used to modify PullOptions.
type PullOption func(opts *PullOptions) error
// NewPullOptions creates PullOptions
func NewPullOptions(opts ...PullOption) (*PullOptions, error) {
options := &PullOptions{}
for _, o := range opts {
err := o(options)
if err != nil {
return nil, err
}
}
return options, nil
}
// WithPullAuthConfig sets the authentication config to use when pulling the
// package.
func WithPullAuthConfig(auth map[string]config.AuthConfig) PullOption {
return func(opts *PullOptions) error {
if opts.auths == nil {
opts.auths = map[string]config.AuthConfig{}
}
for k, v := range auth {
opts.auths[k] = v
}
return nil
}
}
// WithPullArchitecture requests a given architecture (if applicable)
func WithPullArchitecture(arch string) PullOption {
return func(opts *PullOptions) error {
opts.architecture = arch
return nil
}
}
// WithPullPlatform requests a given platform (if applicable).
func WithPullPlatform(plat string) PullOption {
return func(opts *PullOptions) error {
opts.platform = plat
return nil
}
}
// WithPullProgressFunc set an optional progress function which is used as a
// callback during the transmission of the package and the host.
func WithPullProgressFunc(onProgress func(progress float64)) PullOption {
return func(opts *PullOptions) error {
opts.onProgress = onProgress
return nil
}
}
// WithPullWorkdir set the working directory context of the pull such that the
// resources of the package are placed there appropriately.
func WithPullWorkdir(workdir string) PullOption {
return func(opts *PullOptions) error {
opts.workdir = workdir
return nil
}
}
// WithPullChecksum to set whether to calculate and compare the checksum of the
// package.
func WithPullChecksum(calc bool) PullOption {
return func(opts *PullOptions) error {
opts.calculateChecksum = calc
return nil
}
}
// WithPullCache to set whether use cache if possible.
func WithPullCache(cache bool) PullOption {
return func(opts *PullOptions) error {
opts.useCache = cache
return nil
}
}
// WithPullVersion sets the version that should be pulled.
func WithPullVersion(version string) PullOption {
return func(opts *PullOptions) error {
opts.version = version
return nil
}
}