Compare commits
No commits in common. "2c8b842f1728acb400c611131934ffd1e9b472da" and "5a0ecdec1c459204201aabfb2892b5d2d6373618" have entirely different histories.
2c8b842f17
...
5a0ecdec1c
@ -14,17 +14,17 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
root_cmd := &cobra.Command{
|
rootCmd := &cobra.Command{
|
||||||
Use: "gas",
|
Use: "gas",
|
||||||
SilenceErrors: true,
|
SilenceErrors: true,
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
}
|
}
|
||||||
root_cmd.AddCommand(sqlite_lint)
|
rootCmd.AddCommand(sqlite_lint)
|
||||||
root_cmd.AddCommand(cmd_init)
|
if err := rootCmd.Execute(); err != nil {
|
||||||
if err := root_cmd.Execute(); err != nil {
|
|
||||||
fmt.Println(RED + err.Error() + RESET)
|
fmt.Println(RED + err.Error() + RESET)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Subcommand "init"
|
||||||
// Subcommand "generate_models"
|
// Subcommand "generate_models"
|
||||||
|
@ -1,68 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
|
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/codegen"
|
|
||||||
. "git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"
|
|
||||||
)
|
|
||||||
|
|
||||||
var cmd_init = &cobra.Command{
|
|
||||||
Use: "init [path]",
|
|
||||||
Short: "Initialize a new project",
|
|
||||||
Long: "Initialize a new Gas Stack project at the given path. If no path is given, defaults to current directory.",
|
|
||||||
|
|
||||||
Args: cobra.MaximumNArgs(1),
|
|
||||||
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
var target string
|
|
||||||
if len(args) != 0 {
|
|
||||||
target = args[0]
|
|
||||||
PanicIf(os.MkdirAll(target, 0o755))
|
|
||||||
PanicIf(os.Chdir(target))
|
|
||||||
} else {
|
|
||||||
// Default to current directory (".")
|
|
||||||
target = Must(os.Getwd())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get all the config values
|
|
||||||
get_val := func(prompt string, val *string) {
|
|
||||||
fmt.Printf("%s (%q): ", prompt, *val)
|
|
||||||
input := ""
|
|
||||||
Must(fmt.Scanln(&input))
|
|
||||||
if input != "" {
|
|
||||||
*val = input
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pkg_opts := codegen.PkgOpts{
|
|
||||||
ModuleName: Must(cmd.Flags().GetString("module")),
|
|
||||||
DBFilename: Must(cmd.Flags().GetString("db")),
|
|
||||||
BinaryName: Must(cmd.Flags().GetString("binary")),
|
|
||||||
}
|
|
||||||
if pkg_opts.ModuleName == "" {
|
|
||||||
pkg_opts.ModuleName = filepath.Base(target)
|
|
||||||
get_val("module name", &pkg_opts.ModuleName)
|
|
||||||
}
|
|
||||||
if pkg_opts.DBFilename == "" {
|
|
||||||
pkg_opts.DBFilename = pkg_opts.ModuleName + ".db"
|
|
||||||
get_val("db name", &pkg_opts.DBFilename)
|
|
||||||
}
|
|
||||||
if pkg_opts.BinaryName == "" {
|
|
||||||
pkg_opts.BinaryName = pkg_opts.ModuleName
|
|
||||||
get_val("binary name", &pkg_opts.BinaryName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run project initialization
|
|
||||||
codegen.InitPkg(pkg_opts)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
cmd_init.Flags().String("module", "", "Module name")
|
|
||||||
cmd_init.Flags().String("db", "", "Database filename")
|
|
||||||
cmd_init.Flags().String("binary", "", "Binary name")
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
https://astexplorer.net/
|
|
@ -1,51 +0,0 @@
|
|||||||
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`)
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
sudo mount -t tmpfs -o size=100M tmpfs sample_data/data
|
|
@ -1,6 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
rm sample_data/data/* || true
|
|
||||||
|
|
||||||
go run ./cmd init
|
|
||||||
sqlite3 sample_data/data/{{ .DBFilename }} < sample_data/seed.sql
|
|
@ -1,10 +0,0 @@
|
|||||||
PRAGMA foreign_keys = on;
|
|
||||||
|
|
||||||
-- =======
|
|
||||||
-- DB meta
|
|
||||||
-- =======
|
|
||||||
|
|
||||||
create table db_version (
|
|
||||||
version integer not null
|
|
||||||
) strict;
|
|
||||||
insert into db_version values(0);
|
|
Loading…
x
Reference in New Issue
Block a user