Mocks Templates for the framework. Build with go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mocks/main.go

226 lines
4.7 KiB

/**
* @author Robert Strutts <Robert@TryingToScale.com>
* @copyright Copyright (c) 2022, Robert Strutts.
* @license https://mit-license.org/
*/
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"text/template"
)
const version = "1.0.0"
var root string
var app_dir string
var method_name string
var main_file string
var root_in string
var verbose bool
type PhpTemplate struct {
Root string
Subfolder string
File string
Method string
}
// Declare type pointer to a template
var temp_ctrl *template.Template
var temp_inputs *template.Template
var temp_logic *template.Template
var temp_models *template.Template
var temp_outputs *template.Template
var temp_views *template.Template
var php_tpl PhpTemplate
func init_template() {
// template.Must takes the reponse of template.ParseFiles and does error checking
temp_ctrl = template.Must(template.ParseFiles(root + "/go_text_templates/ctrl.txt"))
temp_inputs = template.Must(template.ParseFiles(root + "/go_text_templates/inputs.txt"))
temp_logic = template.Must(template.ParseFiles(root + "/go_text_templates/logic.txt"))
temp_models = template.Must(template.ParseFiles(root + "/go_text_templates/models.txt"))
temp_outputs = template.Must(template.ParseFiles(root + "/go_text_templates/outputs.txt"))
temp_views = template.Must(template.ParseFiles(root + "/go_text_templates/views.txt"))
}
func make_a_dir(mydir string) {
if _, err := os.Stat(root + "/" + mydir); os.IsNotExist(err) {
os.Mkdir(root+"/"+mydir, 0775)
}
}
func use_file(filename string) io.Writer {
if verbose {
fmt.Println("------------")
return os.Stdout
}
_, err_fnf := os.Stat(root + filename)
if !os.IsNotExist(err_fnf) {
log.Fatalf("Sorry, %s already exists!", root+filename)
}
file, err := os.Create(root + filename)
if err != nil {
log.Fatal(err)
}
return file
}
func ctrl() {
mydir := "/controllers/" + app_dir
make_a_dir(mydir)
file := use_file(mydir + "/" + main_file + "_ctrl.php")
temp_err := temp_ctrl.Execute(file, php_tpl)
if temp_err != nil {
log.Fatalln(temp_err)
}
}
func inputs() {
mydir := "/inputs/" + app_dir
make_a_dir(mydir)
file := use_file(mydir + "/" + main_file + "_in.php")
temp_err := temp_inputs.Execute(file, php_tpl)
if temp_err != nil {
log.Fatalln(temp_err)
}
}
func logic() {
mydir := "/logic/" + app_dir
make_a_dir(mydir)
file := use_file(mydir + "/" + main_file + "_logic.php")
temp_err := temp_logic.Execute(file, php_tpl)
if temp_err != nil {
log.Fatalln(temp_err)
}
}
func models() {
mydir := "/models/" + app_dir
make_a_dir(mydir)
file := use_file(mydir + "/" + main_file + "_model.php")
temp_err := temp_models.Execute(file, php_tpl)
if temp_err != nil {
log.Fatalln(temp_err)
}
}
func outputs() {
mydir := "/outputs/" + app_dir
make_a_dir(mydir)
file := use_file(mydir + "/" + main_file + "_out.php")
temp_err := temp_outputs.Execute(file, php_tpl)
if temp_err != nil {
log.Fatalln(temp_err)
}
}
func views() {
mydir := "/views/default/" + app_dir
make_a_dir(mydir)
file := use_file(mydir + "/" + main_file + "_" + method_name + "_view.php")
temp_err := temp_views.Execute(file, php_tpl)
if temp_err != nil {
log.Fatalln(temp_err)
}
}
func grab_input() {
var sub_in string
var file_in string
var method_in string
fmt.Print("Enter SubFolder (app): ")
fmt.Scanln(&sub_in)
if sub_in == "" {
sub_in = "app"
}
fmt.Print("Enter File (home): ")
fmt.Scanln(&file_in)
if file_in == "" {
file_in = "home"
}
fmt.Print("Enter Method (index): ")
fmt.Scanln(&method_in)
if method_in == "" {
method_in = "index"
}
app_dir = sub_in
main_file = file_in
method_name = method_in
php_tpl = PhpTemplate{
Root: root_in,
Subfolder: sub_in,
File: file_in,
Method: method_in,
}
}
func usage() {
progName := filepath.Base(os.Args[0])
fmt.Printf(`%s version %s, (c) 2022 Robert Strutts
Usage:
%s [-www] [-app] [-root] [-v]
-h Print this help message.
-v Verbose mode, Display output instead of making files.
-www Web Dir IE /var/www
-app App Dir IE tts_project/src
-root Root Dir IE mockup
`, progName, version, progName)
}
func main() {
help := flag.Bool("h", false, "Help Page")
web_path := flag.String("www", "/var/www", "Web Dir")
app_path := flag.String("app", "tts_project/src", "App Dir")
root_path := flag.String("root", "mockup", "Root Dir")
v := flag.Bool("v", false, "Verbose output")
flag.Parse()
if *help {
usage()
return
}
verbose = *v
root = *web_path + "/" + *app_path + "/" + *root_path
root_in = *root_path
_, err := os.Stat(root)
if os.IsNotExist(err) {
log.Fatal("Missing root folders: mkdir -p " + root)
}
fmt.Println("Please wait...")
init_template()
grab_input()
ctrl()
inputs()
logic()
models()
outputs()
views()
}