-
Notifications
You must be signed in to change notification settings - Fork 4
/
type.go
134 lines (117 loc) · 3.56 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
123
124
125
126
127
128
129
130
131
132
133
134
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
)
// AllTypes contains all possible variants of Type.
var AllTypes = []Type{
Generated,
FromFile,
FromEnvironment,
}
func (instance Type) String() string {
result, err := instance.CheckedString()
if err != nil {
panic(err)
}
return result
}
// CheckedString is like String but return also an optional error if there are some
// validation errors.
func (instance Type) CheckedString() (string, error) {
switch instance {
case Generated:
return "generated", nil
case FromFile:
return "fromFile", nil
case FromEnvironment:
return "fromEnvironment", nil
}
return "", errors.New("Illegal keyStore type: %d", instance)
}
// Set the given string to current object from a string.
// Return an error object if there are some problems while transforming the string.
func (instance *Type) Set(value string) error {
if valueAsInt, err := strconv.Atoi(value); err == nil {
for _, candidate := range AllTypes {
if int(candidate) == valueAsInt {
(*instance) = candidate
return nil
}
}
return errors.New("Illegal keyStore type: " + value)
}
lowerValue := strings.ToLower(value)
for _, candidate := range AllTypes {
if strings.ToLower(candidate.String()) == lowerValue {
(*instance) = candidate
return nil
}
}
return errors.New("Illegal keyStore type: " + value)
}
// MarshalYAML is used until yaml marshalling. Do not call directly.
func (instance Type) MarshalYAML() (interface{}, error) {
return instance.String(), nil
}
// UnmarshalYAML is used until yaml unmarshalling. Do not call directly.
func (instance *Type) UnmarshalYAML(unmarshal func(interface{}) error) error {
var value string
if err := unmarshal(&value); err != nil {
return err
}
return instance.Set(value)
}
// MarshalJSON is used until json marshalling. Do not call directly.
func (instance Type) MarshalJSON() ([]byte, error) {
s, err := instance.CheckedString()
if err != nil {
return []byte{}, err
}
return json.Marshal(s)
}
// UnmarshalJSON is used until json unmarshalling. Do not call directly.
func (instance *Type) UnmarshalJSON(b []byte) error {
var value string
if err := json.Unmarshal(b, &value); err != nil {
return err
}
return instance.Set(value)
}
// IsTakingFilename returns true if the KeyStore instance created with this type is created from file.
func (instance Type) IsTakingFilename() bool {
return instance == FromFile
}
// IsGenerating returns true if the KeyStore instance created with this type will be generated.
func (instance Type) IsGenerating() bool {
return instance == Generated
}
// IsConsumingCAFile returns true if the KeyStore instance created with this type can consumes a CA bundle file.
func (instance Type) IsConsumingCAFile() bool {
return instance == FromFile || instance == FromEnvironment
}
// Validate do validate action on this object and return an error object if any.
func (instance Type) Validate() error {
_, err := instance.CheckedString()
return err
}