Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add default config file name tbls.yml #186

Merged
merged 4 commits into from
Mar 26, 2020
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ $ docker pull k1low/tbls:latest

### Document a database

Add `.tbls.yml` file to your repository.
Add `.tbls.yml` ( or `tbls.yml` ) file to your repository.

``` yaml
# .tbls.yml
Expand Down
17 changes: 13 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (
"github.com/pkg/errors"
)

const defaultConfigFilePath = ".tbls.yml"
const defaultDocPath = "dbdoc"

var defaultConfigFilePaths = []string{".tbls.yml", "tbls.yml"}

// DefaultERFormat is the default ER diagram format
const DefaultERFormat = "png"

Expand All @@ -42,6 +43,7 @@ type Config struct {
Comments []AdditionalComment `yaml:"comments"`
Dict dict.Dict `yaml:"dict"`
MergedDict dict.Dict `yaml:"-"`
root string
}

// Format is document format setting
Expand Down Expand Up @@ -209,11 +211,18 @@ func (c *Config) LoadEnviron() error {
// LoadConfigFile load config file
func (c *Config) LoadConfigFile(path string) error {
if path == "" {
path = defaultConfigFilePath
if _, err := os.Lstat(path); err != nil {
return nil
for _, p := range defaultConfigFilePaths {
if f, err := os.Stat(filepath.Join(c.root, p)); err == nil && !f.IsDir() {
if path != "" {
return fmt.Errorf("duplicate config file [%s, %s]", path, p)
}
path = p
}
}
}
if path == "" {
return nil
}

fullPath, err := filepath.Abs(path)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -59,6 +60,17 @@ func TestLoadConfigFile(t *testing.T) {
}
}

func TestDuplicateConfigFile(t *testing.T) {
config := &Config{
root: filepath.Join(testdataDir(), "config"),
}
got := config.LoadConfigFile("")
want := "duplicate config file [.tbls.yml, tbls.yml]"
if fmt.Sprintf("%v", got) != want {
t.Errorf("got %v\nwant %v", got, want)
}
}

var tests = []struct {
value string
want string
Expand Down
1 change: 1 addition & 0 deletions testdata/config/.tbls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
1 change: 1 addition & 0 deletions testdata/config/tbls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---