-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
125 lines (111 loc) · 3.17 KB
/
format.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
package template
import (
"fmt"
"reflect"
"gopkg.in/yaml.v3"
box "github.com/hckops/hckctl/pkg/box/model"
lab "github.com/hckops/hckctl/pkg/lab/model"
"github.com/hckops/hckctl/pkg/schema"
task "github.com/hckops/hckctl/pkg/task/model"
"github.com/hckops/hckctl/pkg/util"
)
// formats, strips out comments, quotes, etc. and prints optional empty fields
func convertFromYamlToYaml(kind schema.SchemaKind, value string) (string, error) {
switch kind {
case schema.KindBoxV1:
if model, err := decodeFromYaml[box.BoxV1](value); err != nil {
return "", err
} else {
return util.EncodeYaml(model)
}
case schema.KindLabV1:
if model, err := decodeFromYaml[lab.LabV1](value); err != nil {
return "", err
} else {
return util.EncodeYaml(model)
}
case schema.KindTaskV1:
if model, err := decodeFromYaml[task.TaskV1](value); err != nil {
return "", err
} else {
return util.EncodeYaml(model)
}
case schema.KindDumpV1:
if model, err := decodeFromYaml[lab.DumpV1](value); err != nil {
return "", err
} else {
return util.EncodeYaml(model)
}
default:
return "", fmt.Errorf("invalid kind: %v", kind)
}
}
func convertFromYamlToJson(kind schema.SchemaKind, value string) (string, error) {
switch kind {
case schema.KindBoxV1:
if model, err := decodeFromYaml[box.BoxV1](value); err != nil {
return "", err
} else {
return util.EncodeJsonIndent(model)
}
case schema.KindLabV1:
if model, err := decodeFromYaml[lab.LabV1](value); err != nil {
return "", err
} else {
return util.EncodeJsonIndent(model)
}
case schema.KindTaskV1:
if model, err := decodeFromYaml[task.TaskV1](value); err != nil {
return "", err
} else {
return util.EncodeJsonIndent(model)
}
case schema.KindDumpV1:
if model, err := decodeFromYaml[lab.DumpV1](value); err != nil {
return "", err
} else {
return util.EncodeJsonIndent(model)
}
default:
return "", fmt.Errorf("invalid kind: %v", kind)
}
}
func decodeFromYaml[T TemplateType](value string) (T, error) {
// https://stackoverflow.com/questions/71047848/how-to-assign-or-return-generic-t-that-is-constrained-by-union
var templateType T
switch typeRef := any(&templateType).(type) {
case *string:
*typeRef = value
case *box.BoxV1:
var model box.BoxV1
if err := yaml.Unmarshal([]byte(value), &model); err != nil {
return none[T](), fmt.Errorf("box decoder error: %v", err)
}
*typeRef = model
case *lab.LabV1:
var model lab.LabV1
if err := yaml.Unmarshal([]byte(value), &model); err != nil {
return none[T](), fmt.Errorf("lab decoder error: %v", err)
}
*typeRef = model
case *task.TaskV1:
var model task.TaskV1
if err := yaml.Unmarshal([]byte(value), &model); err != nil {
return none[T](), fmt.Errorf("task decoder error: %v", err)
}
*typeRef = model
case *lab.DumpV1:
var model lab.DumpV1
if err := yaml.Unmarshal([]byte(value), &model); err != nil {
return none[T](), fmt.Errorf("dump decoder error: %v", err)
}
*typeRef = model
default:
return templateType, fmt.Errorf("unable to decode yaml invalid schema %v", reflect.TypeOf(templateType))
}
return templateType, nil
}
// nil for generics
func none[T TemplateType]() T {
return *new(T)
}