-
Notifications
You must be signed in to change notification settings - Fork 61
/
query.go
200 lines (164 loc) · 4.81 KB
/
query.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// 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 packmanager
import (
"kraftkit.sh/config"
"kraftkit.sh/unikraft"
"kraftkit.sh/utils"
)
// Query is the request structure with associated attributes which are used to
// search the package manager's catalog
type Query struct {
// Source specifies where the origin of the package
source string
// Types specifies the associated list of possible types for the package
types []unikraft.ComponentType
// Name specifies the name of the package
name string
// Version specifies the version of the package
version string
// useCache forces the package manager to update values using what it has
// locally.
useCache bool
// Auth contains required authentication for the query.
auths map[string]config.AuthConfig
// Selects all the packages
// (Currently, being used to prune all the packages on the host machine)
all bool
// If set to true then no manifest package will be deleted
// (Currently, being used to prune all the packages on the host machine)
noManifestPackage bool
// If set to true then no OCI package will be deleted
// (Currently, being used to prune all the packages on the host machine)
noOCIPackage bool
}
// Source specifies where the origin of the package
func (query *Query) Source() string {
return query.source
}
// Types specifies the associated list of possible types for the package
func (query *Query) Types() []unikraft.ComponentType {
return query.types
}
// Name specifies the name of the package
func (query *Query) Name() string {
return query.name
}
// Version specifies the version of the package
func (query *Query) Version() string {
return query.version
}
// UseCache indicates whether the package manager should use any existing cache.
func (query *Query) UseCache() bool {
return query.useCache
}
// Auth returns authentication configuration for a given domain or nil if the
// domain does not have (or require) any authentication.
func (query *Query) Auths() map[string]config.AuthConfig {
return query.auths
}
// All returns the value set for all.
func (query *Query) All() bool {
return query.all
}
func (query *Query) NoManifestPackage() bool {
return query.noManifestPackage
}
func (query *Query) NoOCIPackage() bool {
return query.noOCIPackage
}
func (query *Query) Fields() map[string]interface{} {
return map[string]interface{}{
"name": query.name,
"version": query.version,
"source": query.source,
"types": query.types,
"cache": query.useCache,
"auth": query.auths != nil,
}
}
// QueryOption is a method-option which sets a specific query parameter.
type QueryOption func(*Query)
// NewQuery returns the finalized query given the provided options.
func NewQuery(qopts ...QueryOption) *Query {
query := Query{}
for _, qopt := range qopts {
qopt(&query)
}
return &query
}
// WithSource sets the query parameter for the origin source of the package.
func WithSource(source string) QueryOption {
return func(query *Query) {
query.source = source
}
}
// WithTypes sets the query parameter for the specific Unikraft types to search
// for.
func WithTypes(types ...unikraft.ComponentType) QueryOption {
return func(query *Query) {
query.types = types
}
}
// WithName sets the query parameter for the name of the package.
func WithName(name string) QueryOption {
return func(query *Query) {
query.name = name
}
}
// WithVersion sets the query parameter for the version of the package.
func WithVersion(version string) QueryOption {
return func(query *Query) {
query.version = version
}
}
// WithCache sets whether to use local caching when making the query.
func WithCache(useCache bool) QueryOption {
return func(query *Query) {
query.useCache = useCache
}
}
// WithAuthConfig sets the the required authorization for when making the query.
func WithAuthConfig(auths map[string]config.AuthConfig) QueryOption {
return func(query *Query) {
query.auths = auths
}
}
func WithAll(all bool) QueryOption {
return func(query *Query) {
query.all = all
}
}
func WithNoManifestPackage(noManifestPackage bool) QueryOption {
return func(query *Query) {
query.noManifestPackage = noManifestPackage
}
}
func WithNoOCIPackage(noOCIPackage bool) QueryOption {
return func(query *Query) {
query.noOCIPackage = noOCIPackage
}
}
func (cq Query) String() string {
s := ""
if len(cq.types) == 1 {
s += string(cq.types[0]) + "-"
} else if len(cq.types) > 1 {
var types []string
for _, t := range cq.types {
types = append(types, string(t))
}
s += "{" + utils.ListJoinStr(types, ", ") + "}-"
}
if len(cq.name) > 0 {
s += cq.name
} else {
s += "*"
}
if len(cq.version) > 0 {
s += ":" + cq.version
}
return s
}