mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user