# gruntwork-io/boilerplate architecture

> Architecture diagram of the GitHub repository gruntwork-io/boilerplate, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gruntwork-io/boilerplate
- Repository: https://github.com/gruntwork-io/boilerplate

## Overview

##### Project Type and Purpose
- This project is a command-line tool used to generate “boilerplate” files and folders from templates.
- Its main purpose is to scaffold out projects, update code files with snippets, and insert variables into file templates using the Go Template engine (with experimental support for Jsonnet).
- The README emphasizes interactive and non-interactive modes, dependency chaining between templates, hooks before and after rendering, and variable validations.

##### Main Components
a. User Interface & Input
– CLI front-end: Users interact via command-line flags (for example, --template-url, --output-folder, --var, etc.).
– Environment Variables: Users can pass variable values via environment (e.g. BOILERPLATE_Title).
b. Application Entry Point
– The main.go file initializes the application and routes control to the core modules.
c. Configuration & Variable Management
– Configuration Module: Reads and validates the boilerplate.yml file (from the “config” folder and test fixtures).
– Variables Module: Manages variable definitions, default values, validations, and variable resolution (see “variables” directory).
d. Template & Rendering Engine
– Rendering Module: Processes templates using the standard Go Template engine (and also sprig plus custom helpers).
– Alternative Engine: Includes the experimental Jsonnet support (as defined in the render engine and JSON parsing files).
e. Dependency & Hooks Management
– Dependency Processor: Recursively triggers additional templates if dependencies are defined (as seen in the “config” and test examples).
– Hook Executor: Runs before and after hooks (configuration parsed from boilerplate.yml) to execute arbitrary shell commands.
f. File Handling & Output Generation
– File Copier: Copies files from the template source to the output folder.
– Template Processor: Applies the templating engine to non-binary files and passes through files unchanged if needed.
g. Auxiliary Helpers and Utilities
– Logging/Errors Module: In the “errors” and “util” directories, for tracking problems during execution.
– Getter and Options Helpers: The “getter-helper” and “options” modules support file retrieval and option parsing.
– Testing/Integration: The “integration-tests” and “test-fixtures” directories are used for CI and regression testing but could be shown as support tools rather than core runtime components.

##### Relationships and Data Flow
– At the top, “User Input” (command-line flags, environment variables) feeds directly into Main (main.go).
– The Main process calls the CLI module which does initial option parsing.
– This leads to the Configuration Module which reads “boilerplate.yml” and loads variable definitions; the Variables Module then resolves user input, defaults, and validations.
– The Dependency Manager uses resolved variables to determine if additional templates (or “dependencies”) should be processed.
– Before rendering, the Hooks Executor runs any “before” commands.
– The Rendering Engine takes control next: it uses the Go Template (or Jsonnet if matched by “engines”) plus custom helpers (for snippets, includes, etc.) to process files.
– The file I/O layer (File Copier/Template Processor) pulls in the static files from the template folder and applies templating as needed to produce the final output in the output folder.
– After rendering, the Hooks Executor runs “after” hook commands.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Clearly separate the CLI, configuration/variable handling, template rendering, dependency management, and hook execution.
- Modular Design: Each folder (cli, config, render, variables, util) represents a distinct module that interacts via well-defined methods.
- Extensibility: The dependency and hooks mechanism allows recursive template invocation and custom behavior.
- Plug-in Like Behavior: The support for alternative templating (Jsonnet) and template helpers shows a plugin‐style architecture.
- Testing Layer: Integration tests and test fixtures ensure that all components can be verified independently.

##### Elements
– Rectangular nodes for each major component:
- User Input/CLI
- Main/Application Entrypoint (main.go)
- Configuration Module (boilerplate.yml parser)
- Variables/Validation Module
- Dependency Manager (supports “for_each” loops, conditional dependencies)
- Hook Executor (Before and After hooks)
- Template Engine (Go Template + alternative Jsonnet support)
- File Copier/Output Generator
- Logging/Error module (supporting utilities)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Flow
    CLI["User Input/CLI"]:::cli
    MAIN["Main (main.go)"]:::cli
    CONFIG["Configuration Module"]:::core
    VARS["Variables/Validation Module"]:::core
    DEP["Dependency Manager"]:::dependency
    HOOK_BEFORE["Hooks Executor (Before)"]:::dependency
    GO_TMPL["Go Template Engine"]:::engine
    JSONNET["Experimental Jsonnet Support"]:::engine
    MERGE["Rendered Template"]:::engine
    FILE_OUT["File Handling/Output Generation"]:::fileio
    HOOK_AFTER["Hooks Executor (After)"]:::dependency
    FINAL["Final Output"]:::fileio

    %% Auxiliary Modules Subgraph
    subgraph "Auxiliary Modules"
        LOG["Logging/Errors Module"]:::aux
        GETOPT["Getter & Options Helpers"]:::aux
        AUX["Auxiliary Utilities"]:::aux
    end

    %% Testing Subgraph
    subgraph "Testing & Integration"
        TEST["Integration Tests & Test Fixtures"]:::test
    end

    %% Flow Connections
    CLI -->|"start"| MAIN
    MAIN -->|"load-config"| CONFIG
    CONFIG -->|"validate-variables"| VARS
    VARS -->|"resolve-dependencies"| DEP
    DEP -->|"execute-before-hooks"| HOOK_BEFORE
    HOOK_BEFORE -->|"trigger"| GO_TMPL
    HOOK_BEFORE -->|"trigger"| JSONNET
    GO_TMPL --> MERGE
    JSONNET --> MERGE
    MERGE -->|"apply-templates"| FILE_OUT
    FILE_OUT -->|"execute-after-hooks"| HOOK_AFTER
    HOOK_AFTER -->|"generate-output"| FINAL

    %% Auxiliary Support Connections (dashed)
    MAIN ---|"log-errors"| LOG
    CLI ---|"parse-options"| GETOPT
    DEP ---|"support"| AUX
    FINAL ---|"validate-output"| TEST

    %% Click Events
    click CLI "https://github.com/gruntwork-io/boilerplate/blob/main/cli/boilerplate_cli.go"
    click MAIN "https://github.com/gruntwork-io/boilerplate/blob/main/main.go"
    click CONFIG "https://github.com/gruntwork-io/boilerplate/blob/main/config/config.go"
    click VARS "https://github.com/gruntwork-io/boilerplate/blob/main/variables/variables.go"
    click VARS "https://github.com/gruntwork-io/boilerplate/blob/main/variables/types.go"
    click DEP "https://github.com/gruntwork-io/boilerplate/blob/main/variables/dependencies.go"
    click HOOK_BEFORE "https://github.com/gruntwork-io/boilerplate/blob/main/variables/hooks.go"
    click HOOK_AFTER "https://github.com/gruntwork-io/boilerplate/blob/main/variables/hooks.go"
    click GO_TMPL "https://github.com/gruntwork-io/boilerplate/blob/main/render/render_template.go"
    click GO_TMPL "https://github.com/gruntwork-io/boilerplate/blob/main/render/template_helpers.go"
    click JSONNET "https://github.com/gruntwork-io/boilerplate/blob/main/render/render_jsonnet.go"
    click FILE_OUT "https://github.com/gruntwork-io/boilerplate/blob/main/templates/template_processor.go"
    click FILE_OUT "https://github.com/gruntwork-io/boilerplate/blob/main/templates/skip_files_processor.go"
    click LOG "https://github.com/gruntwork-io/boilerplate/blob/main/errors/errors.go"
    click LOG "https://github.com/gruntwork-io/boilerplate/blob/main/util/logger.go"
    click GETOPT "https://github.com/gruntwork-io/boilerplate/blob/main/getter-helper/getter_helper.go"
    click GETOPT "https://github.com/gruntwork-io/boilerplate/blob/main/options/options.go"
    click AUX "https://github.com/gruntwork-io/boilerplate/blob/main/util/file.go"
    click AUX "https://github.com/gruntwork-io/boilerplate/blob/main/util/prompt.go"
    click AUX "https://github.com/gruntwork-io/boilerplate/blob/main/util/shell.go"
    click TEST "https://github.com/gruntwork-io/boilerplate/tree/main/integration-tests/"
    click TEST "https://github.com/gruntwork-io/boilerplate/tree/main/test-fixtures/"

    %% Class Definitions
    classDef cli fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef core fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef dependency fill:#FFA500,stroke:#000,stroke-width:1px;
    classDef engine fill:#DDA0DD,stroke:#000,stroke-width:1px;
    classDef fileio fill:#FFFF99,stroke:#000,stroke-width:1px;
    classDef aux fill:#E0E0E0,stroke:#000,stroke-width:1px;
    classDef test fill:#FFC0CB,stroke:#000,stroke-width:1px;
```
