Skip to content

Commit

Permalink
Merge pull request #73 from schemahero/apply-multi
Browse files Browse the repository at this point in the history
Apply multi
  • Loading branch information
marccampbell committed Aug 13, 2019
2 parents ba48a4e + 7ea4075 commit 65b63bf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
54 changes: 47 additions & 7 deletions pkg/cli/apply.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cli

import (
"github.com/schemahero/schemahero/pkg/database"
"fmt"
"os"
"path/filepath"

"github.com/schemahero/schemahero/pkg/database"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -16,18 +19,55 @@ func Apply() *cobra.Command {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()

// to support automaticenv, we can't use cobra required flags
driver := v.GetString("driver")
uri := v.GetString("uri")
specFile := v.GetString("spec-file")

if driver == "" || uri == "" || specFile == "" {
missing := []string{}
if driver == "" {
missing = append(missing, "driver")
}
if uri == "" {
missing = append(missing, "uri")
}
if specFile == "" {
missing = append(missing, "spec-file")
}

return fmt.Errorf("missing required params: %v", missing)
}

fi, err := os.Stat(v.GetString("spec-file"))
if err != nil {
return err
}

db := database.NewDatabase()
return db.ApplySync()
if fi.Mode().IsDir() {
err := filepath.Walk(v.GetString("spec-file"), func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
if err := db.ApplySync(path); err != nil {
return err
}
}

return nil
})

return err
} else {
return db.ApplySync(v.GetString("spec-file"))
}
},
}

cmd.Flags().String("driver", "", "name of the database driver to use")
cmd.Flags().String("uri", "", "connection string uri to use")
cmd.Flags().String("spec-file", "", "filename containing the spec to apply")

cmd.MarkFlagRequired("driver")
cmd.MarkFlagRequired("uri")
cmd.MarkFlagRequired("spec-file")
cmd.Flags().String("spec-file", "", "filename or directory name containing the spec(s) to apply")

return cmd
}
3 changes: 3 additions & 0 deletions pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func RootCmd() *cobra.Command {
Use: "schemahero",
Short: "SchemaHero is a cloud-native database schema management tool",
Long: `...`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
os.Exit(1)
Expand Down
6 changes: 3 additions & 3 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func (d *Database) CreateFixturesSync() error {
return nil
}

func (d *Database) ApplySync() error {
specContents, err := ioutil.ReadFile(d.Viper.GetString("spec-file"))
func (d *Database) ApplySync(filename string) error {
specContents, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
Expand All @@ -139,7 +139,7 @@ func (d *Database) ApplySync() error {
}

if spec.Schema == nil {
fmt.Printf("skipping file %s because there is no schema\n", d.Viper.GetString("spec-file"))
fmt.Printf("skipping file %s because there is no schema\n", filename)
return nil
}

Expand Down

0 comments on commit 65b63bf

Please sign in to comment.