-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
250 lines (214 loc) · 5.54 KB
/
example_test.go
File metadata and controls
250 lines (214 loc) · 5.54 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package env_test
import (
"fmt"
"log"
"os"
"time"
"github.com/lindenlab/env"
)
func ExampleGet() {
os.Setenv("HOME", "/home/user")
fmt.Println(env.Get("HOME"))
fmt.Println(env.Get("NONEXISTENT"))
// Output:
// /home/user
//
}
func ExampleGetOr() {
os.Setenv("PORT", "8080")
fmt.Println(env.GetOr("PORT", "3000"))
fmt.Println(env.GetOr("MISSING_PORT", "3000"))
// Output:
// 8080
// 3000
}
func ExampleGetInt() {
os.Setenv("PORT", "8080")
os.Setenv("INVALID_PORT", "not-a-number")
port, err := env.GetInt("PORT")
if err != nil {
log.Fatal(err)
}
fmt.Println("Port:", port)
_, err = env.GetInt("INVALID_PORT")
fmt.Println("Error:", err != nil)
// Output:
// Port: 8080
// Error: true
}
func ExampleGetOrInt() {
os.Setenv("PORT", "8080")
fmt.Println("Existing:", env.GetOrInt("PORT", 3000))
fmt.Println("Missing:", env.GetOrInt("MISSING_PORT", 3000))
fmt.Println("Invalid:", env.GetOrInt("INVALID_PORT", 3000))
// Output:
// Existing: 8080
// Missing: 3000
// Invalid: 3000
}
func ExampleGetBool() {
os.Setenv("DEBUG", "true")
os.Setenv("VERBOSE", "1")
os.Setenv("QUIET", "false")
debug, _ := env.GetBool("DEBUG")
verbose, _ := env.GetBool("VERBOSE")
quiet, _ := env.GetBool("QUIET")
fmt.Println("Debug:", debug)
fmt.Println("Verbose:", verbose)
fmt.Println("Quiet:", quiet)
// Output:
// Debug: true
// Verbose: true
// Quiet: false
}
func ExampleGetDuration() {
os.Setenv("TIMEOUT", "30s")
os.Setenv("INTERVAL", "5m30s")
timeout, _ := env.GetDuration("TIMEOUT")
interval, _ := env.GetDuration("INTERVAL")
fmt.Println("Timeout:", timeout)
fmt.Println("Interval:", interval)
// Output:
// Timeout: 30s
// Interval: 5m30s
}
func ExampleLoad() {
// Create a temporary .env file
envContent := `# Database configuration
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_NAME=myapp
# Application settings
DEBUG=true
LOG_LEVEL=info`
err := os.WriteFile(".env", []byte(envContent), 0600)
if err != nil {
log.Fatal(err)
}
defer os.Remove(".env")
// Load the .env file
err = env.Load()
if err != nil {
log.Fatal(err)
}
// Access the loaded variables
fmt.Println("Database Host:", env.Get("DATABASE_HOST"))
fmt.Println("Database Port:", env.GetOrInt("DATABASE_PORT", 0))
fmt.Println("Debug Mode:", env.GetOrBool("DEBUG", false))
// Output:
// Database Host: localhost
// Database Port: 5432
// Debug Mode: true
}
func ExampleParse() {
// Set up environment variables
os.Setenv("HOME", "/tmp/fakehome")
os.Setenv("PORT", "8080")
os.Setenv("DEBUG", "true")
os.Setenv("TAGS", "web,api,database")
os.Setenv("TIMEOUT", "30s")
type Config struct {
Home string `env:"HOME"`
Port int `env:"PORT" envDefault:"3000"`
Debug bool `env:"DEBUG"`
Tags []string `env:"TAGS" envSeparator:","`
Timeout time.Duration `env:"TIMEOUT" envDefault:"10s"`
Version string `env:"VERSION" envDefault:"1.0.0"`
Required string `env:"REQUIRED_VAR" required:"true"`
}
// This will fail because REQUIRED_VAR is not set
var cfg Config
err := env.Parse(&cfg)
if err != nil {
fmt.Println("Error:", err != nil)
}
// Set the required variable and try again
os.Setenv("REQUIRED_VAR", "important-value")
err = env.Parse(&cfg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Home: %s\n", cfg.Home)
fmt.Printf("Port: %d\n", cfg.Port)
fmt.Printf("Debug: %t\n", cfg.Debug)
fmt.Printf("Tags: %v\n", cfg.Tags)
fmt.Printf("Timeout: %v\n", cfg.Timeout)
fmt.Printf("Version: %s\n", cfg.Version)
fmt.Printf("Required: %s\n", cfg.Required)
// Output:
// Error: true
// Home: /tmp/fakehome
// Port: 8080
// Debug: true
// Tags: [web api database]
// Timeout: 30s
// Version: 1.0.0
// Required: important-value
}
func ExampleParseWithPrefix() {
// Set up environment variables with prefixes
os.Setenv("CLIENT1_HOST", "api.example.com")
os.Setenv("CLIENT1_PORT", "443")
os.Setenv("CLIENT2_HOST", "internal.example.com")
os.Setenv("CLIENT2_PORT", "8080")
type ClientConfig struct {
Host string `env:"HOST" envDefault:"localhost"`
Port int `env:"PORT" envDefault:"80"`
}
var client1, client2 ClientConfig
err := env.ParseWithPrefix(&client1, "CLIENT1_")
if err != nil {
log.Fatal(err)
}
err = env.ParseWithPrefix(&client2, "CLIENT2_")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Client 1: %s:%d\n", client1.Host, client1.Port)
fmt.Printf("Client 2: %s:%d\n", client2.Host, client2.Port)
// Output:
// Client 1: api.example.com:443
// Client 2: internal.example.com:8080
}
func ExampleRead() {
// Create a temporary .env file
envContent := `DATABASE_URL=postgres://localhost/myapp
REDIS_URL=redis://localhost:6379
API_KEY=secret123
DEBUG=true`
err := os.WriteFile("config.env", []byte(envContent), 0600)
if err != nil {
log.Fatal(err)
}
defer os.Remove("config.env")
// Read the file without setting environment variables
envMap, err := env.Read("config.env")
if err != nil {
log.Fatal(err)
}
fmt.Println("Database URL:", envMap["DATABASE_URL"])
fmt.Println("Redis URL:", envMap["REDIS_URL"])
fmt.Println("Debug:", envMap["DEBUG"])
// Output:
// Database URL: postgres://localhost/myapp
// Redis URL: redis://localhost:6379
// Debug: true
}
func ExampleMarshal() {
envMap := map[string]string{
"DATABASE_URL": "postgres://localhost/myapp",
"DEBUG": "true",
"PORT": "8080",
"API_KEY": "secret with spaces",
}
content, err := env.Marshal(envMap)
if err != nil {
log.Fatal(err)
}
fmt.Println(content)
// Output:
// API_KEY="secret with spaces"
// DATABASE_URL="postgres://localhost/myapp"
// DEBUG="true"
// PORT="8080"
}