Skip to content

Commit e9582ca

Browse files
Stuart PadleyStuart Padley
authored andcommitted
Fixed bug with setting current context correctly, if a context with the same name is added again
1 parent edfbcad commit e9582ca

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

cmd/modern/root/config/add-context.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package config
55

66
import (
77
"fmt"
8+
89
"github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
910
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
1011
"github.com/microsoft/go-sqlcmd/internal/config"
@@ -82,7 +83,7 @@ func (c *AddContext) run() {
8283
}
8384
}
8485

85-
config.AddContext(context)
86+
context.Name = config.AddContext(context)
8687
config.SetCurrentContextName(context.Name)
8788
output.InfofWithHintExamples([][]string{
8889
{"Open in Azure Data Studio", "sqlcmd open ads"},

internal/config/context.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ package config
66
import (
77
"errors"
88
"fmt"
9-
. "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
109
"strconv"
10+
11+
. "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
1112
)
1213

13-
// AddContext adds the context to the sqlconfig file.
14+
// AddContext adds the context to the sqlconfig file, and returns the context
15+
// name, which maybe uniquified, if the passed in name already exists.
1416
//
1517
// Before calling this method, verify the Endpoint exists and give the user
1618
// a descriptive error, (this function will panic, which should never be hit)
17-
func AddContext(context Context) {
19+
func AddContext(context Context) string {
1820
if !EndpointExists(context.Endpoint) {
1921
panic("Endpoint doesn't exist")
2022
}
@@ -25,6 +27,8 @@ func AddContext(context Context) {
2527
context.Name = FindUniqueContextName(context.Name, username)
2628
config.Contexts = append(config.Contexts, context)
2729
Save()
30+
31+
return context.Name
2832
}
2933

3034
// CurrentContextName returns the name of the current context in the configuration.

0 commit comments

Comments
 (0)