From 497437d86025356e5986edae62b9a8b884466fc2 Mon Sep 17 00:00:00 2001 From: Balazs Nadasdi Date: Thu, 24 Jun 2021 16:45:34 +0200 Subject: [PATCH] initial commit --- cmd/scaffolder/main.go | 29 +++++++++++ go.mod | 3 ++ internal/template/built-in/go.scaffold | 57 +++++++++++++++++++++ internal/template/main.go | 70 ++++++++++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 cmd/scaffolder/main.go create mode 100644 go.mod create mode 100644 internal/template/built-in/go.scaffold create mode 100644 internal/template/main.go diff --git a/cmd/scaffolder/main.go b/cmd/scaffolder/main.go new file mode 100644 index 0000000..4a2f648 --- /dev/null +++ b/cmd/scaffolder/main.go @@ -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()) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cf12279 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitea.code-infection.com/efertone/scaffolder + +go 1.16 diff --git a/internal/template/built-in/go.scaffold b/internal/template/built-in/go.scaffold new file mode 100644 index 0000000..2c5509f --- /dev/null +++ b/internal/template/built-in/go.scaffold @@ -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 }} diff --git a/internal/template/main.go b/internal/template/main.go new file mode 100644 index 0000000..7930e2a --- /dev/null +++ b/internal/template/main.go @@ -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 +}