Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
language: go
go:
- 1.9
- "1.10"
- 1.11
- 1.12
- 1.13
- 1.14
- 1.15
- 1.16
- 1.17
- tip
before_install:
- go get -v github.com/smartystreets/goconvey
Expand All @@ -16,5 +13,6 @@ before_install:
- sleep 3
script:
- make update
- make lint
- goveralls -v -service travis-ci -repotoken $COVERALLS_TOKEN || make test

2 changes: 1 addition & 1 deletion .travis/install_etcd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -o pipefail

ROOT=$(dirname "${BASH_SOURCE}")/../..

ETCD_VERSION=${ETCD_VERSION:-v3.3.17}
ETCD_VERSION=${ETCD_VERSION:-v3.5.0}

mkdir -p "${ROOT}/third_party"
cd "${ROOT}/third_party"
Expand Down
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
export ROOT:=$(realpath $(dir $(firstword $(MAKEFILE_LIST))))
test:
go test -v -race ./...

update:
go get -u ./...
go get -u -t ./...

tidy:
go mod tidy

bin/golangci-lint:
mkdir -p $(ROOT)/bin
echo "*"> $(ROOT)/bin/.gitignore
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(ROOT)/bin v1.27.0

lint: bin/golangci-lint
bin/golangci-lint run

2 changes: 1 addition & 1 deletion ciphers/secconf/sec_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestNewCipher(t *testing.T) {

secReader := bytes.NewReader([]byte(secring))
c, err := NewCipher(secReader)

So(err, ShouldBeNil)
br, err := c.Decrypt(bytes.NewBuffer(b))
So(err, ShouldBeNil)
So(string(br), ShouldEqual, data)
Expand Down
2 changes: 1 addition & 1 deletion cli/onioncli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"net/url"
"os"

"github.com/coreos/etcd/client"
"github.com/goraz/onion/ciphers/secconf"
"github.com/ogier/pflag"
"go.etcd.io/etcd/client"
)

var (
Expand Down
8 changes: 3 additions & 5 deletions configwatch/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestRefWatch(t *testing.T) {
So(err, ShouldBeNil)
ly := l.(streamReload)
o := onion.New(l)
Watch(o)
tick := Watch(o)

So(i32.Int(), ShouldEqual, 32)
So(i64.Int64(), ShouldEqual, 64)
Expand All @@ -59,12 +59,10 @@ func TestRefWatch(t *testing.T) {
data["f32"] = 99.0
data["f64"] = 9999.0

c := o.ReloadWatch()
time.Sleep(time.Second)
So(ly.Reload(context.Background(), mapToJson(data), "json"), ShouldBeNil)
// This is just a hack for the load to finish
<-c
time.Sleep(3 * time.Second)

<-tick
So(i32.Int(), ShouldEqual, 132)
So(i64.Int64(), ShouldEqual, 164)
So(d.Duration(), ShouldEqual, time.Minute)
Expand Down
17 changes: 12 additions & 5 deletions configwatch/wtach.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ func (rw *RefWatch) watchLoop(o *onion.Onion) {
}

// Watch get an onion and watch over it for changes in the layers.
func (rw *RefWatch) Watch(ctx context.Context, o *onion.Onion) {
func (rw *RefWatch) Watch(ctx context.Context, o *onion.Onion) <-chan struct{} {
rw.watchLoop(o)
w := make(chan struct{})
go func() {
for {
ch := o.ReloadWatch()
Expand All @@ -177,17 +178,23 @@ func (rw *RefWatch) Watch(ctx context.Context, o *onion.Onion) {
return
case <-ch:
rw.watchLoop(o)
select {
case w <- struct{}{}:
default:
}
}
}
}()

return w
}

// Watch get an onion and watch over it for changes in the layers.
func Watch(o *onion.Onion) {
WatchContext(context.Background(), o)
func Watch(o *onion.Onion) <-chan struct{} {
return WatchContext(context.Background(), o)
}

// WatchContext get an onion and watch over it for changes in the layers.
func WatchContext(ctx context.Context, o *onion.Onion) {
w.Watch(ctx, o)
func WatchContext(ctx context.Context, o *onion.Onion) <-chan struct{} {
return w.Watch(ctx, o)
}
16 changes: 16 additions & 0 deletions env_layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,19 @@ func NewEnvLayerPrefix(separator string, prefix string) Layer {

return NewMapLayer(data)
}

// NewFlatEnvLayerPrefix create new env layer, with all values with the same prefix however
// this does not seperate the value hierarchically. This can be used for layered configurations
// such as csv or ini instead of json or yaml.
func NewFlatEnvLayerPrefix(separator string, prefix string) Layer {
var data map[string]interface{}
pf := strings.ToUpper(prefix) + separator
for _, env := range os.Environ() {
if strings.HasPrefix(env, pf) {
k := strings.Trim(strings.Split(env, "=")[0], "\t\n ")
ck := strings.ToLower(strings.TrimPrefix(k, pf))
data = buildMap(data, os.Getenv(k), ck)
}
}
return NewMapLayer(data)
}
4 changes: 4 additions & 0 deletions env_layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ func TestNewEnvLayer(t *testing.T) {
l2 := NewEnvLayerPrefix("_", "key")
o2 := New(l2)
So(o2.GetInt("test.sep"), ShouldEqual, 1)

l3 := NewFlatEnvLayerPrefix("_", "key")
o3 := New(l3)
So(o3.GetInt("test_sep"), ShouldEqual, 1)
})
}
36 changes: 22 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
module github.com/goraz/onion

go 1.13
go 1.17

require (
github.com/BurntSushi/toml v0.3.1
github.com/coreos/etcd v3.3.17+incompatible
github.com/coreos/etcd v3.3.25+incompatible // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/etcd-io/etcd v3.3.17+incompatible
github.com/fsnotify/fsnotify v1.4.7
github.com/imdario/mergo v0.3.8
github.com/json-iterator/go v1.1.8 // indirect
github.com/magiconair/properties v1.8.1
github.com/mitchellh/mapstructure v1.1.2
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4 // indirect
github.com/fsnotify/fsnotify v1.5.0
github.com/google/uuid v1.0.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20210822113901-9ebd50d28389 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.9.5 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/magiconair/properties v1.8.5
github.com/mitchellh/mapstructure v1.4.1
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/ogier/pflag v0.0.1
github.com/pelletier/go-toml v1.6.0
github.com/pelletier/go-toml v1.9.3
github.com/prometheus/client_golang v1.0.0 // indirect
github.com/skarademir/naturalsort v0.0.0-20150715044055-69a5d87bef62
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708
golang.org/x/sys v0.0.0-20191115151921-52ab43148777 // indirect
gopkg.in/yaml.v2 v2.2.5
github.com/smartystreets/assertions v1.2.0 // indirect
github.com/smartystreets/goconvey v1.6.4
go.etcd.io/etcd v3.3.25+incompatible
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
golang.org/x/sys v0.0.0-20210820121016-41cdb8703e55 // indirect
google.golang.org/grpc v1.23.1 // indirect
gopkg.in/yaml.v2 v2.4.0
sigs.k8s.io/yaml v1.1.0 // indirect
)
Loading