Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Commit

Permalink
support custom Unmarshal, remove etcd and vault service
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Nov 20, 2018
1 parent 72ed95d commit 10f7bac
Show file tree
Hide file tree
Showing 30 changed files with 196 additions and 1,143 deletions.
45 changes: 10 additions & 35 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
sudo: false
language: go
go:
- 1.7
matrix:
include:
- go: "1.10.x"
- go: "1.11.x"

before_install:
- bash build_etcd.sh
- go get -t -v ./...
- cd $GOPATH/src/github.com/hashicorp/vault && git checkout v0.6.4 && cd -
env:
- GO111MODULE=on

install:
- go get github.com/haya14busa/reviewdog/cmd/reviewdog
- go get github.com/modocache/gover
before_install:
- go get -v -t ./...
- go get github.com/mattn/goveralls
- go get github.com/golang/lint/golint
- go get honnef.co/go/staticcheck/cmd/staticcheck
- go get honnef.co/go/simple/cmd/gosimple
- go get github.com/kisielk/errcheck
- go get honnef.co/go/unused/cmd/unused

before_script:
- echo $TRAVIS
- echo $TRAVIS_PULL_REQUEST
- echo $TRAVIS_PULL_REQUEST_BRANCH
- echo $TRAVIS_COMMIT
- echo $TRAVIS_PULL_REQUEST_SHA
- echo $TRAVIS_REPO_SLUG
- echo $TRAVIS_SECURE_ENV_VARS

script:
- go test -coverprofile=vault.coverprofile ./vault
- go test -coverprofile=etcd.coverprofile ./etcd
- >-
reviewdog -ci=travis
after_success:
- gover
- goveralls -coverprofile=gover.coverprofile -service=travis-ci

env:
global:
secure: IlYRR6Esh/i2VOoSJ4f8xarx4Z8paN5UZPCROuRdLAfoe/vM4FjQa9bkjwRdwQ5QdGZVFZvQVoXDzHiQrpM4tDuVUieKqJ6m2JtxL4kYgdNoeVvZpIxDkYy5jLRSGEm/GkZLrkcbaai8uy5HYtUW62UJJUgBRci0YQsDkZz5+AvKrAYuwQwbg+BQ1J131rsSmXJtyIgxJC20reJ1u3uCEs0Ib99g4l7czVd5sqmdukoaeMuSYQXDCYuU7wLDrga5x+9vFMHK6ifHvlw9ZuGVWPtN0H8UTAZJW5gNCk0LZr37OmhaapWmTVdhaJhCY1lsZlAdzZA3lx4hEb0h/6C6zQDFgA0M+SUegptJy20Hxr6Y3Cn6lduJeiSevEr1iNKVLTyGw1/XHd6pWQPOvugrRzCXF8LUwm6IVjhrnc/kW5RKd07jHGVS2XicFdyFyHEtkyZEOut/9bqxvGk+8d5l8ARRdweY5jg7grLHsDDjqskORowL1NIXzNd4BnTWUX7tAYs0D/TsfBN+TeMPuoPFQ/0Gv0W5RBDcMiAunYRtX1Le2PfUVK1sW9VBQGGHaMloHq5X4AHJQMerYkOHW88E6IajTBw7qhJfcE8OVn2lCgvD6wfFtxTWj/X3eB3CNe/y6bXTbG/+667ndvpymoNEcLnZcG57tB6i3JZPBWSGOfU=
- go test -v ./... -coverprofile=coverage.out && go tool cover -func=coverage.out
- goveralls -coverprofile=coverage.out -service=travis-ci
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016 Teambition
Copyright (c) 2016-2018 Teambition

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
test:
go test --race ./vault
go test --race ./etcd
go test --race ./...

cover:
rm -f *.coverprofile
go test -race -coverprofile=vault.coverprofile ./vault
go test -race -coverprofile=etcd.coverprofile ./etcd
gover
go tool cover -html=gover.coverprofile
rm -f *.coverprofile
rm -f *.out
go test -v ./... -coverprofile=coverage.out && go tool cover -func=coverage.out
go tool cover -html=coverage.out
rm -f *.out

.PHONY: test cover
148 changes: 42 additions & 106 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Confl
=====
Configuration reload with etcd, security storage with vault!
Load or reload configuration!

[![Build Status](http://img.shields.io/travis/teambition/confl.svg?style=flat-square)](https://travis-ci.org/teambition/confl)
[![Coverage Status](http://img.shields.io/coveralls/teambition/confl.svg?style=flat-square)](https://coveralls.io/r/teambition/confl)
Expand All @@ -24,116 +24,52 @@ Configuration reload with etcd, security storage with vault!
go get -u -v github.com/teambition/confl
```

#### Start vault with etcd backend

```shell
cat vault.hcl
```

```hcl
backend "etcd" {
# etcd listen address
address = "http://127.0.0.1:2379"
# root path in etcd
path = "vault/"
}
listener "tcp" {
# listen address
address = "localhost:8200"
# secure config
tls_disable = 1
# tls_cert_file = /path/to/cert/file
# tls_key_file = /path/to/key/file
# tls_min_version = tls12
}
```

```shell
vault server -config=vault.hcl
```

### Usage

#### Watch a configuration file

More [example](examples/watch_file)


#### Watch a backend storage

```go
// Options

// vault
// use https://github.com/kelseyhightower/envconfig to parse the environment variables for config
// it's container-friendly like docker, rocket
type Config struct {
// type of auth one in (app-id, token, github, userpass)
AuthType string
// vault service address
Address string
// AuthType = app-id
// "app id auth backend"(See https://www.vaultproject.io/docs/auth/app-id.html)
// this is more useful for micro services
// every micro service can be given a app_id to distinguish between identities
AppID string
UserID string
// AuthType = userpass
// "userpass auth backend"(see https://www.vaultproject.io/docs/auth/userpass.html)
Username string
Password string
// AuthType = token or github
// auth token (See https://www.vaultproject.io/docs/auth/token.html)
Token string
// security connection
// Cert and Key are the pair of x509
// the path of certificate file
Cert string
// the path of certificate'key file
Key string
// the path of CACert file
CAcert string
// loop interval
// vaule likes `10s` `1m` `1h`
Interval string
}

// etcd
// Config etcd configuration
type Config struct {
// cluseter addresses
Clusters []string
// security connection
// the path of certificate file
Cert string
// the path of certificate'key file
Key string
// the path of CACert file
CAcert string
// auth user/pass
Username string
Password string
package main

import (
"fmt"

"github.com/teambition/confl"
"github.com/teambition/confl/examples/config"
"gopkg.in/yaml.v2"
)

func main() {
watcher, err := confl.NewFileWatcher(&config.Config{}, "./default.yaml", yaml.Unmarshal)
if err != nil {
panic(err)
}
defer watcher.Close()

watcher.OnError(func(err error) {
fmt.Println("your error handler start")
fmt.Println(err)
})

// add hook for update events
// perhaps you need reload something that depends the configuration
watcher.AddHook(func(oc, nc interface{}) {
ocfg := oc.(config.Config)
ncfg := nc.(config.Config)
// use cfg
fmt.Printf("old config: %#v\n", ocfg)
fmt.Printf("new config: %#v\n", ncfg)
})

// get configuration from watcher
cfg := watcher.Config().(config.Config)
// use cfg
fmt.Printf("load config: %#v\n", cfg)

// start watch
// it is a blocking method choose run with `go` by situation
watcher.Watch()
}
```

More [example](examples/watch_store)

## Development

### Test

1. start a local etcd service:
```shell
etcd
```

2. start local valut service:
```shell
vault server -dev
```

3. test with cover:
```shell
make cover
```
More [example](./examples)
13 changes: 0 additions & 13 deletions build_etcd.sh

This file was deleted.

15 changes: 15 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package confl_test

// Config - your configuration struct
type Config struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Token string `yaml:"token"`
In ConfigIn `yaml:"in"`
}

// ConfigIn -
type ConfigIn struct {
Addr string `yaml:"addr"`
Pass string `yaml:"pass"`
}
9 changes: 9 additions & 0 deletions config_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"username": "username",
"password": "1234",
"token": "4321",
"in": {
"addr": "localhost",
"pass": "666"
}
}
6 changes: 6 additions & 0 deletions config_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
username: username
password: '1234'
token: '4321'
in:
addr: localhost
pass: '666'
105 changes: 0 additions & 105 deletions etcd/client.go

This file was deleted.

0 comments on commit 10f7bac

Please sign in to comment.