52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package codegen
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
|
|
. "git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"
|
|
)
|
|
|
|
//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)
|
|
PanicIf(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`")
|
|
PanicIf(exec.Command("git", "init").Run())
|
|
}
|
|
|
|
// Create package structure
|
|
PanicIf(os.MkdirAll("pkg/db", 0o755))
|
|
PanicIf(os.MkdirAll("cmd", 0o755))
|
|
PanicIf(os.MkdirAll("doc", 0o755))
|
|
PanicIf(os.MkdirAll("sample_data", 0o755))
|
|
|
|
PanicIf(os.WriteFile("pkg/db/schema.sql", Must(tpl.ReadFile("tpl/schema.sql")), 0o664))
|
|
|
|
PanicIf(os.WriteFile("sample_data/mount.sh", Must(tpl.ReadFile("tpl/mount.sh")), 0o775))
|
|
PanicIf(os.WriteFile("sample_data/reset.sh", Must(tpl.ReadFile("tpl/reset.sh")), 0o775))
|
|
PanicIf(os.WriteFile("pkg/db/schema.sql", Must(tpl.ReadFile("tpl/schema.sql")), 0o664))
|
|
|
|
// 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`)
|
|
}
|