Skip to content

Commit d60eadb

Browse files
xsadiaorisano
andauthored
feat(cmd): Support sqlc.yml configuration file (#2828)
Read `sqlc.yml` files in addition to `sqlc.yaml` and `sqlc.json`. --------- Co-authored-by: Nao Yonashiro <owan.orisano@gmail.com>
1 parent 170059f commit d60eadb

File tree

9 files changed

+28
-18
lines changed

9 files changed

+28
-18
lines changed

.github/ISSUE_TEMPLATE/BUG_REPORT.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ body:
5151
id: config
5252
attributes:
5353
label: Configuration
54-
description: Please include the sqlc.yaml or sqlc.json file you using in your project. This will be automatically formatted, so no need for backticks.
54+
description: Please include the sqlc.(yaml|yml) or sqlc.json file you using in your project. This will be automatically formatted, so no need for backticks.
5555
render: yaml
5656
- type: input
5757
id: playground

docs/reference/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Configuration
22

3-
The `sqlc` tool is configured via a `sqlc.yaml` or `sqlc.json` file. This
3+
The `sqlc` tool is configured via a `sqlc.(yaml|yml)` or `sqlc.json` file. This
44
file must be in the directory where the `sqlc` command is run.
55

66
## Version 2

docs/tutorials/getting-started-mysql.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Initialize a new Go module named `tutorial.sql.dev/app`
1111
go mod init tutorial.sqlc.dev/app
1212
```
1313

14-
sqlc looks for either a `sqlc.yaml` or `sqlc.json` file in the current
14+
sqlc looks for either a `sqlc.(yaml|yml)` or `sqlc.json` file in the current
1515
directory. In our new directory, create a file named `sqlc.yaml` with the
1616
following contents:
1717

docs/tutorials/getting-started-postgresql.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Getting started with PostgreSQL
1+
# Getting started with PostgreSQL
22

33
This tutorial assumes that the latest version of sqlc is
44
[installed](../overview/install.md) and ready to use.
@@ -11,7 +11,7 @@ Initialize a new Go module named `tutorial.sqlc.dev/app`
1111
go mod init tutorial.sqlc.dev/app
1212
```
1313

14-
sqlc looks for either a `sqlc.yaml` or `sqlc.json` file in the current
14+
sqlc looks for either a `sqlc.(yaml|yml)` or `sqlc.json` file in the current
1515
directory. In our new directory, create a file named `sqlc.yaml` with the
1616
following contents:
1717

docs/tutorials/getting-started-sqlite.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Initialize a new Go module named `tutorial.sql.dev/app`
1111
go mod init tutorial.sqlc.dev/app
1212
```
1313

14-
sqlc looks for either a `sqlc.yaml` or `sqlc.json` file in the current
14+
sqlc looks for either a `sqlc.(yaml|yml)` or `sqlc.json` file in the current
1515
directory. In our new directory, create a file named `sqlc.yaml` with the
1616
following contents:
1717

internal/cmd/generate.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ func readConfig(stderr io.Writer, dir, filename string) (string, *config.Config,
7777
if filename != "" {
7878
configPath = filepath.Join(dir, filename)
7979
} else {
80-
var yamlMissing, jsonMissing bool
80+
var yamlMissing, jsonMissing, ymlMissing bool
8181
yamlPath := filepath.Join(dir, "sqlc.yaml")
82+
ymlPath := filepath.Join(dir, "sqlc.yml")
8283
jsonPath := filepath.Join(dir, "sqlc.json")
8384

8485
if _, err := os.Stat(yamlPath); os.IsNotExist(err) {
@@ -88,18 +89,27 @@ func readConfig(stderr io.Writer, dir, filename string) (string, *config.Config,
8889
jsonMissing = true
8990
}
9091

91-
if yamlMissing && jsonMissing {
92-
fmt.Fprintln(stderr, "error parsing configuration files. sqlc.yaml or sqlc.json: file does not exist")
92+
if _, err := os.Stat(ymlPath); os.IsNotExist(err) {
93+
ymlMissing = true
94+
}
95+
96+
if yamlMissing && ymlMissing && jsonMissing {
97+
fmt.Fprintln(stderr, "error parsing configuration files. sqlc.(yaml|yml) or sqlc.json: file does not exist")
9398
return "", nil, errors.New("config file missing")
9499
}
95100

96-
if !yamlMissing && !jsonMissing {
97-
fmt.Fprintln(stderr, "error: both sqlc.json and sqlc.yaml files present")
98-
return "", nil, errors.New("sqlc.json and sqlc.yaml present")
101+
if (!yamlMissing || !ymlMissing) && !jsonMissing {
102+
fmt.Fprintln(stderr, "error: both sqlc.json and sqlc.(yaml|yml) files present")
103+
return "", nil, errors.New("sqlc.json and sqlc.(yaml|yml) present")
99104
}
100105

101-
configPath = yamlPath
102-
if yamlMissing {
106+
if jsonMissing {
107+
if yamlMissing {
108+
configPath = ymlPath
109+
} else {
110+
configPath = yamlPath
111+
}
112+
} else {
103113
configPath = jsonPath
104114
}
105115
}

internal/endtoend/ddl_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestValidSchema(t *testing.T) {
3939
if err != nil {
4040
return err
4141
}
42-
if filepath.Base(path) == "sqlc.json" || filepath.Base(path) == "sqlc.yaml" {
42+
if filepath.Base(path) == "sqlc.json" || filepath.Base(path) == "sqlc.yaml" || filepath.Base(path) == "sqlc.yml" {
4343
stderr := filepath.Join(filepath.Dir(path), "stderr.txt")
4444
if _, err := os.Stat(stderr); !os.IsNotExist(err) {
4545
return nil

internal/endtoend/endtoend_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestReplay(t *testing.T) {
8686
if err != nil {
8787
return err
8888
}
89-
if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" {
89+
if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" || info.Name() == "sqlc.yml" {
9090
dirs = append(dirs, filepath.Dir(path))
9191
return filepath.SkipDir
9292
}
@@ -251,7 +251,7 @@ func BenchmarkReplay(b *testing.B) {
251251
if err != nil {
252252
return err
253253
}
254-
if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" {
254+
if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" || info.Name() == "sqlc.yml" {
255255
dirs = append(dirs, filepath.Dir(path))
256256
return filepath.SkipDir
257257
}

scripts/regenerate/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func regenerate(dir string) error {
3939
if info.IsDir() {
4040
return nil
4141
}
42-
if strings.HasSuffix(path, "sqlc.json") || strings.HasSuffix(path, "sqlc.yaml") {
42+
if strings.HasSuffix(path, "sqlc.json") || strings.HasSuffix(path, "sqlc.yaml") || strings.HasSuffix(path, "sqlc.yml") {
4343
cwd := filepath.Dir(path)
4444
command, err := parseExecCommand(cwd)
4545
if err != nil {

0 commit comments

Comments
 (0)