Skip to content

Commit 0bd799c

Browse files
authored
Merge pull request #10 from ieee0824/dump-dotenv
Dump dotenv
2 parents c0967c2 + eed7564 commit 0bd799c

File tree

12 files changed

+108
-32
lines changed

12 files changed

+108
-32
lines changed

.circleci/config.yml

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@ version: 2
22
jobs:
33
build:
44
docker:
5-
- image: golang:1.8.3
5+
- image: golang:1.16.4
66
working_directory: /go/src/github.com/ieee0824/getenv
77
steps:
88
- checkout
9-
- run:
10-
name: install glide
11-
command: |
12-
go get github.com/Masterminds/glide
139
- run:
1410
name: install packages
1511
command: |
16-
glide i
12+
go get -u -v ./...
1713
- run:
1814
name: run test
1915
command: |
20-
go test $(glide nv)
16+
go test ./...

.go-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.16.4

bool.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package getenv
22

33
import (
4-
"strings"
54
"os"
5+
"strings"
66
)
77

88
func convertStringToBoolean(s string) bool {
@@ -16,6 +16,7 @@ func convertStringToBoolean(s string) bool {
1616
}
1717

1818
func Bool(key string, def ...bool) bool {
19+
Logger.Dump(key, def)
1920
var d bool
2021
if len(def) != 0 {
2122
d = def[0]

bool_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestConvertStringToBoolean(t *testing.T) {
2626
for _, test := range tests {
2727
got := convertStringToBoolean(test.input)
2828
if got != test.want {
29-
t.Fatalf("want %q, but %q:", test.want, got)
29+
t.Fatalf("want %v, but %v:", test.want, got)
3030
}
3131
}
3232
}

duration.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package getenv
22

33
import (
4-
"time"
54
"os"
5+
"time"
66
)
77

88
func Duration(key string, def ...interface{}) time.Duration {
9+
Logger.Dump(key, def)
910
var d time.Duration
1011
if len(def) != 0 {
1112
if i, ok := def[0].(int); ok {
1213
d = time.Duration(int64(i)) * time.Second
1314
} else if dr, ok := def[0].(time.Duration); ok {
1415
d = dr
1516
} else if s, ok := def[0].(string); ok {
16-
d, _ = time.ParseDuration(s)
17+
d, _ = time.ParseDuration(s)
1718
}
1819
}
1920

@@ -25,5 +26,3 @@ func Duration(key string, def ...interface{}) time.Duration {
2526

2627
return d
2728
}
28-
29-

example/example.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,42 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/ieee0824/getenv"
6-
"time"
75
"os"
6+
"time"
7+
8+
"github.com/ieee0824/getenv"
89
)
910

1011
func main() {
1112
fmt.Println(getenv.Duration("ANY_ENV"))
1213
fmt.Println(getenv.Duration("ANY_ENV", 60))
13-
fmt.Println(getenv.Duration("ANY_ENV","120s"))
14-
fmt.Println(getenv.Duration("ANY_ENV", 60 * time.Second))
14+
fmt.Println(getenv.Duration("ANY_ENV", "120s"))
15+
fmt.Println(getenv.Duration("ANY_ENV", 60*time.Second))
1516
fmt.Println(getenv.Duration("ANY_ENV", "1h30m20s"))
1617

1718
os.Setenv("ANY_ENV", "60h")
1819
fmt.Println(getenv.Duration("ANY_ENV"))
1920
fmt.Println(getenv.Duration("ANY_ENV", 60))
20-
fmt.Println(getenv.Duration("ANY_ENV","120s"))
21-
fmt.Println(getenv.Duration("ANY_ENV", 60 * time.Second))
21+
fmt.Println(getenv.Duration("ANY_ENV", "120s"))
22+
fmt.Println(getenv.Duration("ANY_ENV", 60*time.Second))
2223
fmt.Println(getenv.Duration("ANY_ENV", "1h30m20s"))
2324

2425
os.Setenv("ANY_ENV", "60h")
2526
fmt.Println(getenv.Duration("ANY_ENV"))
2627
fmt.Println(getenv.Duration("ANY_ENV", 60))
27-
fmt.Println(getenv.Duration("ANY_ENV","120s"))
28-
fmt.Println(getenv.Duration("ANY_ENV", 60 * time.Second))
28+
fmt.Println(getenv.Duration("ANY_ENV", "120s"))
29+
fmt.Println(getenv.Duration("ANY_ENV", 60*time.Second))
2930
fmt.Println(getenv.Duration("ANY_ENV", "1h30m20s"))
31+
32+
os.Setenv("GETENV_DUMP_MODE", "dotenv")
33+
os.Setenv("ANY_ENV", "true")
34+
getenv.Bool("ANY_ENV", false)
35+
getenv.Duration("ANY_ENV")
36+
getenv.Duration("ANY_ENV", 60)
37+
getenv.Duration("ANY_ENV", "120s")
38+
getenv.Int("INT_ENV")
39+
getenv.Int("INT_ENV", -10)
40+
getenv.String("STR_ENV")
41+
getenv.String("STR_ENV", "")
42+
getenv.String("STR_ENV", "hoge")
3043
}

glide.lock

-4
This file was deleted.

glide.yaml

-2
This file was deleted.

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/ieee0824/getenv
2+
3+
go 1.16

int.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package getenv
22

33
import (
4+
"log"
45
"os"
56
"strconv"
6-
"log"
77
)
88

99
func Int(key string, def ...int) int {
10+
Logger.Dump(key, def)
1011
var d int
1112
if len(def) != 0 {
1213
d = def[0]
@@ -22,7 +23,8 @@ func Int(key string, def ...int) int {
2223
return i
2324
}
2425

25-
func Int32(key string, def ...int32) (int32) {
26+
func Int32(key string, def ...int32) int32 {
27+
Logger.Dump(key, def)
2628
var d int32
2729
if len(def) != 0 {
2830
d = def[0]
@@ -39,7 +41,8 @@ func Int32(key string, def ...int32) (int32) {
3941
return int32(i32)
4042
}
4143

42-
func Int64(key string, def ...int64) (int64) {
44+
func Int64(key string, def ...int64) int64 {
45+
Logger.Dump(key, def)
4346
var d int64
4447
if len(def) != 0 {
4548
d = def[0]
@@ -56,7 +59,8 @@ func Int64(key string, def ...int64) (int64) {
5659
return int64(i64)
5760
}
5861

59-
func Int16(key string, def ...int16) (int16) {
62+
func Int16(key string, def ...int16) int16 {
63+
Logger.Dump(key, def)
6064
var d int16
6165
if len(def) != 0 {
6266
d = def[0]
@@ -71,4 +75,4 @@ func Int16(key string, def ...int16) (int16) {
7175
return d
7276
}
7377
return int16(i16)
74-
}
78+
}

logger.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package getenv
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"reflect"
8+
)
9+
10+
var Logger = &EnvLogger{
11+
w: os.Stdout,
12+
}
13+
14+
type EnvLogger struct {
15+
w io.Writer
16+
}
17+
18+
func (l *EnvLogger) SetWriter(w io.Writer) {
19+
l.w = w
20+
}
21+
22+
func (l *EnvLogger) Dump(key string, def ...interface{}) {
23+
switch os.Getenv("GETENV_DUMP_MODE") {
24+
case "dotenv":
25+
if err := l.dumpDotEnv(key, def...); err != nil {
26+
panic(err)
27+
}
28+
}
29+
}
30+
31+
func (l *EnvLogger) dumpDotEnv(key string, def ...interface{}) error {
32+
if _, err := l.w.Write([]byte(key)); err != nil {
33+
return err
34+
}
35+
if _, err := l.w.Write([]byte("=")); err != nil {
36+
return err
37+
}
38+
if len(def) == 0 {
39+
if _, err := l.w.Write([]byte("\n")); err != nil {
40+
return err
41+
}
42+
return nil
43+
}
44+
45+
var v interface{}
46+
switch reflect.TypeOf(def[0]).Kind() {
47+
case reflect.Slice:
48+
s := reflect.ValueOf(def[0])
49+
if s.Len() == 0 {
50+
if _, err := l.w.Write([]byte("\n")); err != nil {
51+
return err
52+
}
53+
return nil
54+
}
55+
v = s.Index(0).Interface()
56+
default:
57+
v = def[0]
58+
}
59+
if _, err := l.w.Write([]byte(fmt.Sprintf("%v\n", v))); err != nil {
60+
return err
61+
}
62+
63+
return nil
64+
}

string.go

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package getenv
33
import "os"
44

55
func String(key string, def ...string) string {
6+
Logger.Dump(key, def)
67
var d string
78
if len(def) != 0 {
89
d = def[0]

0 commit comments

Comments
 (0)