github.com/duskeagle/pop@v4.10.1-0.20190417200916-92f2b794aab5+incompatible/soda/cmd/schema/dump.go (about) 1 package schema 2 3 import ( 4 "errors" 5 "io" 6 "os" 7 "path/filepath" 8 9 "github.com/gobuffalo/pop" 10 "github.com/spf13/cobra" 11 ) 12 13 var dumpOptions = struct { 14 env string 15 output string 16 }{} 17 18 // DumpCmd dumps out the schema of the selected database. 19 var DumpCmd = &cobra.Command{ 20 Use: "dump", 21 Short: "Dumps out the schema of the selected database", 22 RunE: func(cmd *cobra.Command, args []string) error { 23 env := cmd.Flag("env") 24 if env == nil { 25 return errors.New("env is required") 26 } 27 dumpOptions.env = env.Value.String() 28 c, err := pop.Connect(dumpOptions.env) 29 if err != nil { 30 return err 31 } 32 var out io.Writer 33 rollback := func() {} 34 if dumpOptions.output == "-" { 35 out = os.Stdout 36 } else { 37 err = os.MkdirAll(filepath.Dir(dumpOptions.output), 0755) 38 if err != nil { 39 return err 40 } 41 out, err = os.Create(dumpOptions.output) 42 if err != nil { 43 return err 44 } 45 rollback = func() { 46 os.RemoveAll(dumpOptions.output) 47 } 48 } 49 if err := c.Dialect.DumpSchema(out); err != nil { 50 rollback() 51 return err 52 } 53 return nil 54 }, 55 } 56 57 func init() { 58 DumpCmd.Flags().StringVarP(&dumpOptions.output, "output", "o", "./migrations/schema.sql", "The path to dump the schema to.") 59 }