Continuing the starter kit

This commit is contained in:
Achim Rohn
2025-09-01 00:57:55 +02:00
parent f807f47fa5
commit dae891a610
2 changed files with 55 additions and 0 deletions
+50
View File
@@ -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)
+5
View File
@@ -0,0 +1,5 @@
module $ModuleName$
go 1.25
require github.com/steebchen/prisma-client-go v0.47.0 // indirect