MCPcopy
hub / github.com/sqldef/sqldef / parseOptions

Function parseOptions

cmd/mssqldef/mssqldef.go:22–146  ·  view source on GitHub ↗

Return parsed options and schema filename TODO: Support `sqldef schema.sql -opt val...`

(args []string)

Source from the content-addressed store, hash-verified

20// Return parsed options and schema filename
21// TODO: Support `sqldef schema.sql -opt val...`
22func parseOptions(args []string) (database.Config, *sqldef.Options) {
23 // MSSQL default: legacy_ignore_quotes is true (legacy mode)
24 defaultConfig := database.GeneratorConfig{LegacyIgnoreQuotes: true}
25 configs := []database.GeneratorConfig{defaultConfig}
26
27 var opts struct {
28 User string `short:"U" long:"user" description:"MSSQL user name" value-name:"USERNAME" default:"sa"`
29 Password string `short:"P" long:"password" description:"MSSQL user password, overridden by $MSSQL_PWD" value-name:"PASSWORD"`
30 Host string `short:"h" long:"host" description:"Host to connect to the MSSQL server" value-name:"HOSTNAME" default:"127.0.0.1"`
31 Port uint `short:"p" long:"port" description:"Port used for the connection" value-name:"PORT" default:"1433"`
32 Trusted bool `short:"E" long:"trusted-connection" description:"Use Windows authentication"`
33 Instance string `long:"instance" description:"Instance name" value-name:"INSTANCE"`
34 TrustCert bool `long:"trust-server-cert" description:"Trust server certificate"`
35 Prompt bool `long:"password-prompt" description:"Force MSSQL user password prompt"`
36 File []string `long:"file" description:"Read desired SQL from the file, rather than stdin" value-name:"FILENAME" default:"-"`
37 DryRun bool `long:"dry-run" description:"Don't run DDLs but just show them"`
38 Check bool `long:"check" description:"Like --dry-run, but exit with code 2 when DDL would be applied (useful as a CI gate to detect schema drift)"`
39 Apply bool `long:"apply" description:"Apply DDLs to the database (default, but will require this flag in future versions)"`
40 Export bool `long:"export" description:"Just dump the current schema to stdout"`
41 EnableDrop bool `long:"enable-drop" description:"Enable destructive changes such as DROP for TABLE, SCHEMA, ROLE, USER, FUNCTION, PROCEDURE, TRIGGER, VIEW, INDEX, SEQUENCE, TYPE"`
42
43 // Custom handlers for config flags to preserve order
44 Config func(string) `long:"config" description:"YAML configuration file (can be specified multiple times)" value-name:"PATH"`
45 ConfigInline func(string) `long:"config-inline" description:"YAML configuration as inline string (can be specified multiple times)" value-name:"YAML"`
46
47 Help bool `long:"help" description:"Show this help"`
48 Version bool `long:"version" description:"Show version information"`
49 }
50
51 opts.Config = func(path string) {
52 configs = append(configs, database.ParseGeneratorConfig(path, defaultConfig))
53 }
54 opts.ConfigInline = func(yaml string) {
55 configs = append(configs, database.ParseGeneratorConfigString(yaml, defaultConfig))
56 }
57
58 parser := flags.NewParser(&opts, flags.None)
59 parser.Usage = `[OPTION]... DATABASE --export
60 mssqldef [OPTION]... DATABASE --apply < desired.sql
61 mssqldef [OPTION]... DATABASE --dry-run < desired.sql
62 mssqldef [OPTION]... current.sql < desired.sql`
63 args, err := parser.ParseArgs(args)
64 if err != nil {
65 log.Fatal(err)
66 }
67
68 if opts.Help {
69 parser.WriteHelp(os.Stdout)
70 fmt.Printf("\nFor more information, see: https://github.com/sqldef/sqldef/blob/v%s/cmd-mssqldef.md\n", sqldef.GetVersion())
71 os.Exit(0)
72 }
73
74 if opts.Version {
75 fmt.Println(sqldef.GetFullVersion())
76 os.Exit(0)
77 }
78
79 desiredFiles := sqldef.ParseFiles(opts.File)

Callers 1

mainFunction · 0.70

Calls 8

ParseGeneratorConfigFunction · 0.92
MergeGeneratorConfigsFunction · 0.92
FatalMethod · 0.65
PrintfMethod · 0.65
PrintlnMethod · 0.65
FatalfMethod · 0.65
PrintMethod · 0.65

Tested by

no test coverage detected