-
Notifications
You must be signed in to change notification settings - Fork 63
/
pull_options.go
122 lines (103 loc) · 3.16 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
// 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
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
}
// 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
}
// 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
}
}
// 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
}
}