Skip to content
This repository has been archived by the owner on Jul 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #9 from felipeneuwald/20210426
Browse files Browse the repository at this point in the history
Improve error handling and logging
  • Loading branch information
felipeneuwald committed Apr 26, 2021
2 parents 5d44727 + 41186d4 commit 4df8846
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 111 deletions.
Empty file removed .gitignore
Empty file.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# envh
Package envh provides convenient helpers to easily retrieve environment variables.

The purpose of the library is to be simple and functional. It has functions to retrieve environment variables and assign their values to `string`, `int`, and `bool` types.
The purpose of the library is to be simple and functional. It has functions to retrieve environment variables and assign their values to `string`, `int`, and `bool` types. It uses [https://github.com/pkg/errors](https://github.com/pkg/errors) for error handling and [https://github.com/rs/zerolog](https://github.com/rs/zerolog) for logging.

## Installation
`go get github.com/felipeneuwald/envh`
Expand Down
38 changes: 23 additions & 15 deletions envh.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"os"
"strconv"

"github.com/felipeneuwald/printh"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)

// String receives a s string and retrieves the value of the environment variable named by s.
Expand All @@ -15,7 +16,9 @@ import (
func String(s string) (string, error) {
v, ok := os.LookupEnv(s)
if !ok {
return "", fmt.Errorf("Environment variable %v is missing", s)
err := fmt.Errorf("environment variable %v is missing", s)
err = errors.New(err.Error())
return "", err
}
return v, nil
}
Expand All @@ -28,12 +31,11 @@ func MustString(s string) string {
}

// MustStringFatal receives a s string, passes it to func String, and returns the value returned by String.
// If String returns an error, it prints an error message and exits by calling
// github.com/felipeneuwald/printh ErrFatal.
// If String returns an error, it prints an error message and exits.
func MustStringFatal(s string) string {
v, err := String(s)
if err != nil {
printh.ErrFatal(err)
log.Fatal().Stack().Err(err).Send()
}
return v
}
Expand All @@ -45,15 +47,19 @@ func MustStringFatal(s string) string {
func Bool(s string) (bool, error) {
v, ok := os.LookupEnv(s)
if !ok {
return false, fmt.Errorf("Environment variable %v is missing", s)
err := fmt.Errorf("environment variable %v is missing", s)
err = errors.New(err.Error())
return false, err
}
if v == "true" {
return true, nil
}
if v == "false" {
return false, nil
}
return false, fmt.Errorf("Environment variable %v is %q; want \"true\" or \"false\"", s, v)
err := fmt.Errorf("environment variable %v is %q; want \"true\" or \"false\"", s, v)
err = errors.New(err.Error())
return false, err
}

// MustBool receives a s string, passes it to func Bool, and returns the value returned by Bool.
Expand All @@ -64,12 +70,11 @@ func MustBool(s string) bool {
}

// MustBoolFatal receives a s string, passes it to func Bool, and returns the value returned by Bool.
// If Bool returns an error, it prints an error message and exits by calling
// github.com/felipeneuwald/printh ErrFatal.
// If Bool returns an error, it prints an error message and exits.
func MustBoolFatal(s string) bool {
v, err := Bool(s)
if err != nil {
printh.ErrFatal(err)
log.Fatal().Stack().Err(err).Send()
}
return v
}
Expand All @@ -81,11 +86,15 @@ func MustBoolFatal(s string) bool {
func Int(s string) (int, error) {
v, ok := os.LookupEnv(s)
if !ok {
return 0, fmt.Errorf("Environment variable %v is missing", s)
err := fmt.Errorf("environment variable %v is missing", s)
err = errors.New(err.Error())
return 0, err
}
i, err := strconv.Atoi(v)
if err != nil {
return 0, fmt.Errorf("Environment variable %v; %v", s, err)
err = fmt.Errorf("environment variable %v: %v", s, err)
err = errors.New(err.Error())
return 0, err
}
return i, nil
}
Expand All @@ -98,12 +107,11 @@ func MustInt(s string) int {
}

// MustIntFatal receives a s string, passes it to func Int, and returns the value returned by Int.
// If Int returns an error, it prints an error message and exits by calling
// github.com/felipeneuwald/printh ErrFatal.
// If Int returns an error, it prints an error message and exits.
func MustIntFatal(s string) int {
v, err := Int(s)
if err != nil {
printh.ErrFatal(err)
log.Fatal().Stack().Err(err).Send()
}
return v
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/felipeneuwald/envh

go 1.15

require github.com/felipeneuwald/printh v1.0.0
require (
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.21.0
)
30 changes: 28 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
github.com/felipeneuwald/printh v1.0.0 h1:fPOJIHhzGyNQARcwqoxx4eM38+P0r+ycftc8bShMmBw=
github.com/felipeneuwald/printh v1.0.0/go.mod h1:MUOV2WT0+R3FVz/PkbxsIM03Q8j2vAxu7y2Hac42rVo=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.21.0 h1:Q3vdXlfLNT+OftyBHsU0Y445MD+8m8axjKgf2si0QcM=
github.com/rs/zerolog v1.21.0/go.mod h1:ZPhntP/xmq1nnND05hhpAh2QMhSsA4UN3MGZ6O2J3hM=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
1 change: 0 additions & 1 deletion vendor/github.com/felipeneuwald/printh/.gitignore

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/github.com/felipeneuwald/printh/LICENSE

This file was deleted.

6 changes: 0 additions & 6 deletions vendor/github.com/felipeneuwald/printh/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions vendor/github.com/felipeneuwald/printh/go.mod

This file was deleted.

Empty file.
58 changes: 0 additions & 58 deletions vendor/github.com/felipeneuwald/printh/printh.go

This file was deleted.

3 changes: 0 additions & 3 deletions vendor/modules.txt

This file was deleted.

0 comments on commit 4df8846

Please sign in to comment.