Skip to content

Commit 3b688f7

Browse files
committed
Cope with no HOME env var
1 parent 23113f7 commit 3b688f7

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

cmd/modern/main.go

+21
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import (
1616
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
1717
"github.com/microsoft/go-sqlcmd/internal/cmdparser/dependency"
1818
"github.com/microsoft/go-sqlcmd/internal/config"
19+
"github.com/microsoft/go-sqlcmd/internal/io/file"
1920
"github.com/microsoft/go-sqlcmd/internal/output"
2021
"github.com/microsoft/go-sqlcmd/internal/output/verbosity"
2122
"github.com/microsoft/go-sqlcmd/pkg/sqlcmd"
2223
"github.com/spf13/cobra"
24+
"path"
2325

2426
"os"
2527

@@ -56,6 +58,25 @@ func main() {
5658
// than env variables and env variables take higher precedence over config
5759
// file info.
5860
func initializeEnvVars() {
61+
home, err := os.UserHomeDir()
62+
63+
// Special case, some shells don't have any home dir env var set, see:
64+
// https://github.com/microsoft/go-sqlcmd/issues/279
65+
// in this case, we early exit here and don't initialize anything, there is nothing
66+
// else we can do
67+
if err != nil {
68+
return
69+
}
70+
71+
// The only place we can check for the existence of the sqlconfig file is in the
72+
// default location, because this code path is only used for the legacy kong CLI,
73+
// if the sqlconfig file doesn't exist at the default location we just return so
74+
// because the initializeCallback() function below will create the sqlconfig file,
75+
// which legacy sqlcmd users might not want.
76+
if !file.Exists(path.Join(home, ".sqlcmd", "sqlconfig")) {
77+
return
78+
}
79+
5980
initializeCallback()
6081
if config.CurrentContextName() != "" {
6182
server, username, password := config.GetCurrentContextInfo()

internal/config/config.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package config
66
import (
77
. "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
88
"github.com/microsoft/go-sqlcmd/internal/io/file"
9+
"github.com/microsoft/go-sqlcmd/internal/io/folder"
910
"github.com/microsoft/go-sqlcmd/internal/pal"
1011
"os"
1112
"path/filepath"
@@ -40,7 +41,14 @@ func SetFileNameForTest(t *testing.T) {
4041
// the user's home directory, the function will return an empty string.
4142
func DefaultFileName() (filename string) {
4243
home, err := os.UserHomeDir()
43-
checkErr(err)
44+
if err != nil {
45+
trace(
46+
"Error getting user's home directory: %v, will use current directory %q as default",
47+
err,
48+
folder.Getwd(),
49+
)
50+
home = "."
51+
}
4452
filename = filepath.Join(home, ".sqlcmd", "sqlconfig")
4553

4654
return

0 commit comments

Comments
 (0)