generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
73 lines (60 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
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package osenv
import (
"fmt"
"os"
"strconv"
)
// Get returns the environment variable associated with the given key.
// Returns the fallback value if not exists.
func Get(key string, fallback ...string) string {
if len(fallback) == 0 {
return os.Getenv(key)
}
if v, exists := os.LookupEnv(key); exists {
return v
}
return fallback[0]
}
// MustGet returns the environment variable associated with the given key,
// and panics if the key is not exists.
func MustGet(key string) string {
if v, exists := os.LookupEnv(key); exists {
return v
}
panic(fmt.Errorf("getting an unknown environment variable %q", key))
}
// SetNX sets the value of the environment variable named by the key if not exist.
func SetNX(key, value string) error {
if _, exists := os.LookupEnv(key); !exists {
return os.Setenv(key, value)
}
return nil
}
// GetInt converts the environment variable associated with the given key to int
// and returns it.
// Returns the fallback value if the key is not exists or the value is invalid.
func GetInt(key string, fallback ...int) (int, error) {
if s, exists := os.LookupEnv(key); exists {
return strconv.Atoi(s)
}
if len(fallback) > 0 {
return fallback[0], nil
}
return 0, noEnvError(key)
}
// MustGetInt converts the environment variable associated with the given key to int
// and returns it.
// Panics if the key is not exists or the value is invalid.
func MustGetInt(key string) int {
v, err := GetInt(key)
if err != nil {
panic(err)
}
return v
}
func noEnvError(key string) error {
return fmt.Errorf("no such environment variable: %s", key)
}