https://github.com/ksrichard/gogen
Powerful dynamic template based generation
https://github.com/ksrichard/gogen
archetype-generator generator project template utility
Last synced: about 1 year ago
JSON representation
Powerful dynamic template based generation
- Host: GitHub
- URL: https://github.com/ksrichard/gogen
- Owner: ksrichard
- License: apache-2.0
- Created: 2021-01-20T12:27:31.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-15T09:20:23.000Z (over 5 years ago)
- Last Synced: 2024-05-01T13:29:11.712Z (about 2 years ago)
- Topics: archetype-generator, generator, project, template, utility
- Language: Go
- Homepage:
- Size: 391 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README

[](https://goreportcard.com/report/github.com/ksrichard/gogen)
[](https://pkg.go.dev/github.com/ksrichard/gogen)
[](https://github.com/ksrichard/gogen)
[](https://github.com/ksrichard/gogen/releases/latest/)
Code coverage: https://gocover.io/github.com/ksrichard/gogen
# gogen
A simple CLI and library to generate any kind of projects from any kind of templates! (just like maven archetype, but its much easier)
This CLI tool can be a very good replacement of `sed` commands when need to replace several values in configuration files.
There is absolutely no limitation in programming language or whatever you want to generate from a file and directory based template!
## Features
- Generating output from any kind of folder/file structure
- Uses the famous [Mustache](https://mustache.github.io/) templating engine for directory name, file name, file content generation
## Options
```
./gogen generate --help
Generating projects/folder structures based on a template
Usage:
gogen generate [flags]
Flags:
-h, --help help for generate
-i, --input string Template folder/file to be used for generation
-o, --output string Output folder/file where result will be generated
Not applicable when output type is 'stdout'!
-t, --output-type string Output type - file,folder,stdout.
Not applicable when input is a folder! (default "folder")
-v, --vars stringToString Variables for generation (default [])
-f, --vars-file stringArray Variables from file (YAML) for generation
```
You can use everything in directory/file names and files content that can be done in [Mustache](https://mustache.github.io/) (for loops etc...)
**Note**: Here when you pass any variables to the generator, they will be used for generating both folder/file names and contents of files!
**Note**: When you use multiple sources for variables (more files and/or inline parameters), the order of the overwriting values is the following:
- all YAML files in order
- all command line parameters
## Installation
You can install the CLI using homebrew (or local build, see next section):
```
brew tap ksrichard/tap
brew install gogen
```
## Example usages of CLI
- Build the CLI using the following command executed in root of the project: `go build`
**Note:** You can combine any of the input/output, output types parameters to fit to your needs of course!
- Generate a project/folder structure:
```
./gogen generate -i examples/simple-project/ -o output/GoTest -v module=GoTest -v project_name="Go Test"
```
See the output folder that was generated (and also check for file contents):

- Generate single file:
```
./gogen generate -i ./examples/simple-project/main.go -t stdout -v project_name="Test Project"
```
- Generate single file to file output:
```
./gogen generate -i ./examples/simple-project/main.go -t file -o test.go -v project_name="Test Project"
```
- Generate using variables from file, template from pipe:
```bash
echo "{{testRootValue}} -->> {{#projects}}\"{{name}},{{url}} -> {{#data}}{{value}}{{/data}}\" {{/projects}}" | ./gogen generate -t stdout -f ./examples/test.yaml
```
- Generate using variables from file (properties overwrite from another file), template from pipe:
```bash
echo "{{testRootValue}} -->> {{#projects}}\"{{name}},{{url}} -> {{#data}}{{value}}{{/data}}\" {{/projects}}" | ./gogen generate -t stdout -f ./examples/test.yaml -f ./examples/test2.yaml
```
- Generate using variables from file (properties overwrite from command line option), template from pipe:
```bash
echo "{{testRootValue}} -->> {{#projects}}\"{{name}},{{url}} -> {{#data}}{{value}}{{/data}}\" {{/projects}}" | ./gogen generate -t stdout -f ./examples/test.yaml -v testRootValue="someTest2"
```
- Generate using variables from file (properties overwrite from command line option), template from pipe:
```bash
echo "{{testRootValue}} -->> {{#projects}}\"{{name}},{{url}} -> {{#data}}{{value}}{{/data}}\" {{/projects}}" | ./gogen generate -t stdout -f ./examples/test.yaml -v testRootValue="someTest2"
```
## Example usage of Library
```go
package main
import (
gogen "github.com/ksrichard/gogen/service"
)
func main() {
err := gogen.Generate("examples/simple-project", "output/GoTest", "folder", map[string]interface{
"module": "GoTest",
"project_name": "Go Test",
})
if err != nil {
panic(err)
}
}
```
## TODO (upcoming features)
- [x] Support input from stdin
- [x] Generate result from single file template
- [x] Add support to print result instead of writing to file/output directory
- [x] Support more complex variables than just simple string key -> value ones