Skip to content

Commit 68d5292

Browse files
authored
Merge pull request #4 from eduardbcom/search-schema-folder-within-current-binary-dir-by-default
Search schema folder within current binary dir by default
2 parents e46a105 + 515cf98 commit 68d5292

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

internal/schema/schema.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"os"
1010
"path"
11+
"path/filepath"
1112

1213
ucl "github.com/cesanta/ucl"
1314
validateJSONSchema "github.com/cesanta/validate-json/schema"
@@ -54,19 +55,21 @@ func (s *Schema) Validate(cfg *config.Config) error {
5455
func New() (*Schema, error) {
5556
files := make(map[string]string)
5657

57-
if len(*commandLineSchemaPath) == 0 {
58-
return nil, nil
58+
schemaDir, err := getSchemaDir()
59+
60+
if err != nil {
61+
return nil, err
5962
}
6063

61-
if _files, err := ioutil.ReadDir(*commandLineSchemaPath); err != nil {
64+
if _files, err := ioutil.ReadDir(schemaDir); err != nil {
6265
if os.IsNotExist(err) {
6366
return nil, nil
6467
}
6568

6669
return nil, err
6770
} else {
6871
for _, _file := range _files {
69-
files[_file.Name()] = path.Join(*commandLineSchemaPath, _file.Name())
72+
files[_file.Name()] = path.Join(schemaDir, _file.Name())
7073
}
7174
}
7275

@@ -114,3 +117,15 @@ func readSchema(filePath string) (parsedFileContent ucl.Value, err error) {
114117

115118
return
116119
}
120+
121+
func getSchemaDir() (string, error) {
122+
if len(*commandLineSchemaPath) > 0 {
123+
return filepath.Abs(*commandLineSchemaPath)
124+
}
125+
126+
if dir, err := filepath.Abs(filepath.Dir(os.Args[0])); err != nil {
127+
return "", err
128+
} else {
129+
return path.Join(dir, "schema"), nil
130+
}
131+
}

0 commit comments

Comments
 (0)