Skip to content

Commit

Permalink
Add Load and MustLoad (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
markuswustenberg authored Jun 24, 2021
1 parent dc8d629 commit 496c70d
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Go

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![GoDoc](https://godoc.org/github.com/maragudk/env?status.svg)](https://godoc.org/github.com/maragudk/env)
[![Go](https://github.com/maragudk/env/actions/workflows/go.yml/badge.svg)](https://github.com/maragudk/env/actions/workflows/go.yml)
[![codecov](https://codecov.io/gh/maragudk/env/branch/master/graph/badge.svg)](https://codecov.io/gh/maragudk/env)
[![codecov](https://codecov.io/gh/maragudk/env/branch/main/graph/badge.svg)](https://codecov.io/gh/maragudk/env)

A small utility package to load different types of environment variables.

Expand Down
41 changes: 41 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
package env

import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -54,3 +57,41 @@ func GetDurationOrDefault(name string, defaultV time.Duration) time.Duration {
}
return vAsDuration
}

// Load environment variables from environment files. Defaults to loading from .env.
func Load(paths ...string) error {
if len(paths) == 0 {
paths = []string{".env"}
}
for _, path := range paths {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("error reading %v: %w", path, err)
}
s := bufio.NewScanner(file)
var i int
for s.Scan() {
i++
line := s.Text()
parts := strings.Split(line, "=")
if len(parts) < 2 {
return fmt.Errorf("missing equal sign on line %v in %v", i, path)
}
if err := os.Setenv(parts[0], parts[1]); err != nil {
return fmt.Errorf("error setting environment variable for line %v in %v: %w", i, path, err)
}
}
if err := s.Err(); err != nil {
return fmt.Errorf("error scanning %v: %w", path, err)
}
_ = file.Close()
}
return nil
}

// MustLoad calls Load and panics on errors.
func MustLoad(paths ...string) {
if err := Load(paths...); err != nil {
panic(err)
}
}
53 changes: 53 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,51 @@ func TestGetDurationOrDefault(t *testing.T) {
})
}

func TestLoad(t *testing.T) {
t.Run("loads an environment file", func(t *testing.T) {
is := is.New(t)
defer unsetenv("hat", "hats")
err := env.Load("testdata/env")
is.NoErr(err)
hat := env.GetStringOrDefault("hat", "regular")
is.Equal("party", hat)
hats := env.GetIntOrDefault("hats", 1)
is.Equal(2, hats)
})

t.Run("errors on bad file", func(t *testing.T) {
is := is.New(t)
err := env.Load("testdata/invalid")
is.True(err != nil)
is.Equal("missing equal sign on line 1 in testdata/invalid", err.Error())
})
}

func TestMustLoad(t *testing.T) {
t.Run("loads an environment file", func(t *testing.T) {
is := is.New(t)
defer unsetenv("hat", "hats")
env.MustLoad("testdata/env")
hat := env.GetStringOrDefault("hat", "regular")
is.Equal("party", hat)
hats := env.GetIntOrDefault("hats", 1)
is.Equal(2, hats)
})

t.Run("panics on no such file", func(t *testing.T) {
is := is.New(t)
recovered := false
defer func() {
if err := recover(); err != nil {
recovered = true
}
is.True(recovered)
}()
env.MustLoad()

})
}

func setenv(k, v string) func() {
if err := os.Setenv(k, v); err != nil {
panic(err)
Expand All @@ -100,3 +145,11 @@ func setenv(k, v string) func() {
}
}
}

func unsetenv(ks ...string) {
for _, k := range ks {
if err := os.Unsetenv(k); err != nil {
panic(err)
}
}
}
2 changes: 2 additions & 0 deletions testdata/env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hat=party
hats=2
1 change: 1 addition & 0 deletions testdata/invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
notvalid

0 comments on commit 496c70d

Please sign in to comment.