Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from schemahero/fixtures
First pass at fixture generation
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/schemahero/schemahero/pkg/fixtures" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func Fixtures() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "fixtures", | ||
Short: "fixtures creates sql statements from a schemahero definition", | ||
Long: `...`, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
// workaround for https://github.com/spf13/viper/issues/233 | ||
viper.BindPFlag("driver", cmd.Flags().Lookup("driver")) | ||
viper.BindPFlag("dbname", cmd.Flags().Lookup("dbname")) | ||
viper.BindPFlag("input-dir", cmd.Flags().Lookup("input-dir")) | ||
viper.BindPFlag("output-dir", cmd.Flags().Lookup("output-dir")) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
f := fixtures.NewFixturator() | ||
return f.RunSync() | ||
}, | ||
} | ||
|
||
cmd.Flags().String("driver", "", "name of the database driver to use") | ||
cmd.Flags().String("dbname", "", "schemahero database name to write in the yaml") | ||
cmd.Flags().String("input-dir", "", "directory to read schema files from") | ||
cmd.Flags().String("output-dir", "", "directory to write fixture files to") | ||
|
||
cmd.MarkFlagRequired("driver") | ||
cmd.MarkFlagRequired("dbname") | ||
cmd.MarkFlagRequired("input-dir") | ||
cmd.MarkFlagRequired("output-dir") | ||
|
||
viper.BindPFlags(cmd.Flags()) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package fixtures | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
schemasv1alpha1 "github.com/schemahero/schemahero/pkg/apis/schemas/v1alpha1" | ||
"github.com/schemahero/schemahero/pkg/database/postgres" | ||
"github.com/spf13/viper" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type Fixturator struct { | ||
Viper *viper.Viper | ||
} | ||
|
||
func NewFixturator() *Fixturator { | ||
return &Fixturator{ | ||
Viper: viper.GetViper(), | ||
} | ||
} | ||
|
||
func (f *Fixturator) RunSync() error { | ||
fmt.Printf("generating fixtures for resources in %s\n", f.Viper.GetString("input-dir")) | ||
|
||
statements := []string{} | ||
handleFile := func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
fileData, err := ioutil.ReadFile(filepath.Join(f.Viper.GetString("input-dir"), info.Name())) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
parsed := schemasv1alpha1.Table{} | ||
if err := yaml.Unmarshal(fileData, &parsed); err != nil { | ||
fmt.Printf("%s\n", err) | ||
return err | ||
} | ||
|
||
if parsed.Spec.Schema == nil { | ||
fmt.Printf("skipping file %s because there is no schema\n", info.Name()) | ||
return nil | ||
} | ||
|
||
if f.Viper.GetString("driver") == "postgres" { | ||
if parsed.Spec.Schema.Postgres == nil { | ||
fmt.Printf("skipping file %s because there is no postgres spec\n", info.Name()) | ||
} | ||
|
||
statement, err := postgres.CreateTableStatement(parsed.Spec.Name, parsed.Spec.Schema.Postgres) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
statements = append(statements, statement) | ||
} | ||
return nil | ||
} | ||
|
||
err := filepath.Walk(f.Viper.GetString("input-dir"), handleFile) | ||
if err != nil { | ||
fmt.Printf("%#v\n", err) | ||
return err | ||
} | ||
|
||
output := strings.Join(statements, ";\n") | ||
output = fmt.Sprintf("/* Auto generated file. Do not edit by hand. This file was generated by SchemaHero. */\n\n %s;\n\n", output) | ||
|
||
err = ioutil.WriteFile(filepath.Join(f.Viper.GetString("output-dir"), "fixtures.sql"), []byte(output), 0644) | ||
if err != nil { | ||
fmt.Printf("%#v\n", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |