cmd/ent: Add check that model file doesn't already exist on ent init (#2307)

Previously if you run  it would overwrite an existing file  if that already existed. Now  checks if that file already exists, and returns an error if so.
This commit is contained in:
Andy Day
2022-02-02 15:44:04 -08:00
committed by GitHub
parent 0e49dd1d9f
commit 2e906bffa2
2 changed files with 11 additions and 0 deletions

View File

@@ -184,6 +184,9 @@ func initEnv(target string, names []string) error {
if err := gen.ValidSchemaName(name); err != nil {
return fmt.Errorf("init schema %s: %w", name, err)
}
if fileExists(target, name) {
return fmt.Errorf("init schema %s: already exists", name)
}
b := bytes.NewBuffer(nil)
if err := tmpl.Execute(b, name); err != nil {
return fmt.Errorf("executing template %s: %w", name, err)
@@ -213,6 +216,12 @@ func createDir(target string) error {
return nil
}
func fileExists(target, name string) bool {
var _, err = os.Stat(filepath.Join(target, strings.ToLower(name+".go")))
return err == nil
}
// schema template for the "init" command.
var tmpl = template.Must(template.New("schema").
Parse(`package schema