Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ sqlcmd config connection-strings
sqlcmd config view
```

#### Custom configuration files

The `--sqlconfig` flag specifies a custom configuration file. The file must be YAML. Extensionless filenames are allowed; if the file has an extension, it must be `.yaml` or `.yml`:

```
sqlcmd config --sqlconfig ./myproject.yaml add-endpoint --name ep1434 --address localhost --port 1434
sqlcmd config --sqlconfig ./myproject.yaml view
```

### Versions

To see all version tags to choose from (2017, 2019, 2022 etc.), and install a specific version, run:
Expand Down
14 changes: 11 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
package config

import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

. "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
"github.com/microsoft/go-sqlcmd/internal/io/file"
"github.com/microsoft/go-sqlcmd/internal/io/folder"
"github.com/microsoft/go-sqlcmd/internal/pal"
"os"
"path/filepath"
"testing"
)

var config Sqlconfig
Expand All @@ -24,6 +27,11 @@ func SetFileName(name string) {
panic("name is empty")
}

if ext := strings.ToLower(filepath.Ext(name)); ext != "" && ext != ".yaml" && ext != ".yml" {
checkErr(fmt.Errorf("configuration file %q has unsupported extension %q (must be .yaml or .yml)", name, ext))
return
}

filename = name

file.CreateEmptyIfNotExists(filename)
Expand Down
37 changes: 33 additions & 4 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
package config

import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"

. "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
"github.com/microsoft/go-sqlcmd/internal/output"
"github.com/microsoft/go-sqlcmd/internal/pal"
"github.com/microsoft/go-sqlcmd/internal/secret"
"github.com/stretchr/testify/assert"
"os"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/require"
)

func TestConfig(t *testing.T) {
Expand Down Expand Up @@ -380,6 +383,32 @@ func TestConfig_GetCurrentContextEndPointNotFoundPanic(t *testing.T) {
})
}

func TestSetFileName_RejectsNonYAMLExtension(t *testing.T) {
var gotErr error
originalCallback := errorCallback
errorCallback = func(err error) { gotErr = err }
t.Cleanup(func() { errorCallback = originalCallback })

SetFileName("config.json")
require.Error(t, gotErr)
assert.Contains(t, gotErr.Error(), ".json")

gotErr = nil
SetFileName("config.toml")
require.Error(t, gotErr)
assert.Contains(t, gotErr.Error(), ".toml")

tempDir := t.TempDir()

gotErr = nil
SetFileName(filepath.Join(tempDir, t.Name()+".yaml"))
assert.NoError(t, gotErr)

gotErr = nil
SetFileName(filepath.Join(tempDir, t.Name()+".yml"))
assert.NoError(t, gotErr)
}

func TestConfig_DeleteContextThatDoesNotExist(t *testing.T) {
assert.Panics(t, func() {
contextOrdinal("does-not-exist")
Expand Down
Loading