A small project that collects various nice things to get started with Go Web Development.
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.
 
 
 
 
 

78 lines
1.7 KiB

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) }
}