ent/cmd: add target option for init command

Reviewed By: elys1um

Differential Revision: D16914253

fbshipit-source-id: df2b87d2741c96c374526c0a2d4171bd68d1e4f4
This commit is contained in:
Ariel Mashraki
2019-08-20 06:30:05 -07:00
committed by Facebook Github Bot
parent 24d03df613
commit c8c01f7398

View File

@@ -21,37 +21,42 @@ import (
func main() {
cmd := &cobra.Command{Use: "entc"}
cmd.AddCommand(
&cobra.Command{
Use: "init [schemas]",
Short: "initialize an environment with zero or more schemas",
Example: examples(
"entc init Example",
"entc init User Group",
),
DisableFlagsInUseLine: true,
Args: func(_ *cobra.Command, names []string) error {
for _, name := range names {
if !unicode.IsUpper(rune(name[0])) {
return fmt.Errorf("schema names must begin with uppercase")
}
func() *cobra.Command {
var (
path string
cmd = &cobra.Command{
Use: "init [flags] [schemas]",
Short: "initialize an environment with zero or more schemas",
Example: examples(
"entc init Example",
"entc init --target entv1/schema User Group",
),
Args: func(_ *cobra.Command, names []string) error {
for _, name := range names {
if !unicode.IsUpper(rune(name[0])) {
return fmt.Errorf("schema names must begin with uppercase")
}
}
return nil
},
Run: func(cmd *cobra.Command, names []string) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
}
failOnErr(err)
for _, name := range names {
b := bytes.NewBuffer(nil)
failOnErr(tmpl.Execute(b, name))
target := filepath.Join(path, strings.ToLower(name+".go"))
failOnErr(ioutil.WriteFile(target, b.Bytes(), 0644))
}
},
}
return nil
},
Run: func(cmd *cobra.Command, names []string) {
path := "ent/schema"
_, err := os.Stat(path)
if os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
}
failOnErr(err)
for _, name := range names {
b := bytes.NewBuffer(nil)
failOnErr(tmpl.Execute(b, name))
target := filepath.Join(path, strings.ToLower(name+".go"))
failOnErr(ioutil.WriteFile(target, b.Bytes(), 0644))
}
},
},
)
cmd.Flags().StringVar(&path, "target", "ent/schema", "target directory for schemas")
return cmd
}(),
&cobra.Command{
Use: "describe [flags] path",
Short: "print a description of the graph schema",