@@ -5,6 +5,7 @@ package install
55
66import (
77 "fmt"
8+ "github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/mechanism"
89 "runtime"
910 "strings"
1011
@@ -100,11 +101,11 @@ func (c *MssqlBase) AddFlags(
100101 Usage : "Context name (a default context name will be created if not provided)" ,
101102 })
102103
103- // BUG(stuartpa): Make this a hidden flag so we don't break existing usage
104104 addFlag (cmdparser.FlagOptions {
105105 String : & c .defaultDatabase ,
106106 Name : "user-database" ,
107107 Shorthand : "u" ,
108+ Hidden : true ,
108109 Usage : "[DEPRECATED use --database] Create a user database and set it as the default for login" ,
109110 })
110111
@@ -223,11 +224,11 @@ func (c *MssqlBase) AddFlags(
223224 Usage : "Port (next available port from 1433 upwards used by default)" ,
224225 })
225226
226- // BUG(stuartpa): Make this a hidden flag so we don't break existing usage
227227 addFlag (cmdparser.FlagOptions {
228228 String : & c .useDatabaseUrl ,
229229 DefaultString : "" ,
230230 Name : "using" ,
231+ Hidden : true ,
231232 Usage : fmt .Sprintf ("[DEPRECATED use --use] Download %q and use database" , ingest .ValidFileExtensions ()),
232233 })
233234
@@ -242,7 +243,7 @@ func (c *MssqlBase) AddFlags(
242243 String : & c .useMechanism ,
243244 DefaultString : "" ,
244245 Name : "use-mechanism" ,
245- Usage : "Mechanism to use to bring database online (attach, restore, dacfx)" ,
246+ Usage : fmt . Sprintf ( "Mechanism to use to bring database online (%s)" , strings . Join ( mechanism . Mechanisms (), "," )) ,
246247 })
247248
248249 addFlag (cmdparser.FlagOptions {
@@ -317,37 +318,7 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
317318 // Do an early exit if url doesn't exist
318319 var useDatabase ingest.Ingest
319320 if c .useDatabaseUrl != "" {
320- useDatabase = ingest .NewIngest (c .useDatabaseUrl , controller , ingest.IngestOptions {
321- Mechanism : c .useMechanism ,
322- })
323-
324- if ! useDatabase .IsValidFileExtension () {
325- output .FatalfWithHints (
326- []string {
327- fmt .Sprintf (
328- "--using must be a path to a file with a %q extension" ,
329- ingest .ValidFileExtensions (),
330- ),
331- },
332- "%q is not a valid file extension for --using flag" , useDatabase .UserProvidedFileExt ())
333- }
334-
335- if useDatabase .IsRemoteUrl () && ! useDatabase .IsValidScheme () {
336- output .FatalfWithHints (
337- []string {
338- fmt .Sprintf (
339- "--using URL must one of %q" ,
340- strings .Join (useDatabase .ValidSchemes (), ", " ),
341- ),
342- },
343- "%q is not a valid URL for --using flag" , c .useDatabaseUrl )
344- }
345-
346- if ! useDatabase .SourceFileExists () {
347- output .FatalfWithHints (
348- []string {fmt .Sprintf ("File does not exist at URL %q" , c .useDatabaseUrl )},
349- "Unable to download file" )
350- }
321+ useDatabase = c .verifyUseSourceFileExists (useDatabase , controller , output )
351322 }
352323
353324 if c .defaultDatabase != "" {
@@ -360,18 +331,18 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
360331 c .downloadImage (imageName , output , controller )
361332 }
362333
334+ runOptions := container.RunOptions {
335+ Env : env ,
336+ Port : c .port ,
337+ Name : c .name ,
338+ Hostname : c .hostname ,
339+ Architecture : c .architecture ,
340+ Os : c .os ,
341+ Command : []string {},
342+ }
343+
363344 output .Infof ("Starting %v" , imageName )
364- containerId := controller .ContainerRun (
365- imageName ,
366- env ,
367- c .port ,
368- c .name ,
369- c .hostname ,
370- c .architecture ,
371- c .os ,
372- []string {},
373- false ,
374- )
345+ containerId := controller .ContainerRun (imageName , runOptions )
375346 previousContextName := config .CurrentContextName ()
376347
377348 userName := pal .UserName ()
@@ -396,8 +367,7 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
396367
397368 controller .ContainerWaitForLogEntry (containerId , c .errorLogEntryToWaitFor )
398369
399- output .Infof (
400- "Disabled %q account (and rotated %q password). Creating user %q" ,
370+ output .Infof ("Disabled %q account (and rotated %q password). Creating user %q" ,
401371 "sa" ,
402372 "sa" ,
403373 userName )
@@ -488,14 +458,10 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
488458 ads .PersistCredentialForAds (endpoint .EndpointDetails .Address , endpoint , user )
489459
490460 output := c .Output ()
491- args := []string {
492- "-r" ,
493- fmt .Sprintf (
494- "--server=%s" , fmt .Sprintf (
495- "%s,%d" ,
496- "127.0.0.1" ,
497- c .port )),
498- }
461+ args := []string {"-r" , fmt .Sprintf ("--server=%s" , fmt .Sprintf (
462+ "%s,%d" ,
463+ "127.0.0.1" ,
464+ c .port ))}
499465
500466 args = append (args , fmt .Sprintf ("--user=%s" ,
501467 strings .Replace (userName , `"` , `\"` , - 1 )))
@@ -510,6 +476,41 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
510476 }
511477}
512478
479+ func (c * MssqlBase ) verifyUseSourceFileExists (useDatabase ingest.Ingest , controller * container.Controller , output * output.Output ) ingest.Ingest {
480+ useDatabase = ingest .NewIngest (c .useDatabaseUrl , controller , ingest.IngestOptions {
481+ Mechanism : c .useMechanism ,
482+ })
483+
484+ if ! useDatabase .IsValidFileExtension () {
485+ output .FatalfWithHints (
486+ []string {
487+ fmt .Sprintf (
488+ "--using must be a path to a file with a %q extension" ,
489+ ingest .ValidFileExtensions (),
490+ ),
491+ },
492+ "%q is not a valid file extension for --using flag" , useDatabase .UserProvidedFileExt ())
493+ }
494+
495+ if useDatabase .IsRemoteUrl () && ! useDatabase .IsValidScheme () {
496+ output .FatalfWithHints (
497+ []string {
498+ fmt .Sprintf (
499+ "--using URL must one of %q" ,
500+ strings .Join (useDatabase .ValidSchemes (), ", " ),
501+ ),
502+ },
503+ "%q is not a valid URL for --using flag" , c .useDatabaseUrl )
504+ }
505+
506+ if ! useDatabase .SourceFileExists () {
507+ output .FatalfWithHints (
508+ []string {fmt .Sprintf ("File does not exist at URL %q" , c .useDatabaseUrl )},
509+ "Unable to download file" )
510+ }
511+ return useDatabase
512+ }
513+
513514// createNonSaUser creates a user (non-sa) and assigns the sysadmin role
514515// to the user. It also creates a default database with the provided name
515516// and assigns the default database to the user. Finally, it disables
0 commit comments