diff --git a/README.md b/README.md index b04666f3c..c281e55dd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config/config.go b/config/config.go index 093c81f09..b8b28fec5 100644 --- a/config/config.go +++ b/config/config.go @@ -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" @@ -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 @@ -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 { diff --git a/config/config_test.go b/config/config_test.go index 4aa8bff52..fa8a5a570 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "os" "path/filepath" "testing" @@ -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 diff --git a/testdata/config/.tbls.yml b/testdata/config/.tbls.yml new file mode 100644 index 000000000..ed97d539c --- /dev/null +++ b/testdata/config/.tbls.yml @@ -0,0 +1 @@ +--- diff --git a/testdata/config/tbls.yml b/testdata/config/tbls.yml new file mode 100644 index 000000000..ed97d539c --- /dev/null +++ b/testdata/config/tbls.yml @@ -0,0 +1 @@ +---