Skip to content

Commit

Permalink
[BREAKING] Use github.com/k1LoW/expand to expand environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Oct 22, 2021
1 parent 5654076 commit 6935825
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 75 deletions.
23 changes: 9 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,6 @@ comments:
dsn: my://dbuser:dbpass@hostname:3306/dbname
```

`dsn:` can expand environment variables.

``` yaml
# .tbls.yml
dsn: my://${MYSQL_USER}:${MYSQL_PASSWORD}@hostname:3306/${MYSQL_DATABASE}
```

#### Support Datasource

tbls support following databases/datasources.
Expand Down Expand Up @@ -552,13 +545,6 @@ dsn:
docPath: doc/schema
```
`docPath:` can expand environment variables.

``` yaml
# .tbls.yml
docPath: ${DOC_PATH}
```

### Table format
`format:` is used to change the document format.
Expand Down Expand Up @@ -901,6 +887,15 @@ The `requiredVersion` setting defines a version constraint string. This defines
requiredVersion: '>= 1.42, < 2'
```

## Expand environment variables

All configuration values can be set by expanding the environment variables.

``` yaml
# .tbls.yml
dsn: my://${MYSQL_USER}:${MYSQL_PASSWORD}@hostname:3306/${MYSQL_DATABASE}
```

## Output formats

`tbls out` output in various formats.
Expand Down
36 changes: 2 additions & 34 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package config

import (
"bytes"
"fmt"
"html/template"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/aquasecurity/go-version/pkg/version"
"github.com/goccy/go-yaml"
"github.com/k1LoW/expand"
"github.com/k1LoW/tbls/dict"
"github.com/k1LoW/tbls/schema"
ver "github.com/k1LoW/tbls/version"
Expand Down Expand Up @@ -304,19 +302,10 @@ func (c *Config) LoadConfigFile(path string) error {

// LoadConfig load config from []byte
func (c *Config) LoadConfig(in []byte) error {
err := yaml.Unmarshal(in, c)
err := yaml.Unmarshal(expand.ExpandenvYAMLBytes(in), c)
if err != nil {
return errors.Wrap(errors.WithStack(err), "failed to load config file")
}
c.DSN.URL, err = parseWithEnviron(c.DSN.URL)
if err != nil {
return errors.Wrap(errors.WithStack(err), "failed to load config file")
}
c.DocPath, err = parseWithEnviron(c.DocPath)
if err != nil {
return errors.Wrap(errors.WithStack(err), "failed to load config file")
}

c.MergedDict.Merge(c.Dict.Dump())
return nil
}
Expand Down Expand Up @@ -579,27 +568,6 @@ func mergeDetectedRelations(s *schema.Schema, strategy *NamingStrategy) {
}
}

var (
re = regexp.MustCompile(`\${\s*([^{}]+)\s*}`)
re2 = regexp.MustCompile(`{{([^\.])`)
re3 = regexp.MustCompile(`__TBLS__(.)`)
)

func parseWithEnviron(v string) (string, error) {
replaced := re.ReplaceAllString(v, "{{.$1}}")
replaced2 := re2.ReplaceAllString(replaced, "__TBLS__$1")
tmpl, err := template.New("config").Parse(replaced2)
if err != nil {
return "", err
}
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, envMap())
if err != nil {
return "", err
}
return re3.ReplaceAllString(buf.String(), "{{$1"), nil
}

func envMap() map[string]string {
m := map[string]string{}
for _, kv := range os.Environ() {
Expand Down
24 changes: 0 additions & 24 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,6 @@ func TestDuplicateConfigFile(t *testing.T) {
}
}

var tests = []struct {
value string
want string
}{
{"${TBLS_ONE}/${TBLS_TWO}", "one/two"},
{"${TBLS_ONE}/${TBLS_TWO}/${TBLS_NONE}", "one/two/"},
{"${{TBLS_ONE}}", "${{TBLS_ONE}}"},
{"{{.TBLS_ONE}}/{{.TBLS_TWO}}", "one/two"},
}

func TestParseWithEnvirion(t *testing.T) {
_ = os.Setenv("TBLS_ONE", "one")
_ = os.Setenv("TBLS_TWO", "two")
for _, tt := range tests {
got, err := parseWithEnviron(tt.value)
if err != nil {
t.Fatal(err)
}
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
}
}

func TestMergeAditionalData(t *testing.T) {
s := schema.Schema{
Name: "testschema",
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ require (
github.com/gertd/go-pluralize v0.1.7
github.com/go-sql-driver/mysql v1.6.0
github.com/goccy/go-graphviz v0.0.9
github.com/goccy/go-yaml v1.8.9
github.com/goccy/go-yaml v1.9.3
github.com/google/go-cmp v0.5.5
github.com/k1LoW/expand v0.1.0
github.com/k1LoW/ffff v0.2.0
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/labstack/gommon v0.3.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me
github.com/goccy/go-graphviz v0.0.9 h1:s/FMMJ1Joj6La3S5ApO3Jk2cwM4LpXECC2muFx3IPQQ=
github.com/goccy/go-graphviz v0.0.9/go.mod h1:wXVsXxmyMQU6TN3zGRttjNn3h+iCAS7xQFC6TlNvLhk=
github.com/goccy/go-json v0.4.8/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-yaml v1.8.9 h1:4AEXg2qx+/w29jXnXpMY6mTckmYu1TMoHteKuMf0HFg=
github.com/goccy/go-yaml v1.8.9/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3KfscvA=
github.com/goccy/go-yaml v1.9.3 h1:9A7DkTBb7cZs5wqcqAhgR+2Ms8O7HTjT0SqOXO10HqM=
github.com/goccy/go-yaml v1.9.3/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3KfscvA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand Down Expand Up @@ -302,6 +302,8 @@ github.com/jstemmer/go-junit-report v0.9.1 h1:6QPYqodiu3GuPL+7mfx+NwDdp2eTkp9IfE
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/k1LoW/expand v0.1.0 h1:WnExzFli09OzQ2cZpaOiUjP+/sZ6DBpHQWWJAsWAZPY=
github.com/k1LoW/expand v0.1.0/go.mod h1:CojQ4SR1YvdeC4Srw8pTtF9PRnGr5pUvAkiNfdAwRQg=
github.com/k1LoW/ffff v0.2.0 h1:k5IYb2ggSKrkgVRkFnn1fs4xVUkJxanTH5gInmEKTDA=
github.com/k1LoW/ffff v0.2.0/go.mod h1:XuWQte6DCVCGKkvfeOFHTihuYWusf7dU+kp9jot7FlM=
github.com/k1LoW/fontdir v0.1.0 h1:c5JD8M7qOpFpQ5RF8tey7L+sC8IgsXAx3/MeOFzQA10=
Expand Down

0 comments on commit 6935825

Please # to comment.