69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
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")
|
|
}
|