forked from goraz/onion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new way to load variables like flags library
add consul layer
- Loading branch information
1 parent
0789309
commit e8f28a5
Showing
16 changed files
with
272 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
language: go | ||
dist: trusty | ||
go: | ||
- 1.1 | ||
- 1.2 | ||
- 1.3 | ||
- 1.4 | ||
- 1.5 | ||
- 1.7 | ||
- 1.8 | ||
- tip | ||
before_install: | ||
- go get -v github.com/smartystreets/goconvey | ||
- go get -v github.com/axw/gocov/gocov | ||
- go get -v github.com/mattn/goveralls | ||
- go get -v github.com/hashicorp/consul/testutil | ||
- if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi | ||
- go get -v github.com/hashicorp/consul | ||
- go get -v ./... | ||
script: | ||
- goveralls -v -service travis-ci -repotoken $COVERALLS_TOKEN || go test -v | ||
matrix: | ||
allow_failures: | ||
- go: 1.1 | ||
- goveralls -v -service travis-ci -repotoken $COVERALLS_TOKEN || go test -v ./... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package consulloader | ||
|
||
import ( | ||
"testing" | ||
|
||
"fmt" | ||
|
||
"github.com/hashicorp/consul/api" | ||
"github.com/hashicorp/consul/testutil" | ||
. "github.com/smartystreets/goconvey/convey" | ||
. "gopkg.in/fzerorubigd/onion.v3" | ||
) | ||
|
||
func TestConsulLoader(t *testing.T) { | ||
// Create a test Consul server | ||
srv1, err := testutil.NewTestServer() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer srv1.Stop() | ||
|
||
//// Create a secondary server, passing in configuration | ||
//// to avoid bootstrapping as we are forming a cluster. | ||
//srv2, err := testutil.NewTestServerConfig(func(c *testutil.TestServerConfig) { | ||
// c.Bootstrap = false | ||
//}) | ||
//if err != nil { | ||
// t.Fatal(err) | ||
//} | ||
//defer srv2.Stop() | ||
|
||
// Join the servers together | ||
//srv1.JoinLAN(t, srv2.LANAddr) | ||
|
||
Convey("Extra env in config", t, func() { | ||
client, err := api.NewClient( | ||
&api.Config{ | ||
HttpClient: srv1.HTTPClient, | ||
Address: srv1.HTTPAddr, | ||
}, | ||
) | ||
So(err, ShouldBeNil) | ||
|
||
o := New() | ||
layer := NewConsulLayer(client, "prefix") | ||
o.AddLazyLayer(layer) | ||
Convey("check data from env", func() { | ||
srv1.SetKV(t, "prefix/data/nested", []byte("TDN")) | ||
fmt.Println(string(srv1.GetKV(t, "prefix/data/nested"))) | ||
So(o.GetString("data.nested"), ShouldEqual, "TDN") | ||
}) | ||
|
||
Convey("check data not in env", func() { | ||
So(o.GetString("not.valid.data"), ShouldEqual, "") | ||
So(o.GetString(""), ShouldEqual, "") | ||
}) | ||
|
||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Package consulloader is a simple loader to handle Reading from consul. | ||
// its a simple read only functionality. | ||
package consulloader | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/hashicorp/consul/api" | ||
"gopkg.in/fzerorubigd/onion.v3" | ||
) | ||
|
||
type layer struct { | ||
client *api.KV | ||
prefix string | ||
} | ||
|
||
func (l layer) Get(path ...string) (interface{}, bool) { | ||
p := filepath.Join(l.prefix, filepath.Join(path...)) | ||
kv, _, err := l.client.Get(p, nil) | ||
if err != nil { | ||
// I don't think this is correct. may be panic? | ||
return nil, false | ||
} | ||
if kv == nil { | ||
return nil, false | ||
} | ||
return string(kv.Value), true | ||
} | ||
|
||
// NewConsulLayer create a new lazy layer from a consul client | ||
func NewConsulLayer(c *api.Client, prefix string) onion.LazyLayer { | ||
return &layer{ | ||
client: c.KV(), | ||
prefix: prefix, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.