-
Notifications
You must be signed in to change notification settings - Fork 4
/
type.go
122 lines (105 loc) · 2.46 KB
/
type.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
package keyStore
import (
"encoding/json"
"github.com/echocat/caretakerd/errors"
"strconv"
"strings"
)
// # Description
//
// Represents the type of the keyStore.
type Type int
const (
// @id generated
// Indicates that caretakerd have to generate its own keyStore on startup.
// This is the best solution in most cases.
Generated Type = 0
// @id fromFile
// Load keyStore from a provided PEM file.
// If instance type is selected instance file have to be provided.
FromFile Type = 1
// @id fromEnvironment
// Load keyStore from the environment variable ``CTD_PEM`` in PEM format.
// If instance type is selected instance variable have to be provided.
FromEnvironment Type = 2
)
var AllTypes = []Type{
Generated,
FromFile,
FromEnvironment,
}
func (i Type) String() string {
result, err := i.CheckedString()
if err != nil {
panic(err)
}
return result
}
func (i Type) CheckedString() (string, error) {
switch i {
case Generated:
return "generated", nil
case FromFile:
return "fromFile", nil
case FromEnvironment:
return "fromEnvironment", nil
}
return "", errors.New("Illegal keyStore type: %d", i)
}
func (i *Type) Set(value string) error {
if valueAsInt, err := strconv.Atoi(value); err == nil {
for _, candidate := range AllTypes {
if int(candidate) == valueAsInt {
(*i) = candidate
return nil
}
}
return errors.New("Illegal keyStore type: " + value)
} else {
lowerValue := strings.ToLower(value)
for _, candidate := range AllTypes {
if strings.ToLower(candidate.String()) == lowerValue {
(*i) = candidate
return nil
}
}
return errors.New("Illegal keyStore type: " + value)
}
}
func (i Type) MarshalYAML() (interface{}, error) {
return i.String(), nil
}
func (i *Type) UnmarshalYAML(unmarshal func(interface{}) error) error {
var value string
if err := unmarshal(&value); err != nil {
return err
}
return i.Set(value)
}
func (i Type) MarshalJSON() ([]byte, error) {
s, err := i.CheckedString()
if err != nil {
return []byte{}, err
}
return json.Marshal(s)
}
func (i *Type) UnmarshalJSON(b []byte) error {
var value string
if err := json.Unmarshal(b, &value); err != nil {
return err
}
return i.Set(value)
}
func (i Type) IsTakingFilename() bool {
return i == FromFile
}
func (i Type) IsGenerating() bool {
return i == Generated
}
func (i Type) IsConsumingCaFile() bool {
return i == FromFile || i == FromEnvironment
}
func (i Type) Validate() error {
_, err := i.CheckedString()
return err
}