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:
		
							parent
							
								
									0bc951748d
								
							
						
					
					
						commit
						2c8b842f17
					
				| @ -14,17 +14,17 @@ const ( | |||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func main() { | func main() { | ||||||
| 	rootCmd := &cobra.Command{ | 	root_cmd := &cobra.Command{ | ||||||
| 		Use:           "gas", | 		Use:           "gas", | ||||||
| 		SilenceErrors: true, | 		SilenceErrors: true, | ||||||
| 		SilenceUsage:  true, | 		SilenceUsage:  true, | ||||||
| 	} | 	} | ||||||
| 	rootCmd.AddCommand(sqlite_lint) | 	root_cmd.AddCommand(sqlite_lint) | ||||||
| 	if err := rootCmd.Execute(); err != nil { | 	root_cmd.AddCommand(cmd_init) | ||||||
|  | 	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" | ||||||
|  | |||||||
							
								
								
									
										68
									
								
								cmd/subcmd_init.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								cmd/subcmd_init.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,68 @@ | |||||||
|  | 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") | ||||||
|  | } | ||||||
							
								
								
									
										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); | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user