# 0xdones/tfgen architecture

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

- Interactive diagram: https://gitdiagram.com/0xdones/tfgen
- Repository: https://github.com/0xdones/tfgen

## Overview

##### Main Components

a. Input Layer:
- Configuration Files (.tfgen.yaml)
- Command Line Interface

b. Core Processing Components:
- Config Parser/Merger
- Template Engine
- File System Handler

c. Output Layer:
- Generated Terraform Files

##### Component Relationships and Flow

Use the following structure to show the data flow:

**[CLI Commands] -----> [File System Scanner]**

- v
**[Config Files] -----> [Config Parser/Merger]**

- v
**[Templates] ---------> [Template Engine]**

- v
**[File System Handler] ---> [Generated Files]**

##### Important Details

a. Configuration Flow:
- How multiple .tfgen.yaml files are merged (root and environment-specific)
- Precedence (inner config over outer config)

b. Template Processing:
- Injection of built-in variables (tfgen_state_key)
- Template variable substitution

c. File Generation:
- Output of standard files (_backend.tf, _provider.tf, _vars.tf)

##### Key Architectural Elements

- cmd/ - Command handling (clean.go, exec.go)
- tfgen/ - Core logic (config.go, finder.go, fs.go, handler.go)
- Main orchestration (main.go)

##### Additional Notes

- File naming conventions
- Configuration hierarchy
- Variable precedence
- Template syntax

##### Implementation Details

How the following key files interact:
- finder.go: Configuration file discovery
- config.go: Configuration parsing and merging
- handler.go: Template processing
- fs.go: File system operations

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Input
        CLI["CLI Interface"]
        CFG[("Configuration Files<br/>(.tfgen.yaml)")]:::config
        TPL[("Template Files")]:::template
    end

    subgraph Processing
        FSS["File System Scanner"]:::processor
        CFP["Config Parser/Merger"]:::processor
        TEP["Template Engine"]:::processor
        FSH["File System Handler"]:::processor
    end

    subgraph Output
        GF["Generated Terraform Files"]:::output
        subgraph StandardFiles
            BF["_backend.tf"]:::output
            PF["_provider.tf"]:::output
            VF["_vars.tf"]:::output
        end
    end

    CLI --> FSS
    CFG --> FSS
    FSS --> CFP
    CFP --> TEP
    TPL --> TEP
    TEP --> FSH
    FSH --> GF
    GF --> StandardFiles
    
    classDef input fill:#a8d1ff,stroke:#333,stroke-width:2px
    classDef processor fill:#90EE90,stroke:#333,stroke-width:2px
    classDef output fill:#FFB347,stroke:#333,stroke-width:2px
    classDef config fill:#B19CD9,stroke:#333,stroke-width:2px
    classDef template fill:#87CEEB,stroke:#333,stroke-width:2px

    click CLI "https://github.com/0xDones/tfgen/tree/main/cmd/"
    click FSS "https://github.com/0xDones/tfgen/blob/main/tfgen/finder.go"
    click CFP "https://github.com/0xDones/tfgen/blob/main/tfgen/config.go"
    click FSH "https://github.com/0xDones/tfgen/blob/main/tfgen/fs.go"
    click TEP "https://github.com/0xDones/tfgen/blob/main/tfgen/handler.go"

    subgraph Legend
        L1["Input Components"]:::input
        L2["Processing Components"]:::processor
        L3["Output Components"]:::output
        L4["Configuration Files"]:::config
        L5["Template Files"]:::template
    end
```
