-
Notifications
You must be signed in to change notification settings - Fork 53
/
env.go
54 lines (44 loc) · 983 Bytes
/
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
package ident
import (
"context"
"fmt"
"os"
"github.com/rancher/opni/pkg/util"
)
type envProvider struct {
EnvIdentOptions
}
type EnvIdentOptions struct {
variableName string
}
type EnvIdentOption func(*EnvIdentOptions)
func (o *EnvIdentOptions) apply(opts ...EnvIdentOption) {
for _, op := range opts {
op(o)
}
}
func WithVariableName(name string) EnvIdentOption {
return func(o *EnvIdentOptions) {
o.variableName = name
}
}
func NewEnvProvider(opts ...EnvIdentOption) Provider {
options := EnvIdentOptions{
variableName: "OPNI_UNIQUE_IDENTIFIER",
}
options.apply(opts...)
return &envProvider{
EnvIdentOptions: options,
}
}
func (p *envProvider) UniqueIdentifier(_ context.Context) (string, error) {
if v, ok := os.LookupEnv(p.variableName); ok {
return v, nil
}
return "", fmt.Errorf("environment variable %s not set", p.variableName)
}
func init() {
util.Must(RegisterProvider("env", func(...any) Provider {
return NewEnvProvider()
}))
}