Skip to content

Commit

Permalink
Merge pull request #27 from nrfta/feat/v2
Browse files Browse the repository at this point in the history
feat: Move away from packr; Use embed instead
  • Loading branch information
josemarluedke authored Oct 25, 2023
2 parents 9c443ab + 43af018 commit 7093c82
Show file tree
Hide file tree
Showing 8 changed files with 521 additions and 183 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- main
- 'v*'
- "v*"
pull_request: {}

jobs:
Expand All @@ -14,17 +14,17 @@ jobs:

strategy:
matrix:
go-version: [1.12.x, 1.13.x]
go-version: [1.21.x]
os: [ubuntu-latest, macos-latest, windows-latest]

env:
ENV: test

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4

- name: Install Go
uses: actions/setup-go@v1
uses: actions/setup-go@v4
with:
go-version: ${{matrix.go-version}}

Expand Down
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
# go-config ![](https://github.com/neighborly/go-config/workflows/CI/badge.svg)
# go-config ![](https://github.com/nrfta/go-config/workflows/CI/badge.svg)

This package loads config information into a struct. It uses [viper](https://github.com/spf13/viper).

## Installation

```sh
go get github.com/neighborly/go-config
go get github.com/nrfta/go-config/v2
```

## Usage

To load the config data into a struct you will need two parameters

1) a variable of type **box**: To get the box type you will need to import ``` github.com/gobuffalo/packr``` . The box variable will hold the config data in the binary
2) a variable of type **"customStruct"** where customStruct is a struct you define to mirror the key values of your config

### Example

Consider this example config content
Expand All @@ -39,8 +34,9 @@ In the file you want to load the config in do the following:
// config/config.go

import (
"github.com/gobuffalo/packr"
"github.com/neighborly/go-config"
"embed"

"github.com/nrfta/go-config/v2"
)

// this MyAppConfig struct is the "custom struct" it has the same attributes that mirror the config json above
Expand All @@ -54,13 +50,16 @@ type MyAppConfig struct {
SegmentWriteKey string `mapstructure:"segment_write_key"`
}

//go:embed config.json config_test.json
var fs embed.FS

// the definition of our two paramaters
var (
Config MyAppConfig
)

func init() {
err := config.Load(packr.NewBox("."), &Config) // now the config data has been loaded into appConfig
err := config.Load(fs, &Config) // now the config data has been loaded into Config
if err != nil {
panic(err)
}
Expand Down
21 changes: 13 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package config

import (
"bytes"
"embed"
"errors"
"fmt"
"os"
"reflect"
"strings"

"github.com/gobuffalo/packr"
"github.com/spf13/viper"

"github.com/neighborly/go-errors"
)

// MetaConfig holds configuration for environment name and service name
Expand All @@ -19,7 +19,7 @@ type MetaConfig struct {
}

// Load config from file then from environment variables
func Load(box packr.Box, config interface{}) error {
func Load(fs embed.FS, config interface{}) error {
configType := "json"
viper.SetConfigType(configType)

Expand All @@ -30,13 +30,14 @@ func Load(box packr.Box, config interface{}) error {
viper.SetConfigName(configName)

configFile := configName + "." + configType
contents, err := box.Find(configFile)

contents, err := fs.ReadFile(configFile)
if err != nil {
return errors.Wrapf(err, "unable to read config from %s", configFile)
return fmt.Errorf("unable to read config from %s: %w", configFile, err)
}

if err := viper.ReadConfig(bytes.NewReader(contents)); err != nil {
return errors.Wrapf(err, "unable to read config from %s", configFile)
return fmt.Errorf("unable to read config from %s: %w", configFile, err)
}

return unmarshalConfig(config)
Expand All @@ -48,7 +49,11 @@ func unmarshalConfig(config interface{}) error {

// Read the config file again and consider environment variables at the same time
if err := viper.Unmarshal(config); err != nil {
return errors.Wrapf(err, "unable to unmarshal config at %s", viper.ConfigFileUsed())
return fmt.Errorf(
"unable to unmarshal config at %s: %w",
viper.ConfigFileUsed(),
err,
)
}

// Set the environment to be "test" if tests are being run.
Expand Down
12 changes: 7 additions & 5 deletions tests/unit/config_test.go → config_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package config_test

import (
"embed"
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/gobuffalo/packr"
"github.com/neighborly/go-config"
"github.com/nrfta/go-config/v2"
)

//go:embed config_test.json
var fs embed.FS

type MyAppConfig struct {
Meta config.MetaConfig

Expand All @@ -21,13 +24,12 @@ type MyAppConfig struct {
}

var (
testBox = packr.NewBox(".")
testConfig MyAppConfig
)

var _ = Describe("Test Load ", func() {
It("It should load config file", func() {
Expect(config.Load(testBox, &testConfig)).To(Succeed())
Expect(config.Load(fs, &testConfig)).To(Succeed())

Expect(testConfig.Meta.Environment).To(Equal("test"))
Expect(testConfig.Meta.ServiceName).To(Equal("my-app"))
Expand All @@ -43,7 +45,7 @@ var _ = Describe("Test Load With Environment Variables", func() {
os.Setenv("PORT", "5001")
os.Setenv("META_SERVICE_NAME", "service-api")

Expect(config.Load(testBox, &testConfig)).To(Succeed())
Expect(config.Load(fs, &testConfig)).To(Succeed())

Expect(testConfig.Meta.Environment).To(Equal("test"))
Expect(testConfig.Meta.ServiceName).To(Equal("service-api"))
Expand Down
File renamed without changes.
35 changes: 30 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
module github.com/neighborly/go-config
module github.com/nrfta/go-config/v2

go 1.13
go 1.21

require (
github.com/gobuffalo/packr v1.30.1
github.com/neighborly/go-errors v0.2.0
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.8.1
github.com/spf13/viper v1.6.2
github.com/spf13/viper v1.17.0
)

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hpcloud/tail v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 7093c82

Please sign in to comment.