-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
82 lines (71 loc) · 1.49 KB
/
config.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
const (
kernelImageContext = "kernel-image"
initrdImageContext = "initrd-image"
modulesContext = "kernel-modules"
)
type specFlag struct {
scheme string
ref string
}
type vmImageConfig struct {
kernel specFlag
initrd specFlag
modules specFlag
rootfs string
size string
}
func (f *specFlag) Set(s string) error {
if s == "" {
return nil
}
scheme, ref, ok := strings.Cut(s, "://")
if !ok {
if _, err := os.Stat(s); err == nil {
scheme = "local"
ref = s
ok = true
}
if !ok {
return fmt.Errorf("invalid format, must be <scheme>://<ref>: %s", s)
}
}
f.scheme = scheme
f.ref = ref
return nil
}
func (f *specFlag) isEmpty() bool {
return f.scheme == "" && f.ref == ""
}
func (f *specFlag) String() string {
if f.scheme == "" && f.ref == "" {
return ""
}
return f.scheme + "://" + f.ref
}
func getLocalContexts(cfg config) map[string]string {
var contexts map[string]string
get := func() map[string]string {
if contexts != nil {
return contexts
}
contexts = make(map[string]string)
return contexts
}
if cfg.ImageConfig.initrd.scheme == "local" {
get()[initrdImageContext] = filepath.Dir(cfg.ImageConfig.initrd.ref)
}
if cfg.ImageConfig.kernel.scheme == "local" {
get()[kernelImageContext] = filepath.Dir(cfg.ImageConfig.kernel.ref)
}
if cfg.ImageConfig.modules.scheme == "local" {
get()[modulesContext] = filepath.Dir(cfg.ImageConfig.modules.ref)
}
return contexts
}