-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
58 lines (49 loc) · 1.03 KB
/
env.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
package ctfdsetup
import (
"fmt"
"os"
"github.com/invopop/jsonschema"
"gopkg.in/yaml.v3"
)
type FromEnv struct {
Content string `yaml:"-" json:"-" jsonschema:"-"`
}
var _ yaml.Unmarshaler = (*FromEnv)(nil)
func (fe *FromEnv) UnmarshalYAML(node *yaml.Node) error {
if node.Value != "" {
fe.Content = node.Value
return nil
}
type lfe struct {
FromEnv *string `yaml:"from_env"`
}
var lfev lfe
if err := node.Decode(&lfev); err != nil {
return err
}
if lfev.FromEnv == nil {
return nil
}
fe.Content = os.Getenv(*lfev.FromEnv)
if len(fe.Content) == 0 {
return fmt.Errorf("empty value from environment variable %s", *lfev.FromEnv)
}
return nil
}
func (fe FromEnv) JSONSchema() *jsonschema.Schema {
subObj := jsonschema.NewProperties()
subObj.Set("from_env", &jsonschema.Schema{
Type: "string",
Description: "The environment variable to look at.",
})
return &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
{
Type: "object",
Properties: subObj,
}, {
Type: "string",
},
},
}
}