* Builds the beginning of a sqlcmd command populated with the connection settings. * @param connectionConfig The connection settings to be used for this sqlcmd call. * @param database The database to connect to. If not specified, defaults to the database in the connection settings. *
(connectionConfig: SqlConnectionConfig, database?: string)
| 105 | * @returns A partial sqlcmd command with connection and authentication settings. |
| 106 | */ |
| 107 | public static buildSqlCmdCallWithConnectionInfo(connectionConfig: SqlConnectionConfig, database?: string): string { |
| 108 | // sqlcmd should be added to PATH already, we just need to see if need to add ".exe" for Windows |
| 109 | let sqlCmdPath: string; |
| 110 | switch (process.platform) { |
| 111 | case "win32": |
| 112 | sqlCmdPath = "sqlcmd.exe"; |
| 113 | break; |
| 114 | case "linux": |
| 115 | case "darwin": |
| 116 | sqlCmdPath = "sqlcmd"; |
| 117 | break; |
| 118 | default: |
| 119 | throw new Error(`Platform ${process.platform} is not supported.`); |
| 120 | } |
| 121 | |
| 122 | if (!database) { |
| 123 | database = connectionConfig.Database; |
| 124 | } |
| 125 | |
| 126 | let sqlcmdCall = `"${sqlCmdPath}" -S ${connectionConfig.Server},${connectionConfig.Port ?? 1433} -d ${database}`; |
| 127 | |
| 128 | // Determine the correct sqlcmd arguments based on the auth type |
| 129 | switch (connectionConfig.FormattedAuthentication) { |
| 130 | case undefined: |
| 131 | case 'sqlpassword': |
| 132 | // No authentication type defaults SQL login |
| 133 | sqlcmdCall += ` -U "${connectionConfig.UserId}"`; |
| 134 | core.exportVariable(Constants.sqlcmdPasswordEnvVarName, connectionConfig.Password); |
| 135 | break; |
| 136 | |
| 137 | case 'activedirectorydefault': |
| 138 | sqlcmdCall += ` --authentication-method=ActiveDirectoryDefault`; |
| 139 | break; |
| 140 | |
| 141 | case 'activedirectorypassword': |
| 142 | sqlcmdCall += ` --authentication-method=ActiveDirectoryPassword -U "${connectionConfig.UserId}"`; |
| 143 | core.exportVariable(Constants.sqlcmdPasswordEnvVarName, connectionConfig.Password); |
| 144 | break; |
| 145 | |
| 146 | case 'activedirectoryserviceprincipal': |
| 147 | sqlcmdCall += ` --authentication-method=ActiveDirectoryServicePrincipal -U "${connectionConfig.UserId}"`; |
| 148 | core.exportVariable(Constants.sqlcmdPasswordEnvVarName, connectionConfig.Password); |
| 149 | break; |
| 150 | |
| 151 | default: |
| 152 | throw new Error(`Authentication type ${connectionConfig.FormattedAuthentication} is not supported.`); |
| 153 | } |
| 154 | |
| 155 | return sqlcmdCall; |
| 156 | } |
| 157 | |
| 158 | } |
no outgoing calls
no test coverage detected