Skip to content

Commit 1908b0b

Browse files
fix: reject non-YAML extensions for --sqlconfig flag
1 parent a183f6c commit 1908b0b

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ sqlcmd config connection-strings
8484
sqlcmd config view
8585
```
8686

87+
#### Custom configuration files
88+
89+
The `--sqlconfig` flag specifies a custom configuration file. The file must be YAML format with a `.yaml` or `.yml` extension:
90+
91+
```
92+
sqlcmd config --sqlconfig ./myproject.yaml add-endpoint --name ep1434 --address localhost --port 1434
93+
sqlcmd config --sqlconfig ./myproject.yaml view
94+
```
95+
96+
The default file (`~/.sqlcmd/sqlconfig`) has no extension and is also YAML.
97+
8798
### Versions
8899

89100
To see all version tags to choose from (2017, 2019, 2022 etc.), and install a specific version, run:

internal/config/config.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
package config
55

66
import (
7+
"fmt"
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
"testing"
12+
713
. "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
814
"github.com/microsoft/go-sqlcmd/internal/io/file"
915
"github.com/microsoft/go-sqlcmd/internal/io/folder"
1016
"github.com/microsoft/go-sqlcmd/internal/pal"
11-
"os"
12-
"path/filepath"
13-
"testing"
1417
)
1518

1619
var config Sqlconfig
@@ -24,6 +27,11 @@ func SetFileName(name string) {
2427
panic("name is empty")
2528
}
2629

30+
if ext := strings.ToLower(filepath.Ext(name)); ext != "" && ext != ".yaml" && ext != ".yml" {
31+
checkErr(fmt.Errorf("configuration file %q has unsupported extension %q (must be .yaml or .yml)", name, ext))
32+
return
33+
}
34+
2735
filename = name
2836

2937
file.CreateEmptyIfNotExists(filename)

internal/config/config_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,30 @@ func TestConfig_GetCurrentContextEndPointNotFoundPanic(t *testing.T) {
380380
})
381381
}
382382

383+
func TestSetFileName_RejectsNonYAMLExtension(t *testing.T) {
384+
var gotErr error
385+
originalCallback := errorCallback
386+
errorCallback = func(err error) { gotErr = err }
387+
t.Cleanup(func() { errorCallback = originalCallback })
388+
389+
SetFileName("config.json")
390+
assert.Error(t, gotErr)
391+
assert.Contains(t, gotErr.Error(), ".json")
392+
393+
gotErr = nil
394+
SetFileName("config.toml")
395+
assert.Error(t, gotErr)
396+
assert.Contains(t, gotErr.Error(), ".toml")
397+
398+
gotErr = nil
399+
SetFileName(pal.FilenameInUserHomeDotDirectory(".sqlcmd", "sqlconfig-test.yaml"))
400+
assert.NoError(t, gotErr)
401+
402+
gotErr = nil
403+
SetFileName(pal.FilenameInUserHomeDotDirectory(".sqlcmd", "sqlconfig-test.yml"))
404+
assert.NoError(t, gotErr)
405+
}
406+
383407
func TestConfig_DeleteContextThatDoesNotExist(t *testing.T) {
384408
assert.Panics(t, func() {
385409
contextOrdinal("does-not-exist")

0 commit comments

Comments
 (0)