-
Notifications
You must be signed in to change notification settings - Fork 61
/
transform.go
66 lines (55 loc) · 1.75 KB
/
transform.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
// 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 template
import (
"context"
"os"
"kraftkit.sh/kconfig"
"kraftkit.sh/unikraft"
"kraftkit.sh/unikraft/component"
"kraftkit.sh/utils"
)
// TransformFromSchema parses an input schema and returns an instantiated
// TemplateConfig
func TransformFromSchema(ctx context.Context, props interface{}) (interface{}, error) {
uk := unikraft.FromContext(ctx)
template := TemplateConfig{}
c, err := component.TranslateFromSchema(props)
if err != nil {
return template, err
}
if name, ok := c["name"].(string); ok && uk != nil && uk.UK_BASE != "" {
template.path, _ = unikraft.PlaceComponent(
uk.UK_BASE,
unikraft.ComponentTypeApp,
name,
)
}
if source, ok := c["source"]; ok {
template.source = source.(string)
// If the provided source is a directory on the host, set the "path" to this
// value. The "path" is the location on disk where the microlibrary will
// eventually saved by the relevant package manager. For completeness, use
// absolute paths for both the path and the source.
if f, err := os.Stat(template.source); err == nil && f.IsDir() {
if uk != nil && uk.UK_BASE != "" {
template.path = utils.RelativePath(uk.UK_BASE, template.source)
template.source = template.path
} else {
template.path = template.source
}
}
}
if name, ok := c["name"]; ok {
template.name = name.(string)
}
if version, ok := c["version"]; ok {
template.version = version.(string)
}
if kconf, ok := c["kconfig"]; ok {
template.kconfig = kconf.(kconfig.KeyValueMap)
}
return template, nil
}