Skip to content

Redirect sqlcmd errors to err stream #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 6, 2022
Merged
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
5 changes: 3 additions & 2 deletions cmd/sqlcmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -259,7 +260,7 @@ func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) {
if args.ErrorsToStderr >= 0 {
s.PrintError = func(msg string, severity uint8) bool {
if severity >= stderrSeverity {
_, _ = os.Stderr.Write([]byte(msg + sqlcmd.SqlcmdEol))
s.WriteError(os.Stderr, errors.New(msg+sqlcmd.SqlcmdEol))
return true
}
return false
Expand All @@ -285,7 +286,7 @@ func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) {
} else {
for f := range args.InputFile {
if err = s.IncludeFile(args.InputFile[f], true); err != nil {
_, _ = os.Stderr.Write([]byte(err.Error() + sqlcmd.SqlcmdEol))
s.WriteError(s.GetError(), err)
s.Exitcode = 1
break
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/sqlcmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ func (c Commands) matchCommand(line string) (*Command, []string) {
}

func warnDisabled(s *Sqlcmd, args []string, line uint) error {
_, _ = s.GetError().Write([]byte(ErrCommandsDisabled.Error() + SqlcmdEol))
s.WriteError(s.GetError(), ErrCommandsDisabled)
return nil
}

func errorDisabled(s *Sqlcmd, args []string, line uint) error {
_, _ = s.GetError().Write([]byte(ErrCommandsDisabled.Error() + SqlcmdEol))
s.WriteError(s.GetError(), ErrCommandsDisabled)
s.Exitcode = 1
return ErrExitRequested
}
Expand Down Expand Up @@ -433,7 +433,7 @@ func resolveArgumentVariables(s *Sqlcmd, arg []rune, failOnUnresolved bool) (str
if failOnUnresolved {
return "", UndefinedVariable(varName)
}
_, _ = s.GetError().Write([]byte(UndefinedVariable(varName).Error() + SqlcmdEol))
s.WriteError(s.GetError(), UndefinedVariable(varName))
if b != nil {
b.WriteString(string(arg[i : vl+1]))
}
Expand Down
19 changes: 16 additions & 3 deletions pkg/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (s *Sqlcmd) Run(once bool, processAll bool) error {
args = make([]string, 0)
once = true
} else {
_, _ = s.GetOutput().Write([]byte(err.Error() + SqlcmdEol))
s.WriteError(s.GetOutput(), err)
}
}
if cmd != nil {
Expand All @@ -146,7 +146,7 @@ func (s *Sqlcmd) Run(once bool, processAll bool) error {
break
}
if err != nil {
_, _ = s.GetOutput().Write([]byte(err.Error() + SqlcmdEol))
s.WriteError(s.GetOutput(), err)
lastError = err
}
}
Expand Down Expand Up @@ -209,6 +209,19 @@ func (s *Sqlcmd) SetError(e io.WriteCloser) {
s.err = e
}

// WriteError writes the error on specified stream
func (s *Sqlcmd) WriteError(stream io.Writer, err error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the consolidation into this function

if strings.HasPrefix(err.Error(), ErrorPrefix) {
if s.GetError() != os.Stdout {
_, _ = s.GetError().Write([]byte(err.Error() + SqlcmdEol))
} else {
_, _ = os.Stderr.Write([]byte(err.Error() + SqlcmdEol))
}
} else {
_, _ = stream.Write([]byte(err.Error() + SqlcmdEol))
}
}

// ConnectDb opens a connection to the database with the given modifications to the connection
// nopw == true means don't prompt for a password if the auth type requires it
// if connect is nil, ConnectDb uses the current connection. If non-nil and the connection succeeds,
Expand Down Expand Up @@ -364,7 +377,7 @@ func setupCloseHandler(s *Sqlcmd) {
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
_, _ = s.GetOutput().Write([]byte(ErrCtrlC.Error() + SqlcmdEol))
s.WriteError(s.GetOutput(), ErrCtrlC)
os.Exit(0)
}()
}
Expand Down
60 changes: 60 additions & 0 deletions pkg/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,44 @@ func TestQueryServerPropertyReturnsColumnName(t *testing.T) {
}
}

func TestSqlCmdOutputAndError(t *testing.T) {
s, outfile, errfile := setupSqlcmdWithFileErrorOutput(t)
defer os.Remove(outfile.Name())
defer os.Remove(errfile.Name())
s.Query = "select $(X"
err := s.Run(true, false)
if assert.NoError(t, err, "s.Run(once = true)") {
bytes, err := os.ReadFile(errfile.Name())
if assert.NoError(t, err, "os.ReadFile") {
assert.Equal(t, "Sqlcmd: Error: Syntax error at line 1."+SqlcmdEol, string(bytes), "Expected syntax error not received for query execution")
}
}
s.Query = "select '1'"
err = s.Run(true, false)
if assert.NoError(t, err, "s.Run(once = true)") {
bytes, err := os.ReadFile(outfile.Name())
if assert.NoError(t, err, "os.ReadFile") {
assert.Equal(t, "1"+SqlcmdEol+SqlcmdEol+"(1 row affected)"+SqlcmdEol, string(bytes), "Unexpected output for query execution")
}
}

s, outfile, errfile = setupSqlcmdWithFileErrorOutput(t)
defer os.Remove(outfile.Name())
defer os.Remove(errfile.Name())
dataPath := "testdata" + string(os.PathSeparator)
err = s.IncludeFile(dataPath+"testerrorredirection.sql", false)
if assert.NoError(t, err, "IncludeFile testerrorredirection.sql false") {
bytes, err := os.ReadFile(outfile.Name())
if assert.NoError(t, err, "os.ReadFile outfile") {
assert.Equal(t, "1"+SqlcmdEol+SqlcmdEol+"(1 row affected)"+SqlcmdEol, string(bytes), "Unexpected output for sql file execution in outfile")
}
bytes, err = os.ReadFile(errfile.Name())
if assert.NoError(t, err, "os.ReadFile errfile") {
assert.Equal(t, "Sqlcmd: Error: Syntax error at line 3."+SqlcmdEol, string(bytes), "Expected syntax error not found in errfile")
}
}
}

// runSqlCmd uses lines as input for sqlcmd instead of relying on file or console input
func runSqlCmd(t testing.TB, s *Sqlcmd, lines []string) error {
t.Helper()
Expand Down Expand Up @@ -509,6 +547,28 @@ func setupSqlcmdWithFileOutput(t testing.TB) (*Sqlcmd, *os.File) {
return s, file
}

func setupSqlcmdWithFileErrorOutput(t testing.TB) (*Sqlcmd, *os.File, *os.File) {
t.Helper()
v := InitializeVariables(true)
v.Set(SQLCMDMAXVARTYPEWIDTH, "0")
s := New(nil, "", v)
s.Connect = newConnect(t)
s.Format = NewSQLCmdDefaultFormatter(true)
outfile, err := os.CreateTemp("", "sqlcmdout")
assert.NoError(t, err, "os.CreateTemp")
errfile, err := os.CreateTemp("", "sqlcmderr")
assert.NoError(t, err, "os.CreateTemp")
s.SetOutput(outfile)
s.SetError(errfile)
err = s.ConnectDb(nil, true)
if err != nil {
os.Remove(outfile.Name())
os.Remove(errfile.Name())
}
assert.NoError(t, err, "s.ConnectDB")
return s, outfile, errfile
}

// Assuming public Azure, use AAD when SQLCMDUSER environment variable is not set
func canTestAzureAuth() bool {
server := os.Getenv(SQLCMDSERVER)
Expand Down
4 changes: 4 additions & 0 deletions pkg/sqlcmd/testdata/testerrorredirection.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select '1'
go
select $(var
go