Skip to content

Commit 15e95b5

Browse files
committed
Add use command, with local/remote files and attach support
1 parent 99a9267 commit 15e95b5

File tree

20 files changed

+25
-465
lines changed

20 files changed

+25
-465
lines changed

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

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@ import (
88
"runtime"
99
"strings"
1010

11-
"github.com/microsoft/go-sqlcmd/cmd/modern/root/open"
1211
"github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
1312
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
14-
"github.com/microsoft/go-sqlcmd/internal/cmdparser/dependency"
1513
"github.com/microsoft/go-sqlcmd/internal/config"
1614
"github.com/microsoft/go-sqlcmd/internal/container"
1715
"github.com/microsoft/go-sqlcmd/internal/localizer"
1816
"github.com/microsoft/go-sqlcmd/internal/output"
1917
"github.com/microsoft/go-sqlcmd/internal/pal"
2018
"github.com/microsoft/go-sqlcmd/internal/secret"
2119
"github.com/microsoft/go-sqlcmd/internal/sql"
22-
"github.com/microsoft/go-sqlcmd/internal/tools"
2320
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest"
2421
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/mechanism"
2522
"github.com/spf13/viper"
@@ -246,20 +243,6 @@ func (c *MssqlBase) AddFlags(
246243
Name: "use-mechanism",
247244
Usage: localizer.Sprintf("Mechanism to use to bring database online (%s)", strings.Join(mechanism.Mechanisms(), ",")),
248245
})
249-
250-
addFlag(cmdparser.FlagOptions{
251-
String: &c.openTool,
252-
DefaultString: "",
253-
Name: "open",
254-
Usage: localizer.Sprintf("Open tool e.g. ads"),
255-
})
256-
257-
addFlag(cmdparser.FlagOptions{
258-
String: &c.openFile,
259-
DefaultString: "",
260-
Name: "open-file",
261-
Usage: localizer.Sprintf("Open file in tool e.g. https://aks.ms/adventureworks-demo.sql"),
262-
})
263246
}
264247

265248
// Run checks that the end-user license agreement has been accepted,
@@ -401,11 +384,6 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
401384
}
402385
useDatabase.CopyToContainer(containerId)
403386

404-
if useDatabase.IsExtractionNeeded() {
405-
output.Infof("Extracting files from %q archive", useDatabase.UrlFilename())
406-
useDatabase.Extract()
407-
}
408-
409387
output.Infof("Bringing database %q online", useDatabase.DatabaseName())
410388
useDatabase.BringOnline(c.sql.Query, contextOptions.Username, contextOptions.Password)
411389
}
@@ -440,41 +418,6 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
440418
c.port),
441419
)
442420
}
443-
444-
if c.openTool == "ads" {
445-
ads := open.Ads{}
446-
ads.SetCrossCuttingConcerns(dependency.Options{
447-
EndOfLine: pal.LineBreak(),
448-
Output: c.Output(),
449-
})
450-
451-
user := &sqlconfig.User{
452-
AuthenticationType: "basic",
453-
BasicAuth: &sqlconfig.BasicAuthDetails{
454-
Username: contextOptions.Username,
455-
PasswordEncryption: c.passwordEncryption,
456-
Password: secret.Encode(contextOptions.Password, c.passwordEncryption)},
457-
Name: contextOptions.Username}
458-
459-
ads.PersistCredentialForAds(endpoint.EndpointDetails.Address, endpoint, user)
460-
461-
output := c.Output()
462-
args := []string{"-r", fmt.Sprintf("--server=%s", fmt.Sprintf(
463-
"%s,%d",
464-
"127.0.0.1",
465-
c.port))}
466-
467-
args = append(args, fmt.Sprintf("--user=%s",
468-
strings.Replace(contextOptions.Username, `"`, `\"`, -1)))
469-
470-
tool := tools.NewTool("ads")
471-
if !tool.IsInstalled() {
472-
output.Fatalf(tool.HowToInstall())
473-
}
474-
475-
_, err := tool.Run(args)
476-
c.CheckErr(err)
477-
}
478421
}
479422

480423
func (c *MssqlBase) verifyUseSourceFileExists(

cmd/modern/root/open/ads.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (c *Ads) run() {
4646
// If basic auth is used, we need to persist the password in the OS in a way
4747
// that ADS can access it. The method used is OS specific.
4848
if user != nil && user.AuthenticationType == "basic" {
49-
c.PersistCredentialForAds(endpoint.EndpointDetails.Address, endpoint, user)
49+
c.persistCredentialForAds(endpoint.EndpointDetails.Address, endpoint, user)
5050
c.launchAds(endpoint.EndpointDetails.Address, endpoint.EndpointDetails.Port, user.BasicAuth.Username)
5151
} else {
5252
c.launchAds(endpoint.EndpointDetails.Address, endpoint.EndpointDetails.Port, "")
@@ -75,7 +75,6 @@ func (c *Ads) launchAds(host string, port int, username string) {
7575
port)),
7676
}
7777

78-
// If a username is specified, use that (basic auth), otherwise use integrated auth
7978
if username != "" {
8079

8180
// Here's a fun SQL Server behavior - it allows you to create database

cmd/modern/root/open/ads_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ func (c *Ads) displayPreLaunchInfo() {
2828
output.Infof("Press Ctrl+C to exit this process...")
2929
}
3030

31-
// PersistCredentialForAds stores a SQL password in the Windows Credential Manager
31+
// persistCredentialForAds stores a SQL password in the Windows Credential Manager
3232
// for the given hostname and endpoint.
33-
func (c *Ads) PersistCredentialForAds(
33+
func (c *Ads) persistCredentialForAds(
3434
hostname string,
3535
endpoint sqlconfig.Endpoint,
3636
user *sqlconfig.User,

cmd/modern/root/open/ads_windows_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestPersistCredentialForAds(t *testing.T) {
2727
PasswordEncryption: "none",
2828
},
2929
}
30-
ads.PersistCredentialForAds("localhost", sqlconfig.Endpoint{
30+
ads.persistCredentialForAds("localhost", sqlconfig.Endpoint{
3131
EndpointDetails: sqlconfig.EndpointDetails{
3232
Port: 1433,
3333
},

cmd/modern/root/use.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import (
88
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
99
"github.com/microsoft/go-sqlcmd/internal/config"
1010
"github.com/microsoft/go-sqlcmd/internal/container"
11+
"github.com/microsoft/go-sqlcmd/internal/localizer"
1112
"github.com/microsoft/go-sqlcmd/internal/secret"
1213
"github.com/microsoft/go-sqlcmd/internal/sql"
1314
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest"
15+
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/mechanism"
16+
"strings"
1417
)
1518

1619
type Use struct {
@@ -48,7 +51,7 @@ func (c *Use) DefineCommand(...cmdparser.CommandOptions) {
4851
String: &c.useMechanism,
4952
DefaultString: "",
5053
Name: "use-mechanism",
51-
Usage: "Mechanism to use to bring database online (attach, restore, dacfx)",
54+
Usage: localizer.Sprintf("Mechanism to use to bring database online (%s)", strings.Join(mechanism.Mechanisms(), ",")),
5255
})
5356
}
5457

@@ -81,12 +84,7 @@ func (c *Use) run() {
8184

8285
// Copy source file (e.g. .bak/.bacpac etc.) for database to be made available to container
8386
useDatabase.CopyToContainer(id)
84-
85-
if useDatabase.IsExtractionNeeded() {
86-
output.Infof("Extracting files from %q", useDatabase.UrlFilename())
87-
useDatabase.Extract()
88-
}
89-
87+
9088
output.output.Infof("Bringing %q online using %q method",
9189
useDatabase.DatabaseName(),
9290
useDatabase.OnlineMethod(),

internal/cmdparser/options.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,19 @@ type AlternativeForFlagOptions struct {
1313
Value *string
1414
}
1515

16-
// FlagOptions type represents options for defining a flag for a command-line
17-
// interface. The Name and Shorthand fields specify the long and short names
18-
// for the flag, respectively. The Usage field is a string that describes how the
19-
// flag should be used. The String, DefaultString, Int, DefaultInt, Bool, and
20-
// DefaultBool fields are used to specify the type and default value of the flag,
21-
// if it is a string, int, or bool type. The String and Int fields should be pointers
22-
// to the variables that will store the flag's value, and the Bool field should be
23-
// a pointer to a bool variable that will be set to true if the flag is present. The
24-
// DefaultString, DefaultInt, and DefaultBool fields are the default values to
25-
// use if the flag is not provided by the user.
16+
// FlagOptions type represents options for defining a flag for a CLI. The Name
17+
// and Shorthand fields specify the long and short names for the flag, respectively.
18+
// The Usage field is a string that describes how the flag should be used. If you
19+
// want the flag hidden from the --help, see the Hidden field to true.
20+
// The String, DefaultString, Int, DefaultInt, Bool, and DefaultBool fields are
21+
// used to specify the type and default value of the flag, use only one of these pairs
22+
// (the one that match the type for the flag value).
2623
type FlagOptions struct {
27-
Name string
28-
Shorthand string
29-
Usage string
24+
Name string // e.g. --database
25+
Shorthand string // e.g. -d
26+
Usage string // e.g. "The database to connect to"
3027

31-
Hidden bool
28+
Hidden bool // hide the flag from help (use for deprecated flags)
3229

3330
String *string
3431
DefaultString string

internal/container/controller.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bufio"
99
"bytes"
1010
"context"
11-
"fmt"
1211
"github.com/docker/docker/api/types"
1312
"github.com/docker/docker/api/types/container"
1413
"github.com/docker/docker/api/types/filters"
@@ -289,11 +288,6 @@ func (c Controller) DownloadFile(id string, src string, destFolder string) {
289288
c.RunCmdInContainer(id, cmd, ExecOptions{})
290289
}
291290

292-
type ExecOptions struct {
293-
User string
294-
Env []string
295-
}
296-
297291
func (c Controller) RunCmdInContainer(
298292
id string,
299293
cmd []string,
@@ -339,7 +333,6 @@ func (c Controller) RunCmdInContainer(
339333
stderr, err := io.ReadAll(&errBuf)
340334
checkErr(err)
341335

342-
trace(fmt.Sprintf("Err: %v", err))
343336
trace("Stdout: " + string(stdout))
344337
trace("Stderr: " + string(stderr))
345338

internal/container/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ type RunOptions struct {
1010
Command []string
1111
UnitTestFailure bool
1212
}
13+
14+
type ExecOptions struct {
15+
User string
16+
Env []string
17+
}

internal/output/output.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,6 @@ func (o Output) maskSecrets(text string) string {
215215
r := regexp.MustCompile(`(PASSWORD.*\s?=.*\s?N?')(.*)(')`)
216216
text = r.ReplaceAllString(text, "$1********$3")
217217

218-
// Mask password from sqlpackage.exe command line e.g. /TargetPassword:foo
219-
r = regexp.MustCompile(`(/TargetPassword:)(.*)`)
220-
text = r.ReplaceAllString(text, "$1********$3")
221-
222218
return text
223219
}
224220

pkg/mssqlcontainer/ingest/extract/7zip.go

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)