Skip to content

Commit bcb1ebb

Browse files
committed
Set defaultDocPath
1 parent ee5c56e commit bcb1ebb

File tree

7 files changed

+80
-66
lines changed

7 files changed

+80
-66
lines changed

cmd/diff.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,10 @@ var diffCmd = &cobra.Command{
4444

4545
if configPath == "" && additionalDataPath != "" {
4646
fmt.Println("Warning: `--add` option is deprecated. Use `--config`")
47-
err = c.LoadConfigFile(additionalDataPath)
48-
if err != nil {
49-
printError(err)
50-
os.Exit(1)
51-
}
52-
} else {
53-
err = c.LoadConfigFile(configPath)
54-
if err != nil {
55-
printError(err)
56-
os.Exit(1)
57-
}
47+
configPath = additionalDataPath
5848
}
5949

60-
c.LoadArgs(args)
50+
err = c.Load(configPath, args)
6151
if err != nil {
6252
printError(err)
6353
os.Exit(1)

cmd/doc.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,10 @@ var docCmd = &cobra.Command{
5454

5555
if configPath == "" && additionalDataPath != "" {
5656
fmt.Println("Warning: `--add` option is deprecated. Use `--config`")
57-
err = c.LoadConfigFile(additionalDataPath)
58-
if err != nil {
59-
printError(err)
60-
os.Exit(1)
61-
}
62-
} else {
63-
err = c.LoadConfigFile(configPath)
64-
if err != nil {
65-
printError(err)
66-
os.Exit(1)
67-
}
57+
configPath = additionalDataPath
6858
}
6959

70-
c.LoadArgs(args)
60+
err = c.Load(configPath, args)
7161
if err != nil {
7262
printError(err)
7363
os.Exit(1)

cmd/lint.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,7 @@ var lintCmd = &cobra.Command{
4343
os.Exit(1)
4444
}
4545

46-
err = c.LoadConfigFile(configPath)
47-
if err != nil {
48-
printError(err)
49-
os.Exit(1)
50-
}
51-
52-
c.LoadArgs(args)
46+
err = c.Load(configPath, args)
5347
if err != nil {
5448
printError(err)
5549
os.Exit(1)

cmd/out.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,10 @@ var outCmd = &cobra.Command{
5959

6060
if configPath == "" && additionalDataPath != "" {
6161
fmt.Println("Warning: `--add` option is deprecated. Use `--config`")
62-
err = c.LoadConfigFile(additionalDataPath)
63-
if err != nil {
64-
printError(err)
65-
os.Exit(1)
66-
}
67-
} else {
68-
err = c.LoadConfigFile(configPath)
69-
if err != nil {
70-
printError(err)
71-
os.Exit(1)
72-
}
62+
configPath = additionalDataPath
7363
}
7464

75-
c.LoadArgs(args)
65+
err = c.Load(configPath, args)
7666
if err != nil {
7767
printError(err)
7868
os.Exit(1)

config/config.go

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
"gopkg.in/yaml.v2"
1515
)
1616

17-
const configDefaultPath = ".tbls.yml"
17+
const defaultConfigFilePath = ".tbls.yml"
18+
const defaultDocPath = "schema"
1819

1920
// Config is tbls config
2021
type Config struct {
@@ -43,41 +44,69 @@ type AdditionalComment struct {
4344

4445
// NewConfig return Config
4546
func NewConfig() (*Config, error) {
46-
docPath := os.Getenv("TBLS_DOC_PATH")
47-
if docPath == "" {
48-
docPath = "."
49-
}
50-
5147
c := Config{
52-
DSN: os.Getenv("TBLS_DSN"),
53-
DocPath: docPath,
48+
DSN: "",
49+
DocPath: "",
5450
}
5551
return &c, nil
5652
}
5753

54+
// Load load config with all method
55+
func (c *Config) Load(configPath string, args []string) error {
56+
err := c.LoadConfigFile(configPath)
57+
if err != nil {
58+
return err
59+
}
60+
61+
err = c.LoadEnviron()
62+
if err != nil {
63+
return err
64+
}
65+
66+
err = c.LoadArgs(args)
67+
if err != nil {
68+
return err
69+
}
70+
71+
if c.DocPath == "" {
72+
c.DocPath = defaultDocPath
73+
}
74+
75+
return nil
76+
}
77+
78+
// LoadEnviron load environment variables
79+
func (c *Config) LoadEnviron() error {
80+
dsn := os.Getenv("TBLS_DSN")
81+
if dsn != "" {
82+
c.DSN = dsn
83+
}
84+
docPath := os.Getenv("TBLS_DOC_PATH")
85+
if docPath != "" {
86+
c.DocPath = docPath
87+
}
88+
return nil
89+
}
90+
5891
// LoadArgs load args slice
5992
func (c *Config) LoadArgs(args []string) error {
93+
if len(args) > 2 {
94+
return errors.WithStack(errors.New("too many arguments"))
95+
}
6096
if len(args) == 2 {
6197
c.DSN = args[0]
6298
c.DocPath = args[1]
6399
}
64-
if len(args) > 2 {
65-
return errors.WithStack(errors.New("too many arguments"))
66-
}
67100
if len(args) == 1 {
68-
if c.DSN == "" {
69-
c.DSN = args[0]
70-
} else {
71-
c.DocPath = args[0]
72-
}
101+
c.DSN = args[0]
73102
}
74103
return nil
75104
}
76105

77106
// LoadConfigFile load config file
78107
func (c *Config) LoadConfigFile(path string) error {
79108
if path == "" {
80-
path = configDefaultPath
109+
path = defaultConfigFilePath
81110
if _, err := os.Lstat(path); err != nil {
82111
return nil
83112
}

config/config_test.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ import (
88
"github.com/k1LoW/tbls/schema"
99
)
1010

11+
func TestLoadDefault(t *testing.T) {
12+
configFilepath := filepath.Join(testdataDir(), "empty.yml")
13+
config, err := NewConfig()
14+
if err != nil {
15+
t.Fatal(err)
16+
}
17+
err = config.Load(configFilepath, []string{})
18+
if err != nil {
19+
t.Fatal(err)
20+
}
21+
expected := ""
22+
if config.DSN != expected {
23+
t.Errorf("actual %v\nwant %v", config.DSN, expected)
24+
}
25+
expected2 := "schema"
26+
if config.DocPath != expected2 {
27+
t.Errorf("actual %v\nwant %v", config.DocPath, expected2)
28+
}
29+
}
30+
1131
func TestLoadConfigFile(t *testing.T) {
1232
_ = os.Setenv("TBLS_TEST_PG_PASS", "pgpass")
1333
_ = os.Setenv("TBLS_TEST_PG_DOC_PATH", "sample/pg")
@@ -54,12 +74,6 @@ func TestParseWithEnvirion(t *testing.T) {
5474
}
5575
}
5676

57-
func testdataDir() string {
58-
wd, _ := os.Getwd()
59-
dir, _ := filepath.Abs(filepath.Join(filepath.Dir(wd), "testdata"))
60-
return dir
61-
}
62-
6377
func TestMergeAditionalData(t *testing.T) {
6478
s := schema.Schema{
6579
Name: "testschema",
@@ -123,3 +137,9 @@ func TestMergeAditionalData(t *testing.T) {
123137
t.Errorf("actual %v\nwant %v", actual2, expected2)
124138
}
125139
}
140+
141+
func testdataDir() string {
142+
wd, _ := os.Getwd()
143+
dir, _ := filepath.Abs(filepath.Join(filepath.Dir(wd), "testdata"))
144+
return dir
145+
}

testdata/empty.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

0 commit comments

Comments
 (0)