Skip to content

Commit b5e1869

Browse files
committed
update
1 parent 5003bea commit b5e1869

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

platform.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com
22

33
import (
4+
"log"
45
"os"
56
"runtime"
7+
"time"
68
)
79

810
const (
@@ -28,3 +30,90 @@ func ExitOnFailure(msg string, errCodes ...int) {
2830
os.Stderr.WriteString(msg)
2931
os.Exit(errCode)
3032
}
33+
34+
func Getenv(key string, defaults ...string) string {
35+
v := os.Getenv(key)
36+
if len(v) == 0 && len(defaults) > 0 {
37+
return defaults[0]
38+
}
39+
return v
40+
}
41+
42+
func GetenvInt(key string, defaults ...int) int {
43+
v := os.Getenv(key)
44+
if len(v) == 0 && len(defaults) > 0 {
45+
return defaults[0]
46+
}
47+
return Int(v)
48+
}
49+
50+
func GetenvUint(key string, defaults ...uint) uint {
51+
v := os.Getenv(key)
52+
if len(v) == 0 && len(defaults) > 0 {
53+
return defaults[0]
54+
}
55+
return Uint(v)
56+
}
57+
58+
func GetenvInt64(key string, defaults ...int64) int64 {
59+
v := os.Getenv(key)
60+
if len(v) == 0 && len(defaults) > 0 {
61+
return defaults[0]
62+
}
63+
return Int64(v)
64+
}
65+
66+
func GetenvUint64(key string, defaults ...uint64) uint64 {
67+
v := os.Getenv(key)
68+
if len(v) == 0 && len(defaults) > 0 {
69+
return defaults[0]
70+
}
71+
return Uint64(v)
72+
}
73+
74+
func GetenvInt32(key string, defaults ...int32) int32 {
75+
v := os.Getenv(key)
76+
if len(v) == 0 && len(defaults) > 0 {
77+
return defaults[0]
78+
}
79+
return Int32(v)
80+
}
81+
82+
func GetenvUint32(key string, defaults ...uint32) uint32 {
83+
v := os.Getenv(key)
84+
if len(v) == 0 && len(defaults) > 0 {
85+
return defaults[0]
86+
}
87+
return Uint32(v)
88+
}
89+
90+
func GetenvFloat32(key string, defaults ...float32) float32 {
91+
v := os.Getenv(key)
92+
if len(v) == 0 && len(defaults) > 0 {
93+
return defaults[0]
94+
}
95+
return Float32(v)
96+
}
97+
98+
func GetenvFloat64(key string, defaults ...float64) float64 {
99+
v := os.Getenv(key)
100+
if len(v) == 0 && len(defaults) > 0 {
101+
return defaults[0]
102+
}
103+
return Float64(v)
104+
}
105+
106+
func GetenvDuration(key string, defaults ...time.Duration) time.Duration {
107+
v := os.Getenv(key)
108+
if len(v) > 0 {
109+
t, err := time.ParseDuration(v)
110+
if err == nil {
111+
return t
112+
}
113+
log.Printf(`GetenvDuration: %v: %v`, v, err)
114+
}
115+
if len(defaults) > 0 {
116+
return defaults[0]
117+
}
118+
return 0
119+
}

0 commit comments

Comments
 (0)