Add package initialization subcommand and package
All checks were successful
CI / release-test (push) Successful in 26s
All checks were successful
CI / release-test (push) Successful in 26s
This commit is contained in:
51
pkg/codegen/pkg.go
Normal file
51
pkg/codegen/pkg.go
Normal file
@@ -0,0 +1,51 @@
|
||||
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`)
|
||||
}
|
||||
3
pkg/codegen/tpl/mount.sh
Normal file
3
pkg/codegen/tpl/mount.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
sudo mount -t tmpfs -o size=100M tmpfs sample_data/data
|
||||
6
pkg/codegen/tpl/reset.sh
Normal file
6
pkg/codegen/tpl/reset.sh
Normal file
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
rm sample_data/data/* || true
|
||||
|
||||
go run ./cmd init
|
||||
sqlite3 sample_data/data/{{ .DBFilename }} < sample_data/seed.sql
|
||||
10
pkg/codegen/tpl/schema.sql
Normal file
10
pkg/codegen/tpl/schema.sql
Normal file
@@ -0,0 +1,10 @@
|
||||
PRAGMA foreign_keys = on;
|
||||
|
||||
-- =======
|
||||
-- DB meta
|
||||
-- =======
|
||||
|
||||
create table db_version (
|
||||
version integer not null
|
||||
) strict;
|
||||
insert into db_version values(0);
|
||||
Reference in New Issue
Block a user