|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +// Copyright (c) Microsoft Corporation. |
| 4 | +// Licensed under the MIT license. |
| 5 | + |
| 6 | +package open |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig" |
| 13 | + "github.com/microsoft/go-sqlcmd/internal/cmdparser" |
| 14 | + "github.com/microsoft/go-sqlcmd/internal/config" |
| 15 | + "github.com/microsoft/go-sqlcmd/internal/container" |
| 16 | + "github.com/microsoft/go-sqlcmd/internal/localizer" |
| 17 | + "github.com/microsoft/go-sqlcmd/internal/tools" |
| 18 | +) |
| 19 | + |
| 20 | +// Ssms implements the `sqlcmd open ssms` command. It opens |
| 21 | +// SQL Server Management Studio and connects to the current context using the |
| 22 | +// credentials specified in the context. |
| 23 | +func (c *Ssms) DefineCommand(...cmdparser.CommandOptions) { |
| 24 | + options := cmdparser.CommandOptions{ |
| 25 | + Use: "ssms", |
| 26 | + Short: localizer.Sprintf("Open SQL Server Management Studio and connect to current context"), |
| 27 | + Examples: []cmdparser.ExampleOptions{{ |
| 28 | + Description: localizer.Sprintf("Open SSMS and connect using the current context"), |
| 29 | + Steps: []string{"sqlcmd open ssms"}}}, |
| 30 | + Run: c.run, |
| 31 | + } |
| 32 | + |
| 33 | + c.Cmd.DefineCommand(options) |
| 34 | +} |
| 35 | + |
| 36 | +// Launch SSMS and connect to the current context |
| 37 | +func (c *Ssms) run() { |
| 38 | + endpoint, user := config.CurrentContext() |
| 39 | + |
| 40 | + // Check if this is a local container connection |
| 41 | + isLocalConnection := isLocalEndpoint(endpoint) |
| 42 | + |
| 43 | + // If the context has a local container, ensure it is running, otherwise bail out |
| 44 | + if asset := endpoint.AssetDetails; asset != nil && asset.ContainerDetails != nil { |
| 45 | + c.ensureContainerIsRunning(asset.Id) |
| 46 | + } |
| 47 | + |
| 48 | + // Launch SSMS with connection parameters |
| 49 | + c.launchSsms(endpoint.Address, endpoint.Port, user, isLocalConnection) |
| 50 | +} |
| 51 | + |
| 52 | +func (c *Ssms) ensureContainerIsRunning(containerID string) { |
| 53 | + output := c.Output() |
| 54 | + controller := container.NewController() |
| 55 | + if !controller.ContainerRunning(containerID) { |
| 56 | + output.FatalWithHintExamples([][]string{ |
| 57 | + {localizer.Sprintf("To start the container"), localizer.Sprintf("sqlcmd start")}, |
| 58 | + }, localizer.Sprintf("Container is not running")) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// launchSsms launches SQL Server Management Studio using the specified server and user credentials. |
| 63 | +func (c *Ssms) launchSsms(host string, port int, user *sqlconfig.User, isLocalConnection bool) { |
| 64 | + output := c.Output() |
| 65 | + |
| 66 | + // Build server connection string |
| 67 | + serverArg := fmt.Sprintf("%s,%d", host, port) |
| 68 | + |
| 69 | + args := []string{ |
| 70 | + "-S", serverArg, |
| 71 | + "-nosplash", |
| 72 | + } |
| 73 | + |
| 74 | + // Only add -C (trust server certificate) for local connections with self-signed certs |
| 75 | + if isLocalConnection { |
| 76 | + args = append(args, "-C") |
| 77 | + } |
| 78 | + |
| 79 | + // Use SQL authentication if configured (commonly used for SQL Server containers) |
| 80 | + if user != nil && user.AuthenticationType == "basic" && user.BasicAuth != nil { |
| 81 | + // Escape double quotes in username (SQL Server allows " in login names) |
| 82 | + username := strings.ReplaceAll(user.BasicAuth.Username, `"`, `\"`) |
| 83 | + args = append(args, "-U", username) |
| 84 | + // Note: -P parameter was removed in SSMS 18+ for security reasons |
| 85 | + // Copy password to clipboard so user can paste it in the login dialog |
| 86 | + copyPasswordToClipboard(user, output) |
| 87 | + } |
| 88 | + |
| 89 | + tool := tools.NewTool("ssms") |
| 90 | + if !tool.IsInstalled() { |
| 91 | + output.Fatal(tool.HowToInstall()) |
| 92 | + } |
| 93 | + |
| 94 | + c.displayPreLaunchInfo() |
| 95 | + |
| 96 | + _, err := tool.Run(args) |
| 97 | + c.CheckErr(err) |
| 98 | +} |
0 commit comments