Features:
- Scaffold out project directories from templates
- Uses Go's native templating engine
- Uses
fs.FS
for input, so it works well withgo:embed
and debme - Go alternative to cookiecutter
go get github.com/leaanthony/gosod
- Define a template directory
- Define some data
- Extract to a target directory
package main
import (
"log"
"github.com/leaanthony/gosod"
)
type config struct {
Name string
}
// mytemplate/
// ├── custom.filtername.txt
// ├── ignored.txt
// ├── subdir
// │ ├── included.txt
// │ └── sub.tmpl.go
// └── test.tmpl.go
//go:embed mytemplate/*
var mytemplate embed.FS
func main() {
// Define a new Template directory
basic, err := gosod.New(mytemplate)
if err != nil {
log.Fatal(err)
}
// Make some config data
myConfig := &config{
Name: "Mat",
}
// Ignore files
basic.IgnoreFile("ignored.txt")
// Custom template filters
basic.SetTemplateFilters([]string{ ".filtername", ".tmpl" })
// Create a new directory using the template and config
err = basic.Extract("./generated", myConfig)
if err != nil {
log.Fatal(err)
}
// Ouput FS:
// generated/
// ├── custom.txt
// ├── subdir
// │ ├── included.txt
// │ └── sub.go
// └── test.go
}
A template directory is simply a directory structure contianing files you wish to copy. The algorithm for copying is:
- Categorise all files into one of: directory, standard file and template files
- Create the directory structure
- Copy standard files
- Copy template files, assembled using the given data
Template files, by default, are any file with ".tmpl" in their filename. To change this, use SetTemplateFilters([]string)
. This allows you to set any number of filters.
Files may also be ignored by using the IgnoreFilename(string)
method.
Google is your friend