forked from gravitational/teleport
/
destination.go
97 lines (77 loc) · 2.57 KB
/
destination.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
/*
Copyright 2022 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"github.com/gravitational/trace"
"github.com/zmb3/teleport/lib/tbot/bot"
)
// DestinationMixin is a reusable struct for all config elements that accept a
// destination. Note that if embedded, DestinationMixin.CheckAndSetDefaults()
// must be called.
type DestinationMixin struct {
Directory *DestinationDirectory `yaml:"directory,omitempty"`
Memory *DestinationMemory `yaml:"memory,omitempty"`
}
type DestinationDefaults = func(*DestinationMixin) error
// checkAndSetDefaultsInner performs member initialization that won't recurse
func (dm *DestinationMixin) checkAndSetDefaultsInner() (int, error) {
notNilCount := 0
if dm.Directory != nil {
if err := dm.Directory.CheckAndSetDefaults(); err != nil {
return 0, trace.Wrap(err)
}
notNilCount++
}
if dm.Memory != nil {
if err := dm.Memory.CheckAndSetDefaults(); err != nil {
return 0, trace.Wrap(err)
}
notNilCount++
}
return notNilCount, nil
}
func (dm *DestinationMixin) CheckAndSetDefaults(applyDefaults DestinationDefaults) error {
notNilCount, err := dm.checkAndSetDefaultsInner()
if err != nil {
return trace.Wrap(err)
}
if notNilCount == 0 {
// use defaults
if err := applyDefaults(dm); err != nil {
return trace.Wrap(err)
}
// CheckAndSetDefaults() again
notNilCount, err := dm.checkAndSetDefaultsInner()
if err != nil {
return trace.Wrap(err)
}
if notNilCount == 0 {
return trace.BadParameter("a destination is required")
}
} else if notNilCount > 1 {
return trace.BadParameter("only one destination backend may be specified at a time")
}
return nil
}
// GetDestination returns the first non-nil Destination set. Note that
// CheckAndSetDefaults() does attempt to ensure that only a single
// destination is set, though this may change at runtime.
func (dm *DestinationMixin) GetDestination() (bot.Destination, error) {
if dm.Directory != nil {
return dm.Directory, nil
}
if dm.Memory != nil {
return dm.Memory, nil
}
return nil, trace.BadParameter("no valid destination exists")
}