Skip to content

Commit

Permalink
Merge pull request #6 from ieee0824/refactor
Browse files Browse the repository at this point in the history
リファクタリング
  • Loading branch information
ieee0824 authored Aug 18, 2017
2 parents c67c7aa + 75ce785 commit 274d740
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 60 deletions.
30 changes: 30 additions & 0 deletions bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package getenv

import (
"strings"
"os"
)

func convertStringToBoolean(s string) bool {
s = strings.ToLower(s)
switch s {
case "true", "t", "1":
return true
default:
return false
}
}

func Bool(key string, def ...bool) bool {
var d bool
if len(def) != 0 {
d = def[0]
} else {
d = false
}
v := os.Getenv(key)
if v == "" {
return d
}
return convertStringToBoolean(v)
}
File renamed without changes.
61 changes: 1 addition & 60 deletions getenv.go
Original file line number Diff line number Diff line change
@@ -1,60 +1 @@
package getenv

import (
"os"
"strings"
"strconv"
)

func convertStringToBoolean(s string) bool {
s = strings.ToLower(s)
switch s {
case "true", "t", "1":
return true
default:
return false
}
}

func Bool(key string, def ...bool) bool {
var d bool
if len(def) != 0 {
d = def[0]
} else {
d = false
}
v := os.Getenv(key)
if v == "" {
return d
}
return convertStringToBoolean(v)
}

func String(key string, def ...string) string {
var d string
if len(def) != 0 {
d = def[0]
}
v := os.Getenv(key)
if v == "" {
return d
}
return v
}

func Int(key string, def ...int) int {
var d int
if len(def) != 0 {
d = def[0]
}
v := os.Getenv(key)
if v == "" {
return d
}
i, err := strconv.Atoi(v)
if err != nil {
return d
}
return i
}

package getenv
22 changes: 22 additions & 0 deletions int.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package getenv

import (
"os"
"strconv"
)

func Int(key string, def ...int) int {
var d int
if len(def) != 0 {
d = def[0]
}
v := os.Getenv(key)
if v == "" {
return d
}
i, err := strconv.Atoi(v)
if err != nil {
return d
}
return i
}
15 changes: 15 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package getenv

import "os"

func String(key string, def ...string) string {
var d string
if len(def) != 0 {
d = def[0]
}
v := os.Getenv(key)
if v == "" {
return d
}
return v
}

0 comments on commit 274d740

Please sign in to comment.