Files
gas-stack/pkg/codegen/pkg.go

58 lines
1.5 KiB
Go

package codegen
import (
"embed"
"fmt"
"os"
"os/exec"
"text/template"
"git.offline-twitter.com/offline-labs/gas-stack/pkg/must"
)
//go:embed "tpl"
var tpl embed.FS
type PkgOpts struct {
ModuleName string
DBFilename string
BinaryName string
}
func InitPkg(opts PkgOpts) {
// Run `go mod init`
fmt.Printf("Running... `go mod init %s`\n", opts.ModuleName)
must.Do(exec.Command("go", "mod", "init", opts.ModuleName).Run())
// Run `git init`, if required
if exec.Command("git", "status").Run() != nil {
// Not in a git repo yet; init one
fmt.Println("Running... `git init`")
must.Do(exec.Command("git", "init").Run())
}
// Create package structure
must.Do(os.MkdirAll("pkg/db", 0o755))
must.Do(os.MkdirAll("cmd", 0o755))
must.Do(os.MkdirAll("doc", 0o755))
must.Do(os.MkdirAll("sample_data", 0o755))
must.Do(os.WriteFile("pkg/db/schema.sql", must.Get(tpl.ReadFile("tpl/schema.sql")), 0o664))
must.Do(os.WriteFile("pkg/db/db.go", must.Get(tpl.ReadFile("tpl/db.go.tpl")), 0o664))
dbTest := must.Get(os.Create("pkg/db/db_test.go"))
defer must.Close(dbTest)
t := must.Get(template.ParseFS(tpl, "tpl/db_test.go.tpl"))
must.Do(t.Execute(dbTest, opts))
must.Do(os.WriteFile("sample_data/mount.sh", must.Get(tpl.ReadFile("tpl/mount.sh")), 0o775))
must.Do(os.WriteFile("sample_data/reset.sh", must.Get(tpl.ReadFile("tpl/reset.sh")), 0o775))
// TODO:
// - create `pkg/db/errors.go`
// - create `sample_data/seed.sql`
// - create `sample_data/data/`
// - create `.gitignore`
// - do something with `db_setup.go` (should go in Gas Stack `pkg/db`)
}