From dae891a6108c093433643e924e4b6b8d302274ed Mon Sep 17 00:00:00 2001 From: Achim Rohn Date: Mon, 1 Sep 2025 00:57:55 +0200 Subject: [PATCH] Continuing the starter kit --- starter/create/create.go | 50 ++++++++++++++++++++++++++++++++++++++++ starter/go.mod.template | 5 ++++ 2 files changed, 55 insertions(+) create mode 100644 starter/go.mod.template diff --git a/starter/create/create.go b/starter/create/create.go index 834da63..5714971 100644 --- a/starter/create/create.go +++ b/starter/create/create.go @@ -20,6 +20,8 @@ const ( type Params struct { DbType DatabaseType ProjectDir string + ModuleName string + GoPath string } type StarterCreator struct { @@ -74,6 +76,9 @@ func (s StarterCreator) Create() { s.copyFile("../go.work.template", "go.work") s.copyFile("../../template.air.toml", ".air.toml") + //s.copyGoMod() + //s.executeGoModTidy() + s.executeGetPrismaClient() if s.params.DbType == DataBaseTypeSqlite { must(os.MkdirAll(filepath.Join(s.params.ProjectDir, "db"), 0o755)) @@ -83,6 +88,12 @@ func (s StarterCreator) Create() { must(os.MkdirAll(filepath.Join(s.params.ProjectDir, "scripts"), 0o755)) s.copyFile("../../scripts/db-push.sh", "scripts/db-push.sh") + if s.params.DbType == DataBaseTypeSqlite { + s.executeDbPush() + } +} + +func (s StarterCreator) executeDbPush() { // After copying create_db.sh, run it with the project directory as the root scriptPath := filepath.Join(s.params.ProjectDir, "scripts", "db-push.sh") if runtime.GOOS == "windows" { @@ -131,12 +142,51 @@ func (s StarterCreator) copySchema() { sqliteBlock := "datasource db {\n provider = \"sqlite\"\n url = \"file:./db/sqlite.db\"\n}\n" contentStr := string(content) contentStr = strings.Replace(contentStr, pgBlock, sqliteBlock, 1) + contentStr = strings.ReplaceAll(contentStr, " @db.Timestamptz(3)", "") + contentStr = strings.ReplaceAll(contentStr, " @db.Timestamp(6)", "") content = []byte(contentStr) } must(os.WriteFile(filepath.Join(s.params.ProjectDir, "schema.prisma"), content, 0o644)) } +func (s StarterCreator) copyGoMod() { + content, err := os.ReadFile(filepath.Join(s.thisDir, "../go.mod.template")) + must(err) + contentString := InlineTemplate(string(content), struct { + ModuleName string + }{ + ModuleName: s.params.ModuleName, + }) + must(os.WriteFile(filepath.Join(s.params.ProjectDir, "go.mod"), []byte(contentString), 0o644)) +} + +func (s StarterCreator) executeGoModTidy() { + goCommand := s.getGoCommand() + cmd := exec.Command(goCommand, "mod", "tidy") + cmd.Dir = s.params.ProjectDir + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + must(cmd.Run()) +} + +func (s StarterCreator) getGoCommand() string { + goCommand := "go" + if s.params.GoPath != "" { + goCommand = s.params.GoPath + } + return goCommand +} + +func (s StarterCreator) executeGetPrismaClient() { + goCommand := s.getGoCommand() + cmd := exec.Command(goCommand, "get", "github.com/steebchen/prisma-client-go") + cmd.Dir = s.params.ProjectDir + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + must(cmd.Run()) +} + func must(err error) { if err != nil { panic(err) diff --git a/starter/go.mod.template b/starter/go.mod.template new file mode 100644 index 0000000..e0e6a22 --- /dev/null +++ b/starter/go.mod.template @@ -0,0 +1,5 @@ +module $ModuleName$ + +go 1.25 + +require github.com/steebchen/prisma-client-go v0.47.0 // indirect