-
Notifications
You must be signed in to change notification settings - Fork 63
/
application_options.go
212 lines (183 loc) · 5.14 KB
/
application_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
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
201
202
203
204
205
206
207
208
209
210
211
212
// 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 app
import (
"fmt"
"os"
"path/filepath"
"kraftkit.sh/kconfig"
"kraftkit.sh/unikraft"
"kraftkit.sh/unikraft/app/volume"
"kraftkit.sh/unikraft/component"
"kraftkit.sh/unikraft/core"
"kraftkit.sh/unikraft/lib"
"kraftkit.sh/unikraft/runtime"
"kraftkit.sh/unikraft/target"
"kraftkit.sh/unikraft/template"
)
// ApplicationOption is a function that manipulates the instantiation of an
// Application.
type ApplicationOption func(ao *application) error
// NewApplicationFromOptions accepts a series of options and returns a rendered
// *ApplicationConfig structure
func NewApplicationFromOptions(aopts ...ApplicationOption) (Application, error) {
var err error
ac := &application{
configuration: kconfig.KeyValueMap{},
}
for _, o := range aopts {
if err := o(ac); err != nil {
return nil, fmt.Errorf("could not apply option: %v", err)
}
}
if ac.name != "" {
ac.configuration.Set(unikraft.UK_NAME, ac.name)
}
if ac.outDir == "" {
if ac.workingDir == "" {
ac.workingDir, err = os.Getwd()
if err != nil {
return nil, err
}
}
ac.outDir = filepath.Join(ac.workingDir, unikraft.BuildDir)
}
if ac.unikraft != nil && len(ac.unikraft.Source()) > 0 {
if p, err := os.Stat(ac.unikraft.Source()); err == nil && p.IsDir() {
ac.configuration.Set(unikraft.UK_BASE, ac.unikraft.Source())
}
}
return ac, nil
}
// WithName sets the application component name
func WithName(name string) ApplicationOption {
return func(ac *application) error {
ac.name = name
return nil
}
}
// WithVersion sets the application version
func WithVersion(version string) ApplicationOption {
return func(ac *application) error {
ac.version = version
return nil
}
}
// WithWorkingDir sets the application's working directory
func WithWorkingDir(workingDir string) ApplicationOption {
return func(ac *application) error {
ac.workingDir = workingDir
return nil
}
}
// WithSource sets the library's source which indicates where it was retrieved
// and in component context and not the origin.
func WithSource(source string) ApplicationOption {
return func(ac *application) error {
ac.source = source
return nil
}
}
// WithFilename sets the application's file name
func WithFilename(filename string) ApplicationOption {
return func(ac *application) error {
ac.filename = filename
return nil
}
}
// WithOutDir sets the application's output directory
func WithOutDir(outDir string) ApplicationOption {
return func(ac *application) error {
ac.outDir = outDir
return nil
}
}
// WithRuntime sets the application's runtime
func WithRuntime(rt *runtime.Runtime) ApplicationOption {
return func(ac *application) error {
ac.runtime = rt
return nil
}
}
// WithRootfs sets the application's rootfs
func WithRootfs(rootfs string) ApplicationOption {
return func(ac *application) error {
ac.rootfs = rootfs
return nil
}
}
// WithTemplate sets the application's template
func WithTemplate(template *template.TemplateConfig) ApplicationOption {
return func(ac *application) error {
ac.template = template
return nil
}
}
// WithCommand sets the command-line arguments for the application.
func WithCommand(command ...string) ApplicationOption {
return func(ac *application) error {
ac.command = command
return nil
}
}
// WithUnikraft sets the application's core
func WithUnikraft(unikraft *core.UnikraftConfig) ApplicationOption {
return func(ac *application) error {
ac.unikraft = unikraft
return nil
}
}
// WithLibraries sets the application's library list
func WithLibraries(libraries map[string]*lib.LibraryConfig) ApplicationOption {
return func(ac *application) error {
ac.libraries = libraries
return nil
}
}
// WithTargets sets the application's target list
func WithTargets(targets []*target.TargetConfig) ApplicationOption {
return func(ac *application) error {
ac.targets = targets
return nil
}
}
// WithExtensions sets the application's extension list
func WithExtensions(extensions component.Extensions) ApplicationOption {
return func(ac *application) error {
ac.extensions = extensions
return nil
}
}
// WithKraftfile sets the application's kraft yaml file
func WithKraftfile(kraftfile *Kraftfile) ApplicationOption {
return func(ac *application) error {
ac.kraftfile = kraftfile
return nil
}
}
// WithConfiguration sets the application's kconfig list
func WithConfiguration(config ...*kconfig.KeyValue) ApplicationOption {
return func(ac *application) error {
if ac.configuration == nil {
ac.configuration = kconfig.KeyValueMap{}
}
ac.configuration.Override(config...)
return nil
}
}
// WithVolumes sets the list of volumes to be supplied to the unikernel
func WithVolumes(volumes ...*volume.VolumeConfig) ApplicationOption {
return func(ac *application) error {
ac.volumes = volumes
return nil
}
}
// WithEnv sets the list of environment variables.
func WithEnv(env map[string]string) ApplicationOption {
return func(ac *application) error {
ac.env = env
return nil
}
}