Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeGen - Go Template-based Code Generator

Overview

CodeGen is a versatile command-line tool designed to streamline file generation using Go templates and JSON data. Originally built to facilitate the creation of Protocol Buffers files, CodeGen is sufficiently general to support the generation of any structured text files given the appropriate template and data combination.

Features

  • General-Purpose Code Generation: Not limited to Protocol Buffers, you can use CodeGen to generate any file type that can be templated using Go's text/template package.
  • Template Flexibility: Utilize Go templates to define your output formats precisely.
  • Data-driven: Feed your templates with JSON data allowing complex file outputs.

Installation

Clone the repository and build the project:

git clone https://github.com/ProImpact/cgen.git
cd cgen
make build

Usage

Generating a Single File

Use the gen command to generate output from a single template and JSON data input.

./cgen gen --input-data='<json_string>' --template-file <template_file_path> --output-file <output_file_path>
  • --input-data: JSON string containing variables for the template.
  • --template-file: Path to the Go template file.
  • --output-file: Destination path for the generated file.

Generating Multiple Files

Use the wgen command to execute multiple templates from a directory against a JSON data file.

./cgen wgen --dir=<template_directory> --data=<json_data_path> --template-name=<template_name> --output-file=<output_path>
  • --dir: Directory containing template files.
  • --data: Path to the JSON file with data for the templates.
  • --template-name: Specific template file in the directory to execute.
  • --output-file: Path for the final generated file.
  • --pattern: (Optional) Pattern to match the template files.

Considerations

  • Ensure that your JSON data structure matches the template variables for successful file generation.
  • Extensive error messages and logging are provided to guide you in case of invalid inputs or template processing errors.
  • Recommended Go version is 1.25.0 as specified in go.mod.

This tool enables rapid file generation tailored to your project's specific needs, providing a robust framework to support various generation scenarios.

About

Code generation with Golang templates

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages