From 77dcb831ffa323d45c4092351b8cf44adf917220 Mon Sep 17 00:00:00 2001 From: davidshi Date: Mon, 3 Jun 2024 09:09:19 -0500 Subject: [PATCH] resolve vars in error and out commands --- pkg/sqlcmd/commands.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/sqlcmd/commands.go b/pkg/sqlcmd/commands.go index 6a258e49..acbfa3d2 100644 --- a/pkg/sqlcmd/commands.go +++ b/pkg/sqlcmd/commands.go @@ -270,7 +270,11 @@ func outCommand(s *Sqlcmd, args []string, line uint) error { case strings.EqualFold(args[0], "stderr"): s.SetOutput(os.Stderr) default: - o, err := os.OpenFile(args[0], os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o644) + filePath, err := resolveArgumentVariables(s, []rune(args[0]), true) + if err != nil { + return err + } + o, err := os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o644) if err != nil { return InvalidFileError(err, args[0]) } @@ -298,7 +302,11 @@ func errorCommand(s *Sqlcmd, args []string, line uint) error { case strings.EqualFold(args[0], "stdout"): s.SetError(os.Stdout) default: - o, err := os.OpenFile(args[0], os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o644) + filePath, err := resolveArgumentVariables(s, []rune(args[0]), true) + if err != nil { + return err + } + o, err := os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o644) if err != nil { return InvalidFileError(err, args[0]) } @@ -549,7 +557,7 @@ func xmlCommand(s *Sqlcmd, args []string, line uint) error { func resolveArgumentVariables(s *Sqlcmd, arg []rune, failOnUnresolved bool) (string, error) { var b *strings.Builder end := len(arg) - for i := 0; i < end; { + for i := 0; i < end && !s.Connect.DisableVariableSubstitution; { c, next := arg[i], grab(arg, i+1, end) switch { case c == '$' && next == '(':