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

Function parseOptions

cmd/psqldef/psqldef.go:23–160  ·  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

21// Return parsed options and schema filename
22// TODO: Support `sqldef schema.sql -opt val...`
23func parseOptions(args []string) (database.Config, *sqldef.Options) {
24 // PostgreSQL-specific default: legacy_ignore_quotes is true for backward compatibility
25 defaultConfig := database.GeneratorConfig{LegacyIgnoreQuotes: true}
26 configs := []database.GeneratorConfig{defaultConfig}
27
28 var opts struct {
29 User string `short:"U" long:"user" description:"PostgreSQL user name" value-name:"USERNAME" default:"postgres"`
30 Password string `short:"W" long:"password" description:"PostgreSQL user password, overridden by $PGPASSWORD" value-name:"PASSWORD"`
31 Host string `short:"h" long:"host" description:"Host or socket directory to connect to the PostgreSQL server" value-name:"HOSTNAME" default:"127.0.0.1"`
32 Port uint `short:"p" long:"port" description:"Port used for the connection" value-name:"PORT" default:"5432"`
33 Prompt bool `long:"password-prompt" description:"Force PostgreSQL user password prompt"`
34 File []string `short:"f" long:"file" description:"Read desired SQL from the file, rather than stdin" value-name:"FILENAME" default:"-"`
35 DryRun bool `long:"dry-run" description:"Don't run DDLs but just show them"`
36 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)"`
37 Apply bool `long:"apply" description:"Apply DDLs to the database (default, but will require this flag in future versions)"`
38 Export bool `long:"export" description:"Just dump the current schema to stdout"`
39 EnableDrop bool `long:"enable-drop" description:"Enable destructive changes such as DROP for TABLE, SCHEMA, ROLE, USER, FUNCTION, PROCEDURE, TRIGGER, VIEW, INDEX, SEQUENCE, TYPE"`
40 SkipView bool `long:"skip-view" description:"Skip managing views/materialized views"`
41 SkipExtension bool `long:"skip-extension" description:"Skip managing extensions"`
42 SkipPartition bool `long:"skip-partition" description:"Skip managing partitioned tables"`
43 DisableDdlTransaction bool `long:"disable-ddl-transaction" description:"Execute DDL statements outside a transaction block"`
44 BeforeApply string `long:"before-apply" description:"Execute the given string before applying the regular DDLs" value-name:"SQL"`
45
46 // Custom handlers for config flags to preserve order
47 Config func(string) `long:"config" description:"YAML configuration file (can be specified multiple times)" value-name:"PATH"`
48 ConfigInline func(string) `long:"config-inline" description:"YAML configuration as inline string (can be specified multiple times)" value-name:"YAML"`
49
50 Help bool `long:"help" description:"Show this help"`
51 Version bool `long:"version" description:"Show version information"`
52 }
53
54 opts.Config = func(path string) {
55 configs = append(configs, database.ParseGeneratorConfig(path, defaultConfig))
56 }
57 opts.ConfigInline = func(yaml string) {
58 configs = append(configs, database.ParseGeneratorConfigString(yaml, defaultConfig))
59 }
60
61 parser := flags.NewParser(&opts, flags.None)
62 parser.Usage = `[OPTION]... DBNAME --export
63 psqldef [OPTION]... DBNAME --apply < desired.sql
64 psqldef [OPTION]... DBNAME --dry-run < desired.sql
65 psqldef [OPTION]... current.sql < desired.sql`
66 args, err := parser.ParseArgs(args)
67 if err != nil {
68 log.Fatal(err)
69 }
70
71 if opts.Help {
72 parser.WriteHelp(os.Stdout)
73 fmt.Printf("\nFor more information, see: https://github.com/sqldef/sqldef/blob/v%s/cmd-psqldef.md\n", sqldef.GetVersion())
74 os.Exit(0)
75 }
76
77 if opts.Version {
78 fmt.Println(sqldef.GetFullVersion())
79 os.Exit(0)
80 }

Calls 8

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