initial commit
commit
497437d860
|
@ -0,0 +1,29 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitea.code-infection.com/efertone/scaffolder/internal/template"
|
||||
)
|
||||
|
||||
type GoVariables struct {
|
||||
AppName string
|
||||
GoVersion string
|
||||
PackageRoot string
|
||||
}
|
||||
|
||||
func main() {
|
||||
tmpl := template.NewTemplate(
|
||||
"go",
|
||||
GoVariables{
|
||||
AppName: "myapp",
|
||||
GoVersion: "1.16",
|
||||
PackageRoot: "gitea.code-infection.com/efertone/myapp",
|
||||
},
|
||||
)
|
||||
|
||||
err := tmpl.Generate()
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
module gitea.code-infection.com/efertone/scaffolder
|
||||
|
||||
go 1.16
|
|
@ -0,0 +1,57 @@
|
|||
#file:go.mod
|
||||
module {{ .PackageRoot }}
|
||||
|
||||
go {{ .GoVersion }}
|
||||
#file:cmd/{{ .AppName }}/main.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("{{ .AppName }}")
|
||||
}
|
||||
#file:internal/README.md
|
||||
Private application and library code. This is the code you don't want others
|
||||
importing in their applications or libraries.
|
||||
#file:pkg/README.md
|
||||
Public libraries that's ok to use by external applications.
|
||||
#file:examples/README.md
|
||||
Examples for your applications and/or public libraries.
|
||||
#file:init/README.md
|
||||
System init (systemd, upstart, sysv) and process manager/supervisor (runit,
|
||||
supervisord) configs.
|
||||
#file:docs/README.md
|
||||
Design and user documents (in addition to your godoc generated documentation).
|
||||
#file:configs/README.md
|
||||
Configuration file templates or default configs.
|
||||
|
||||
Put your confd or consul-template template files here.
|
||||
#file:deployments/README.md
|
||||
IaaS, PaaS, system and container orchestration deployment configurations and
|
||||
templates (docker-compose, kubernetes/helm, mesos, terraform, bosh).
|
||||
#file:assets/README.md
|
||||
Other assets to go along with your repository (images, logos, etc).
|
||||
#file:api/README.md
|
||||
OpenAPI/Swagger specs, JSON schema files, protocol definition files.
|
||||
#file:scripts/README.md
|
||||
Scripts to perform various build, install, analysis, etc operations.
|
||||
|
||||
These scripts keep the root level Makefile small and simple.
|
||||
#file:.gitignore
|
||||
# Mac OS X files
|
||||
.DS_Store
|
||||
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, build with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
#file:README.md
|
||||
# {{ .AppName }}
|
|
@ -0,0 +1,70 @@
|
|||
package template
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"embed"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed built-in/*.scaffold
|
||||
var tmpl embed.FS
|
||||
|
||||
type File struct {
|
||||
Path string
|
||||
Content []byte
|
||||
}
|
||||
|
||||
type Template struct {
|
||||
Language string
|
||||
Variables interface{}
|
||||
}
|
||||
|
||||
func NewTemplate(language string, variables interface{}) Template {
|
||||
return Template{
|
||||
Language: language,
|
||||
Variables: variables,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Template) Generate() error {
|
||||
file, err := tmpl.Open(fmt.Sprintf("built-in/%s.scaffold", t.Language))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
files := []File{}
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
var current File
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
if strings.HasPrefix(line, "#file:") {
|
||||
if current.Path != "" {
|
||||
files = append(files, current)
|
||||
}
|
||||
|
||||
parts := strings.Split(line, ":")
|
||||
current = File{Path: parts[1], Content: []byte{}}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
current.Content = append(current.Content, []byte(line+"\n")...)
|
||||
}
|
||||
|
||||
if current.Path != "" {
|
||||
files = append(files, current)
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
fmt.Printf(">>> %s\n", f.Path)
|
||||
fmt.Printf("%s---\n", f.Content)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue