Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lalabuy948 committed Aug 16, 2020
0 parents commit 43dfbef
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [lalabuy948]
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: build

on:
push:
branches:
tags:
pull_request:

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: set up go 1.14
uses: actions/setup-go@v1
with:
go-version: 1.14
id: go

- name: checkout
uses: actions/checkout@v2

- name: build and test
run: |
export GO111MODULE=on
go get -v
go test -timeout=60s -v -covermode=count -coverprofile=$GITHUB_WORKSPACE/profile.cov_tmp
cat $GITHUB_WORKSPACE/profile.cov_tmp | grep -v "_mock.go" > $GITHUB_WORKSPACE/profile.cov
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vscode
.idea

*.DS_Store
21 changes: 21 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Lalabuy948

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# gonvutils [![Go Report Card](https://goreportcard.com/badge/github.com/lalabuy948/gonvutils)](https://goreportcard.com/report/github.com/lalabuy948/gonvutils) [![Coverage Status](https://coveralls.io/repos/github.com/lalabuy948/gonvutils/badge.svg?branch=master)]

> Package gonvutils provides useful environment operations
## Funcs

- `IsProduction`, `IsDevelopment` and `IsTesting` checks for `ENVIRONMENT` dot env value.
- `GetEnv` gets env value, fallback as second function argument.

## Install

go get -u github.com/lalabuy948/gonvutils
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/lalabuy948/gonvutils

go 1.14
45 changes: 45 additions & 0 deletions gonvutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Package gonvutils provides useful environment operations
package gonvutils

import "os"

//IsProduction checks if ENVIRONMENT value is equal to "PROD".
func IsProduction() bool {
if os.Getenv("ENVIRONMENT") == "PROD" {
return true
}
if os.Getenv("ENVIRONMENT") == "PRODUCTION" {
return true
}
return false
}

//IsDevelopment checks if ENVIRONMENT value is equal to "DEV".
func IsDevelopment() bool {
if os.Getenv("ENVIRONMENT") == "DEV" {
return true
}
if os.Getenv("ENVIRONMENT") == "DEVELOPMENT" {
return true
}
return false
}

//IsTesting checks if ENVIRONMENT value is equal to "TEST".
func IsTesting() bool {
if os.Getenv("ENVIRONMENT") == "TEST" {
return true
}
if os.Getenv("ENVIRONMENT") == "TESTING" {
return true
}
return false
}

//GetEnv extracts env value by provided key, otherwise falls back to second function argument.
func GetEnv(key string, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
87 changes: 87 additions & 0 deletions gonvutils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package gonvutils

import (
"os"
"testing"
)

func TestIsProduction(t *testing.T) {
os.Setenv("ENVIRONMENT", "PROD")
got := IsProduction()
if got != true {
t.Errorf("IsProduction() = %v; want true", got)
}

os.Setenv("ENVIRONMENT", "PRODUCTION")
got = IsProduction()
if got != true {
t.Errorf("IsProduction() = %v; want true", got)
}

os.Setenv("ENVIRONMENT", "BLA")
got = IsProduction()
if got != false {
t.Errorf("IsProduction() = %v; want false", got)
}

os.Unsetenv("ENVIRONMENT")
}

func TestIsDevelopment(t *testing.T) {
os.Setenv("ENVIRONMENT", "DEV")
got := IsDevelopment()
if got != true {
t.Errorf("IsDevelopment() = %v; want true", got)
}

os.Setenv("ENVIRONMENT", "DEVELOPMENT")
got = IsDevelopment()
if got != true {
t.Errorf("IsDevelopment() = %v; want true", got)
}

os.Setenv("ENVIRONMENT", "BLA")
got = IsDevelopment()
if got != false {
t.Errorf("IsDevelopment() = %v; want false", got)
}

os.Unsetenv("ENVIRONMENT")
}

func TestIsTesting(t *testing.T) {
os.Setenv("ENVIRONMENT", "TEST")
got := IsTesting()
if got != true {
t.Errorf("IsTesting() = %v; want true", got)
}

os.Setenv("ENVIRONMENT", "TESTING")
got = IsTesting()
if got != true {
t.Errorf("IsTesting() = %v; want true", got)
}

os.Setenv("ENVIRONMENT", "BLA")
got = IsTesting()
if got != false {
t.Errorf("IsTesting() = %v; want false", got)
}

os.Unsetenv("ENVIRONMENT")
}

func TestGetEnv(t *testing.T) {
os.Setenv("SERVER_PORT", "8080")
got := GetEnv("SERVER_PORT", "8080")
if got != "8080" {
t.Errorf("GetEnv(\"SERVER_PORT\", \"8080\") = %v; want 8080", got)
}

got = GetEnv("REDIS_PORT", "6379")
if got != "6379" {
t.Errorf("GetEnv(\"REDIS_PORT\", \"6379\") = %v; want 6379", got)
}

os.Unsetenv("SERVER_PORT")
}

0 comments on commit 43dfbef

Please sign in to comment.