parent
20fc1c3960
commit
4ddf65e6d1
@ -0,0 +1,8 @@ |
||||
package features |
||||
|
||||
import ( |
||||
"github.com/gofiber/fiber/v2" |
||||
) |
||||
|
||||
func Setup(app *fiber.App) { |
||||
} |
||||
@ -0,0 +1,78 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"embed" |
||||
"fmt" |
||||
"io/fs" |
||||
"flag" |
||||
"path/filepath" |
||||
"text/template" |
||||
"strings" |
||||
"os" |
||||
) |
||||
|
||||
//go:embed templates
|
||||
var templates embed.FS |
||||
|
||||
type Config struct { |
||||
Name string |
||||
} |
||||
|
||||
func WriteTemplate(config Config, from string, to string) error { |
||||
source, err := templates.ReadFile(from) |
||||
if err != nil { return err } |
||||
|
||||
t := template.Must(template.New(from).Parse(string(source))) |
||||
|
||||
out, err := os.Create(to) |
||||
if err != nil { return err } |
||||
|
||||
err = t.Execute(out, config) |
||||
if err != nil { panic(err) } |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func main() { |
||||
var config Config |
||||
|
||||
flag.StringVar(&config.Name, "name", "", "name of the feature to generate") |
||||
flag.Parse() |
||||
|
||||
if config.Name == "" { |
||||
fmt.Println("USAGE: fgen -name name") |
||||
return |
||||
} |
||||
|
||||
feature_dir := filepath.Join("features", config.Name) |
||||
view_dir := filepath.Join("views", config.Name) |
||||
|
||||
err := os.MkdirAll(feature_dir, 0755) |
||||
if err != nil { panic(err) } |
||||
|
||||
err = os.MkdirAll(view_dir, 0755) |
||||
if err != nil { panic(err) } |
||||
|
||||
err = fs.WalkDir(templates, "templates", |
||||
func(path string, d fs.DirEntry, err error) error { |
||||
if err != nil { return err } |
||||
target := filepath.Base(path) |
||||
|
||||
if !d.IsDir() { |
||||
switch { |
||||
case strings.HasPrefix(path, "templates/views"): |
||||
out_path := filepath.Join("views", config.Name, target) |
||||
err := WriteTemplate(config, path, out_path)
|
||||
if err != nil { panic(err) } |
||||
case strings.HasPrefix(path, "templates/feature"): |
||||
out_path := filepath.Join("features", config.Name, target) |
||||
err := WriteTemplate(config, path, out_path)
|
||||
if err != nil { panic(err) } |
||||
} |
||||
} |
||||
|
||||
return nil |
||||
}) |
||||
|
||||
if err != nil { panic(err) } |
||||
} |
||||
@ -0,0 +1,15 @@ |
||||
package features_{{ .Name }} |
||||
|
||||
import ( |
||||
"github.com/gofiber/fiber/v2" |
||||
) |
||||
|
||||
func GetApiExample(c *fiber.Ctx) error { |
||||
tables := []string{"test", "that"} |
||||
|
||||
return c.JSON(tables) |
||||
} |
||||
|
||||
func SetupApi(app *fiber.App) { |
||||
app.Get("/api/{{ .Name }}/example", GetApiExample) |
||||
} |
||||
@ -0,0 +1,9 @@ |
||||
package features_{{ .Name }} |
||||
|
||||
import ( |
||||
// "MY/webapp/data"
|
||||
// _ "github.com/mattn/go-sqlite3"
|
||||
// sq "github.com/Masterminds/squirrel"
|
||||
) |
||||
|
||||
|
||||
@ -0,0 +1,10 @@ |
||||
package features_{{ .Name }} |
||||
|
||||
import ( |
||||
"github.com/gofiber/fiber/v2" |
||||
) |
||||
|
||||
func Setup(app *fiber.App) { |
||||
SetupApi(app) |
||||
SetupViews(app) |
||||
} |
||||
@ -0,0 +1,11 @@ |
||||
package features_{{ .Name }} |
||||
|
||||
import ( |
||||
"github.com/gofiber/fiber/v2" |
||||
. "MY/webapp/common" |
||||
) |
||||
|
||||
func SetupViews(app *fiber.App) { |
||||
err := ConfigViews(app, "views/{{ .Name }}") |
||||
if err != nil { panic(err) } |
||||
} |
||||
@ -0,0 +1,3 @@ |
||||
<h1>{{ .Name }}</h1> |
||||
|
||||
<p>Replace me.</p> |
||||
Loading…
Reference in new issue