Skip to content
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

exit on state 127 #88

Merged
merged 3 commits into from
May 23, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/sqlcmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) {
var line sqlcmd.Console = nil
if iactive {
line = console.NewConsole("")
defer line.Close()
}

s := sqlcmd.New(line, wd, vars)
Expand Down
1 change: 1 addition & 0 deletions cmd/sqlcmd/testdata/select,100.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 100
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module github.com/microsoft/go-sqlcmd
go 1.16

require (
github.com/alecthomas/kong v0.5.0
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188
github.com/alecthomas/kong v0.5.1-0.20220516223738-0aaa4c11997b
github.com/golang-sql/sqlexp v0.1.0
github.com/google/uuid v1.3.0
github.com/microsoft/go-mssqldb v0.13.2
github.com/microsoft/go-mssqldb v0.14.0
github.com/peterh/liner v1.2.2
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.7.1
golang.org/x/text v0.3.6
)
7 changes: 6 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0 h1:v9p9TfTbf7AwNb5NYQt7hI4
github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0/go.mod h1:yqy467j36fJxcRV2TzfVZ1pCb5vxm4BtZPUdYWe/Xo8=
github.com/alecthomas/kong v0.5.0 h1:u8Kdw+eeml93qtMZ04iei0CFYve/WPcA5IFh+9wSskE=
github.com/alecthomas/kong v0.5.0/go.mod h1:uzxf/HUh0tj43x1AyJROl3JT7SgsZ5m+icOv1csRhc0=
github.com/alecthomas/kong v0.5.1-0.20220516223738-0aaa4c11997b h1:QF7Hdi3ReQRAST66vU7bqkHODmcVJIUZyTGo9gLHluk=
github.com/alecthomas/kong v0.5.1-0.20220516223738-0aaa4c11997b/go.mod h1:GaAkr/DV/nSKftP7snQLewFh9pZqrm+OEn3HqkvWU7c=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -14,14 +16,17 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 h1:+eHOFJl1BaXrQxKX+T06f78590z4qA2ZzBTqahsKSE4=
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188/go.mod h1:vXjM/+wXQnTPR4KqTKDgJukSZ6amVRtWMPEjE6sQoK8=
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/microsoft/go-mssqldb v0.13.2 h1:PeUn6Fu7okbaPpcv5QOk+9h7PcetOuB5ff6tVwz5V24=
github.com/microsoft/go-mssqldb v0.13.2/go.mod h1:JWsC4ToRBruAAoknz9wV9Iiqb/K85J4zabR6PnBNbro=
github.com/microsoft/go-mssqldb v0.14.0 h1:eo0rUzFOIImkrJi50KGpXKJe6WbrqzDqmG/yY1+yCHk=
github.com/microsoft/go-mssqldb v0.14.0/go.mod h1:ti85dZ/KY16b1MsdsSm+P+04lz83uPDNdc7mBvKgwU8=
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
github.com/peterh/liner v1.2.2 h1:aJ4AOodmL+JxOZZEL2u9iJf8omNRpqHc/EbrK+3mAXw=
github.com/peterh/liner v1.2.2/go.mod h1:xFwJyiKIXJZUKItq5dGHZSTBRAuG/CpeNpWLyiNRNwI=
Expand Down
19 changes: 17 additions & 2 deletions pkg/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Console interface {
ReadPassword(prompt string) ([]byte, error)
// SetPrompt sets the prompt text shown to input the next line
SetPrompt(s string)
// Close clears any buffers and closes open file handles
Close()
shueybubbles marked this conversation as resolved.
Show resolved Hide resolved
}

// Sqlcmd is the core processor for text lines.
Expand Down Expand Up @@ -389,8 +391,12 @@ func (s *Sqlcmd) runQuery(query string) (int, error) {
msg := retmsg.Message(ctx)
switch m := msg.(type) {
case sqlexp.MsgNotice:
if !s.PrintError(m.Message, 10) {
s.Format.AddMessage(m.Message)
if !s.PrintError(m.Message.String(), 10) {
s.Format.AddMessage(m.Message.String())
switch e := m.Message.(type) {
case mssql.Error:
qe = s.handleError(&retcode, e)
}
}
case sqlexp.MsgError:
switch e := m.Error.(type) {
Expand Down Expand Up @@ -465,11 +471,20 @@ func (s *Sqlcmd) handleError(retcode *int, err error) error {
minSeverityToExit = s.Connect.ErrorSeverityLevel
}
var errSeverity uint8
var errState uint8
var errNumber int32
switch sqlError := err.(type) {
case mssql.Error:
errSeverity = sqlError.Class
errState = sqlError.State
errNumber = sqlError.Number
}

// 127 is the magic exit code
if errState == 127 {
*retcode = int(errNumber)
return ErrExitRequested
}
if s.Connect.ErrorSeverityLevel > 0 {
if errSeverity >= minSeverityToExit {
*retcode = int(errSeverity)
Expand Down
7 changes: 7 additions & 0 deletions pkg/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ func TestExitCodeSetOnError(t *testing.T) {
retcode, err = s.runQuery("RAISERROR (N'Testing!' , 5, 1)")
assert.NoError(t, err, "ExitOnError and ErrorSeverityLevel = 0, Raiserror below 10")
assert.Equal(t, -101, retcode, "ExitOnError and ErrorSeverityLevel = 0, Raiserror below 10")
retcode, err = s.runQuery("RAISERROR (15001, 10, 127)")
assert.ErrorIs(t, err, ErrExitRequested, "RAISERROR with state 127")
assert.Equal(t, 15001, retcode, "RAISERROR (15001, 10, 127)")
}

func TestSqlCmdExitOnError(t *testing.T) {
Expand Down Expand Up @@ -290,6 +293,10 @@ func (tc *testConsole) SetPrompt(s string) {
tc.PromptText = s
}

func (tc *testConsole) Close() {

}

func TestPromptForPasswordNegative(t *testing.T) {
prompted := false
console := &testConsole{
Expand Down