/
env.go
111 lines (82 loc) · 1.72 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
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
package config
import (
"os"
"strings"
"github.com/goraz/cast"
)
// Env middleware struct that handles environment variables.
type Env struct {
}
// Setup returns a error if the middleware setup is failing.
func (s *Env) Setup() error {
return nil
}
// NewEnv creates a new environment middleware.
func NewEnv() *Env {
return &Env{}
}
func (s *Env) value(name string) string {
name = strings.ToUpper(name)
name = strings.Replace(name, ".", "_", -1)
return os.Getenv(name)
}
// Bool returns a bool or a error.
func (s *Env) Bool(key string) (bool, error) {
v := s.value(key)
if v == "" {
return false, nil
}
return cast.Bool(v)
}
// Float returns a float64 or a error.
func (s *Env) Float(key string) (float64, error) {
v := s.value(key)
if v == "" {
return 0.0, nil
}
return cast.Float(v)
}
// Int returns a int or a error.
func (s *Env) Int(key string) (int64, error) {
v := s.value(key)
if v == "" {
return 0, nil
}
return cast.Int(v)
}
// Get returns a interface or a error.
func (s *Env) Get(key string) (interface{}, error) {
v := s.value(key)
if v == "" {
return nil, nil
}
return v, nil
}
// List returns a slice of strings or a error.
func (s *Env) List(key string) ([]string, error) {
v := s.value(key)
if v == "" {
return []string{}, nil
}
return castList(v)
}
// String returns a string or a error.
func (s *Env) String(key string) (string, error) {
v := s.value(key)
if v == "" {
return "", nil
}
return cast.String(v)
}
// Uint returns a unsigned int or a error.
func (s *Env) Uint(key string) (uint64, error) {
v := s.value(key)
if v == "" {
return 0, nil
}
return cast.Uint(v)
}
// ID returns the values struct identifier.
func (s *Env) ID() string {
return "env"
}