Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 10 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
package config

import (
"os"
"path/filepath"
"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 @@ -26,8 +27,13 @@ func SetFileName(name string) {

filename = name

// Validate extension before creating the file
err := validateConfigFileExtension(filename)
checkErr(err)

file.CreateEmptyIfNotExists(filename)
configureViper(filename)
err = configureViper(filename)
checkErr(err)
}

func SetFileNameForTest(t *testing.T) {
Expand Down
24 changes: 23 additions & 1 deletion internal/config/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ package config

import (
"bytes"
"path/filepath"
"strings"

"github.com/microsoft/go-sqlcmd/internal/localizer"
"github.com/microsoft/go-sqlcmd/internal/pal"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -56,16 +60,34 @@ func GetConfigFileUsed() string {
return viper.ConfigFileUsed()
}

// validateConfigFileExtension checks if the config file has a supported extension.
// Allows .yaml, .yml, and no extension (for default sqlconfig file).
func validateConfigFileExtension(configFile string) error {
ext := strings.ToLower(filepath.Ext(configFile))
if ext == "" || ext == ".yaml" || ext == ".yml" {
return nil
}
return localizer.Errorf(
"Configuration files must use YAML format (.yaml or .yml extension). "+
"File '%s' has unsupported extension '%s'.",
configFile, ext)
}

// configureViper initializes the Viper library with the given configuration file.
// This function sets the configuration file type to "yaml" and sets the environment variable prefix to "SQLCMD".
// It also sets the configuration file to use to the one provided as an argument to the function.
// This function is intended to be called at the start of the application to configure Viper before any other code uses it.
func configureViper(configFile string) {
func configureViper(configFile string) error {
if configFile == "" {
panic("Must provide configFile")
}

if err := validateConfigFileExtension(configFile); err != nil {
return err
}

viper.SetConfigType("yaml")
viper.SetEnvPrefix("SQLCMD")
viper.SetConfigFile(configFile)
return nil
}
47 changes: 45 additions & 2 deletions internal/config/viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,59 @@
package config

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_configureViper(t *testing.T) {
assert.Panics(t, func() {
configureViper("")
_ = configureViper("")
})
}

func Test_configureViperValidExtensions(t *testing.T) {
tests := []string{"config.yaml", "config.yml", "sqlconfig", "/path/to/config.YAML"}
for _, name := range tests {
t.Run(name, func(t *testing.T) {
err := configureViper(name)
assert.NoError(t, err)
})
}
}

func Test_configureViperInvalidExtension(t *testing.T) {
err := configureViper("config.txt")
assert.Error(t, err)
assert.Contains(t, err.Error(), "YAML format")
assert.Contains(t, err.Error(), ".txt")
}

func Test_validateConfigFileExtension(t *testing.T) {
tests := []struct {
name string
file string
wantErr bool
}{
{"yaml extension", "config.yaml", false},
{"yml extension", "config.yml", false},
{"no extension", "sqlconfig", false},
{"uppercase YAML", "config.YAML", false},
{"txt extension", "config.txt", true},
{"json extension", "config.json", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateConfigFileExtension(tt.file)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}

func Test_Load(t *testing.T) {
SetFileNameForTest(t)
Clean()
Expand Down
Loading