Skip to content

Commit 8579e37

Browse files
committed
Saving changes
1 parent 964fcb8 commit 8579e37

File tree

10 files changed

+98
-52
lines changed

10 files changed

+98
-52
lines changed

cmd/modern/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ func initializeEnvVars() {
9595
os.Setenv("SQLCMDPASSWORD", password)
9696
}
9797
}
98-
9998
}
100-
10199
}
102100

103101
// isFirstArgModernCliSubCommand is TEMPORARY code, to be removed when

cmd/modern/root.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,10 @@ func (c *Root) SubCommands() []cmdparser.Command {
6767
cmdparser.New[*root.Query](dependencies),
6868
cmdparser.New[*root.Start](dependencies),
6969
cmdparser.New[*root.Stop](dependencies),
70+
cmdparser.New[*root.Use](dependencies),
7071
cmdparser.New[*root.Uninstall](dependencies),
7172
}
7273

73-
// If the current context is a container, then add the "use" sub-command, so
74-
// databases can be added to the already existing container.
75-
if config.CurrentContextEndpointHasContainer() {
76-
subCommands = append(subCommands, cmdparser.New[*root.Use](dependencies))
77-
}
78-
7974
// BUG(stuartpa): - Add Linux support
8075
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
8176
subCommands = append(subCommands, cmdparser.New[*root.Open](dependencies))

cmd/modern/root/install/mssql-base.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package install
55

66
import (
77
"fmt"
8-
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/mechanism"
98
"runtime"
109
"strings"
1110

@@ -21,6 +20,7 @@ import (
2120
"github.com/microsoft/go-sqlcmd/internal/sql"
2221
"github.com/microsoft/go-sqlcmd/internal/tools"
2322
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest"
23+
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/mechanism"
2424
"github.com/spf13/viper"
2525
)
2626

@@ -350,15 +350,17 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
350350

351351
// Save the config now, so user can uninstall/delete, even if mssql in the container
352352
// fails to start
353-
config.AddContextWithContainer(
354-
contextName,
355-
imageName,
356-
c.port,
357-
containerId,
358-
userName,
359-
password,
360-
c.passwordEncryption,
361-
)
353+
354+
contextOptions := config.ContextOptions{
355+
ImageName: imageName,
356+
PortNumber: c.port,
357+
ContainerId: containerId,
358+
Username: userName,
359+
Password: password,
360+
PasswordEncryption: c.passwordEncryption,
361+
}
362+
363+
config.AddContextWithContainer(contextName, contextOptions)
362364

363365
output.Infof(
364366
"Created context %q in \"%s\", configuring user account...",
@@ -378,11 +380,11 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
378380
//
379381
// For Unit Testing we use the Docker Hello World container, which
380382
// starts much faster than the SQL Server container!
383+
sqlOptions := sql.SqlOptions{}
381384
if c.errorLogEntryToWaitFor == "Hello from Docker!" {
382-
c.sql = sql.New(sql.SqlOptions{UnitTesting: true})
383-
} else {
384-
c.sql = sql.New(sql.SqlOptions{UnitTesting: false})
385+
sqlOptions.UnitTesting = true
385386
}
387+
c.sql = sql.New(sqlOptions)
386388

387389
saUser := &sqlconfig.User{
388390
AuthenticationType: "basic",

cmd/modern/root/use.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,17 @@ func (c *Use) DefineCommand(...cmdparser.CommandOptions) {
5151
}
5252

5353
func (c *Use) run() {
54-
output := c.Output()
54+
output := useOutput{output: c.Output()}
5555

5656
controller := container.NewController()
5757
id := config.ContainerId()
5858

59+
if !config.CurrentContextEndpointHasContainer() {
60+
output.FatalNoContainerInCurrentContext()
61+
}
62+
5963
if !controller.ContainerRunning(id) {
60-
output.FatalfWithHintExamples([][]string{
61-
{"Start container for current context", "sqlcmd start"},
62-
}, "Container for current context is not running")
64+
output.FatalContainerNotRunning()
6365
}
6466

6567
endpoint, user := config.CurrentContext()
@@ -72,16 +74,14 @@ func (c *Use) run() {
7274
})
7375

7476
if !useDatabase.SourceFileExists() {
75-
output.FatalfWithHints(
76-
[]string{fmt.Sprintf("File does not exist at URL %q", c.url)},
77-
"Unable to download file to container")
77+
output.FatalDatabaseSourceFileNotExist(c.url)
7878
}
7979

8080
// Copy source file (e.g. .bak/.bacpac etc.) for database to be made available to container
8181
useDatabase.CopyToContainer(id)
8282

8383
if useDatabase.IsExtractionNeeded() {
84-
output.Infof("Extracting files from %q", useDatabase.UrlFilename())
84+
output.output.Infof("Extracting files from %q", useDatabase.UrlFilename())
8585
useDatabase.Extract()
8686
}
8787

cmd/modern/root/use_output.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package root
2+
3+
import (
4+
"fmt"
5+
"github.com/microsoft/go-sqlcmd/internal/output"
6+
)
7+
8+
type useOutput struct {
9+
*output.Output
10+
output *output.Output
11+
}
12+
13+
func (u *useOutput) FatalNoContainerInCurrentContext() {
14+
u.output.FatalfWithHintExamples([][]string{
15+
{"Create a context with a container", "sqlcmd create mssql"},
16+
}, "Current context does not have a container")
17+
}
18+
19+
func (u *useOutput) FatalContainerNotRunning() {
20+
u.output.FatalfWithHintExamples([][]string{
21+
{"Start container for current context", "sqlcmd start"},
22+
}, "Container for current context is not running")
23+
}
24+
25+
func (u *useOutput) FatalDatabaseSourceFileNotExist(url string) {
26+
u.output.FatalfWithHints(
27+
[]string{fmt.Sprintf("File does not exist at URL %q", url)},
28+
"Unable to download file to container")
29+
}

internal/cmdparser/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (c *Cmd) AddFlag(options FlagOptions) {
8787
}
8888

8989
if options.Hidden {
90-
c.command.Flags().MarkHidden(options.Name)
90+
c.command.PersistentFlags().MarkHidden(options.Name)
9191
}
9292
}
9393

internal/config/config.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,47 +92,42 @@ func IsEmpty() (isEmpty bool) {
9292
// requested. The updated configuration is saved to file.
9393
func AddContextWithContainer(
9494
contextName string,
95-
imageName string,
96-
portNumber int,
97-
containerId string,
98-
username string,
99-
password string,
100-
passwordEncryption string,
95+
options ContextOptions,
10196
) {
102-
if containerId == "" {
97+
if options.ContainerId == "" {
10398
panic("containerId must be provided")
10499
}
105-
if imageName == "" {
100+
if options.ImageName == "" {
106101
panic("imageName must be provided")
107102
}
108-
if portNumber == 0 {
103+
if options.PortNumber == 0 {
109104
panic("portNumber must be non-zero")
110105
}
111-
if username == "" {
106+
if options.Username == "" {
112107
panic("username must be provided")
113108
}
114-
if password == "" {
109+
if options.Password == "" {
115110
panic("password must be provided")
116111
}
117112
if contextName == "" {
118113
panic("contextName must be provided")
119114
}
120115

121-
contextName = FindUniqueContextName(contextName, username)
116+
contextName = FindUniqueContextName(contextName, options.Username)
122117
endPointName := FindUniqueEndpointName(contextName)
123-
userName := username + "@" + contextName
118+
userName := options.Username + "@" + contextName
124119

125120
config.CurrentContext = contextName
126121

127122
config.Endpoints = append(config.Endpoints, Endpoint{
128123
AssetDetails: &AssetDetails{
129124
ContainerDetails: &ContainerDetails{
130-
Id: containerId,
131-
Image: imageName},
125+
Id: options.ContainerId,
126+
Image: options.ImageName},
132127
},
133128
EndpointDetails: EndpointDetails{
134129
Address: "127.0.0.1",
135-
Port: portNumber,
130+
Port: options.PortNumber,
136131
},
137132
Name: endPointName,
138133
})
@@ -148,9 +143,9 @@ func AddContextWithContainer(
148143
user := User{
149144
AuthenticationType: "basic",
150145
BasicAuth: &BasicAuthDetails{
151-
Username: username,
152-
PasswordEncryption: passwordEncryption,
153-
Password: encryptCallback(password, passwordEncryption),
146+
Username: options.Username,
147+
PasswordEncryption: options.PasswordEncryption,
148+
Password: encryptCallback(options.Password, options.PasswordEncryption),
154149
},
155150
Name: userName,
156151
}

internal/config/config_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,16 @@ func TestConfig(t *testing.T) {
131131
ContainerId()
132132
RemoveCurrentContext()
133133
RemoveCurrentContext()
134-
AddContextWithContainer("context", "imageName", 1433, "containerId", "user", "password", "none")
134+
135+
options := ContextOptions{
136+
ImageName: "imageName",
137+
PortNumber: 1433,
138+
ContainerId: "containerId",
139+
Username: "user",
140+
Password: "password",
141+
PasswordEncryption: "none",
142+
}
143+
AddContextWithContainer("context", options)
135144
RemoveCurrentContext()
136145
DeleteEndpoint("endpoint")
137146
DeleteContext("context")
@@ -324,7 +333,15 @@ func TestAddContextWithContainerPanic(t *testing.T) {
324333
for _, tt := range tests {
325334
t.Run(tt.name, func(t *testing.T) {
326335
assert.Panics(t, func() {
327-
AddContextWithContainer(tt.args.contextName, tt.args.imageName, tt.args.portNumber, tt.args.containerId, tt.args.username, tt.args.password, tt.args.passwordEncryption)
336+
options := ContextOptions{
337+
ImageName: tt.args.imageName,
338+
PortNumber: tt.args.portNumber,
339+
ContainerId: tt.args.containerId,
340+
Username: tt.args.username,
341+
Password: tt.args.password,
342+
PasswordEncryption: tt.args.passwordEncryption,
343+
}
344+
AddContextWithContainer(tt.args.contextName, options)
328345
})
329346
})
330347
}

internal/config/endpoint-container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func CurrentContextEndpointHasContainer() (exists bool) {
4343
currentContextName := config.CurrentContext
4444

4545
if currentContextName == "" {
46-
panic("currentContextName must not be empty")
46+
return false
4747
}
4848

4949
for _, c := range config.Contexts {

internal/config/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package config
2+
3+
type ContextOptions struct {
4+
ImageName string
5+
PortNumber int
6+
ContainerId string
7+
Username string
8+
Password string
9+
PasswordEncryption string
10+
}

0 commit comments

Comments
 (0)