Skip to content

Commit 2a0995b

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

3 files changed

Lines changed: 52 additions & 7 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: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
package config
55

66
import (
7+
"os"
8+
"reflect"
9+
"strings"
10+
"testing"
11+
712
. "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
813
"github.com/microsoft/go-sqlcmd/internal/output"
914
"github.com/microsoft/go-sqlcmd/internal/pal"
1015
"github.com/microsoft/go-sqlcmd/internal/secret"
1116
"github.com/stretchr/testify/assert"
12-
"os"
13-
"reflect"
14-
"strings"
15-
"testing"
17+
"github.com/stretchr/testify/require"
1618
)
1719

1820
func TestConfig(t *testing.T) {
@@ -380,6 +382,30 @@ func TestConfig_GetCurrentContextEndPointNotFoundPanic(t *testing.T) {
380382
})
381383
}
382384

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

0 commit comments

Comments
 (0)