snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
41
cmd/entc/entc.go
Normal file
41
cmd/entc/entc.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"entgo.io/ent/cmd/internal/base"
|
||||
"entgo.io/ent/entc/gen"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.SetFlags(0)
|
||||
cmd := &cobra.Command{Use: "entc"}
|
||||
cmd.AddCommand(
|
||||
base.NewCmd(),
|
||||
base.DescribeCmd(),
|
||||
base.GenerateCmd(migrate),
|
||||
base.InitCmd(),
|
||||
)
|
||||
_ = cmd.Execute()
|
||||
}
|
||||
|
||||
func migrate(c *gen.Config) {
|
||||
var (
|
||||
target = filepath.Join(c.Target, "generate.go")
|
||||
oldCmd = []byte("entgo.io/ent/cmd/entc")
|
||||
)
|
||||
buf, err := os.ReadFile(target)
|
||||
if err != nil || !bytes.Contains(buf, oldCmd) {
|
||||
return
|
||||
}
|
||||
_ = os.WriteFile(target, bytes.ReplaceAll(buf, oldCmd, []byte("entgo.io/ent/cmd/ent")), 0644)
|
||||
}
|
||||
35
cmd/entc/entc_test.go
Normal file
35
cmd/entc/entc_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCmd(t *testing.T) {
|
||||
defer os.RemoveAll("ent")
|
||||
cmd := exec.Command("go", "run", "entgo.io/ent/cmd/entc", "new", "User")
|
||||
stderr := bytes.NewBuffer(nil)
|
||||
cmd.Stderr = stderr
|
||||
require.NoError(t, cmd.Run(), stderr.String())
|
||||
|
||||
_, err := os.Stat("ent/generate.go")
|
||||
require.NoError(t, err)
|
||||
_, err = os.Stat("ent/schema/user.go")
|
||||
require.NoError(t, err)
|
||||
|
||||
cmd = exec.Command("go", "run", "entgo.io/ent/cmd/entc", "generate", "./ent/schema")
|
||||
stderr = bytes.NewBuffer(nil)
|
||||
cmd.Stderr = stderr
|
||||
require.NoError(t, cmd.Run(), stderr.String())
|
||||
|
||||
_, err = os.Stat("ent/user.go")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user