microsoft / go-sqlcmd

The new sqlcmd, CLI for SQL Server and Azure SQL (winget install sqlcmd / sqlcmd create mssql / sqlcmd open ads)
https://learn.microsoft.com/sql/tools/sqlcmd/go-sqlcmd-utility
MIT License
337 stars 59 forks source link

Establish conn to curret-context if no args given #261

Closed apoorvdeshmukh closed 1 year ago

apoorvdeshmukh commented 1 year ago

sqlcmd when invoked with no arguments, executes in backward compat mode and ignore current-context info for establishing connection. This commit changes the default behavior to establish connection to the endpoint associated with current-context if valid user details exits in sqlcmd config file. This ensures that users can connect to sql server instance created by sqlcmd if no arguments are specified while invoking sqlcmd.

\git\go-sqlcmd>.\sqlcmd.exe
1> select @@servername
2> go

--------------------------------------------------------------------------------------------------------------------------------
LAPTOP-VPBI3ASB

(1 row affected)
1> exit

git\go-sqlcmd>.\sqlcmd.exe create mssql --accept-eula
Downloading mcr.microsoft.com/mssql/server:latest
Starting mcr.microsoft.com/mssql/server:latest
Created context "mssql" in "C:\Users\apdeshmukh\.sqlcmd\sqlconfig", configuring user account...
Disabled "sa" account (and rotated "sa" password). Creating user "apdeshmukh"
Now ready for client connections on port 1435

HINT:
  1. Open in Azure Data Studio: sqlcmd open ads
  2. Run a query:               sqlcmd query "SELECT @@version"
  3. Start interactive session: sqlcmd query
  4. View sqlcmd configuration: sqlcmd config view
  5. See connection strings:    sqlcmd config connection-strings
  6. Remove:                    sqlcmd delete

\git\go-sqlcmd>.\sqlcmd.exe
1> select @@servername
2> go

--------------------------------------------------------------------------------------------------------------------------------
4ee585f27077

(1 row affected)
1>
stuartpa commented 1 year ago
  legacyCmd.Execute(version)

Move this call to the else statement below (right now we only need this envvars when running in legacy mode)


In reply to: 1447954442


In reply to: 1447954442


Refers to: cmd/modern/main.go:49 in 2f1a322. [](commit_id = 2f1a322c68cd1c6f7c0a6910b4635bca4134ad7b, deletion_comment = False)

apoorvdeshmukh commented 1 year ago

Changes as discussed earlier

Incorporated the changes which we discussed earlier. Now we are setting only those env variables for which info is available in context such as endpoint and skipping others such as user details if not available (in case context is to be used for trusted authentication)

apoorvdeshmukh commented 1 year ago
  legacyCmd.Execute(version)

Move this call to the else statement below (right now we only need this envvars when running in legacy mode)

Refers to: cmd/modern/main.go:49 in 2f1a322. [](commit_id = 2f1a322, deletion_comment = False)

Fixed