-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package env | ||
|
||
type Env string | ||
|
||
var ( | ||
Dev Env = "dev" | ||
Prod Env = "prod" | ||
Debug Env = "debug" | ||
Stage Env = "stage" | ||
) | ||
|
||
var currentEnv Env = Prod | ||
|
||
func SetEnv(env Env) { | ||
currentEnv = env | ||
} | ||
|
||
func Is(envs ...Env) bool { | ||
for _, env := range envs { | ||
if currentEnv == env { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func IsDev() bool { | ||
return Is(Dev) | ||
} | ||
|
||
func IsProd() bool { | ||
return Is(Prod) | ||
} | ||
|
||
func IsDebug() bool { | ||
return Is(Debug) | ||
} | ||
|
||
func IsStage() bool { | ||
return Is(Stage) | ||
} | ||
|
||
func IsUseString(envs ...string) bool { | ||
for _, env := range envs { | ||
if currentEnv == Env(env) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package env | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEnv(t *testing.T) { | ||
assert.Equal(t, Env("dev"), Dev) | ||
assert.Equal(t, Env("prod"), Prod) | ||
assert.Equal(t, Env("debug"), Debug) | ||
assert.Equal(t, Env("stage"), Stage) | ||
} | ||
|
||
func TestIs(t *testing.T) { | ||
SetEnv(Dev) | ||
assert.True(t, Is(Dev)) | ||
assert.False(t, Is(Prod)) | ||
assert.False(t, Is(Debug)) | ||
assert.False(t, Is(Stage)) | ||
assert.True(t, Is(Dev, Prod)) | ||
assert.True(t, IsDev()) | ||
assert.False(t, IsProd()) | ||
assert.False(t, IsDebug()) | ||
assert.False(t, IsStage()) | ||
assert.True(t, IsUseString("dev")) | ||
assert.False(t, IsUseString("prod")) | ||
assert.True(t, IsUseString("dev", "prod")) | ||
assert.True(t, Is("dev")) | ||
assert.False(t, Is("prod")) | ||
|
||
SetEnv(Prod) | ||
assert.True(t, Is(Prod)) | ||
assert.False(t, Is(Dev)) | ||
} | ||
|
||
func TestCustom(t *testing.T) { | ||
SetEnv("online") | ||
|
||
assert.True(t, Is("online")) | ||
assert.False(t, Is("dev")) | ||
} |