Return parsed options and schema filename TODO: Support `sqldef schema.sql -opt val...`
(args []string)
| 19 | // Return parsed options and schema filename |
| 20 | // TODO: Support `sqldef schema.sql -opt val...` |
| 21 | func parseOptions(args []string) (database.Config, *sqldef.Options) { |
| 22 | // SQLite default: legacy_ignore_quotes is true (legacy mode) |
| 23 | defaultConfig := database.GeneratorConfig{LegacyIgnoreQuotes: true} |
| 24 | configs := []database.GeneratorConfig{defaultConfig} |
| 25 | |
| 26 | var opts struct { |
| 27 | File []string `short:"f" long:"file" description:"Read desired SQL from the file, rather than stdin" value-name:"FILENAME" default:"-"` |
| 28 | DryRun bool `long:"dry-run" description:"Don't run DDLs but just show them"` |
| 29 | 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)"` |
| 30 | Apply bool `long:"apply" description:"Apply DDLs to the database (default, but will require this flag in future versions)"` |
| 31 | Export bool `long:"export" description:"Just dump the current schema to stdout"` |
| 32 | EnableDrop bool `long:"enable-drop" description:"Enable destructive changes such as DROP for TABLE, SCHEMA, ROLE, USER, FUNCTION, PROCEDURE, TRIGGER, VIEW, INDEX, SEQUENCE, TYPE"` |
| 33 | |
| 34 | // Custom handlers for config flags to preserve order |
| 35 | Config func(string) `long:"config" description:"YAML configuration file (can be specified multiple times)" value-name:"PATH"` |
| 36 | ConfigInline func(string) `long:"config-inline" description:"YAML configuration as inline string (can be specified multiple times)" value-name:"YAML"` |
| 37 | |
| 38 | Help bool `long:"help" description:"Show this help"` |
| 39 | Version bool `long:"version" description:"Show version information"` |
| 40 | } |
| 41 | |
| 42 | opts.Config = func(path string) { |
| 43 | configs = append(configs, database.ParseGeneratorConfig(path, defaultConfig)) |
| 44 | } |
| 45 | opts.ConfigInline = func(yaml string) { |
| 46 | configs = append(configs, database.ParseGeneratorConfigString(yaml, defaultConfig)) |
| 47 | } |
| 48 | |
| 49 | parser := flags.NewParser(&opts, flags.None) |
| 50 | parser.Usage = `[OPTION]... FILENAME --export |
| 51 | sqlite3def [OPTION]... FILENAME --apply < desired.sql |
| 52 | sqlite3def [OPTION]... FILENAME --dry-run < desired.sql |
| 53 | sqlite3def [OPTION]... current.sql < desired.sql` |
| 54 | args, err := parser.ParseArgs(args) |
| 55 | if err != nil { |
| 56 | log.Fatal(err) |
| 57 | } |
| 58 | |
| 59 | if opts.Help { |
| 60 | parser.WriteHelp(os.Stdout) |
| 61 | fmt.Printf("\nFor more information, see: https://github.com/sqldef/sqldef/blob/v%s/cmd-sqlite3def.md\n", sqldef.GetVersion()) |
| 62 | os.Exit(0) |
| 63 | } |
| 64 | |
| 65 | if opts.Version { |
| 66 | fmt.Println(sqldef.GetFullVersion()) |
| 67 | os.Exit(0) |
| 68 | } |
| 69 | |
| 70 | desiredFiles := sqldef.ParseFiles(opts.File) |
| 71 | |
| 72 | var desiredDDLs string |
| 73 | if !opts.Export { |
| 74 | desiredDDLs, err = sqldef.ReadFiles(desiredFiles) |
| 75 | if err != nil { |
| 76 | log.Fatalf("Failed to read '%v': %s", desiredFiles, err) |
| 77 | } |
| 78 | } |
no test coverage detected