# twpayne/chezmoi architecture

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

- Interactive diagram: https://gitdiagram.com/twpayne/chezmoi
- Repository: https://github.com/twpayne/chezmoi

## Overview

a. Input Layer:
- User Commands (add, apply, init, etc.)
- Configuration Files
- Source Files
- Templates

b. Processing Layer:
- Template Processing
- Script Execution
- State Management
- Encryption/Decryption

c. Output Layer:
- Target Files
- Status Reports
- Diffs
- Logs

##### Key Architectural Patterns to Highlight

a. Command Pattern:
- How different commands (in internal/cmd) interact with the core system
- Command flow from CLI to execution

b. State Management:
- Source State
- Target State
- Persistent State
- State Transformation Flow

##### System Boundaries

a. Internal Components:
- Core Logic (internal/chezmoi)
- Command Handling (internal/cmd)
- Template Functions
- Encryption Handlers

b. External Boundaries:
- File System Interface
- External API Connections
- Script Execution Environment

##### Technical Details

- Go-based Architecture
- File System Abstraction
- Template Engine Integration
- Encryption Providers
- External Tool Integration

##### Specific Implementation Notes

- How the system handles different operating systems
- Template processing flow
- Demonstrate encryption/decryption pathways
- Configuration management flow

##### The file system abstraction layer

This will provide a comprehensive view of chezmoi's architecture while maintaining clarity and showing the relationships between components.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Core["Core System"]
        SS["Source State Manager"]
        TS["Target State Manager"]
        TE["Template Engine"]
        EL["Encryption Layer"]
        PS["Persistent State"]
    end

    subgraph CMD["Command Processing"]
        CH["Command Handler"]
        CI["Command Implementation"]
    end

    subgraph EXT["External Integrations"]
        subgraph PM["Password Managers"]
            OP["1Password"]
            BW["Bitwarden"]
            LP["LastPass"]
            KP["KeepassXC"]
        end
        
        subgraph CS["Cloud Services"]
            VT["Vault"]
            AWS["AWS Secrets"]
            AZ["Azure KeyVault"]
        end
        
        GI["Git Integration"]
    end

    subgraph SI["System Interface"]
        RS["Real System"]
        DS["Dry Run System"]
        DBS["Debug System"]
    end

    subgraph TP["Template Processing"]
        TF["Template Functions"]
        TPR["Template Processing"]
    end

    subgraph AR["Archive Support"]
        AH["Archive Handler"]
        AR["Archive Reader"]
    end

    CH --> SS
    CH --> TS
    CI --> CH
    SS --> TE
    TS --> TE
    TE --> EL
    TE --> TPR
    TPR --> TF
    TF --> PM
    TF --> CS
    SS --> PS
    TS --> PS
    SS --> SI
    TS --> SI
    AH --> SI
    AR --> SI
    GI --> SS

    %% Click events for component mapping
    click SS "https://github.com/twpayne/chezmoi/blob/master/internal/chezmoi/sourcestate.go"
    click TS "https://github.com/twpayne/chezmoi/blob/master/internal/chezmoi/targetstateentry.go"
    click TE "https://github.com/twpayne/chezmoi/blob/master/internal/chezmoi/template.go"
    click EL "https://github.com/twpayne/chezmoi/blob/master/internal/chezmoi/ageencryption.go"
    click CH "https://github.com/twpayne/chezmoi/blob/master/internal/cmd/cmd.go"
    click CI "https://github.com/twpayne/chezmoi/tree/master/internal/cmd/"
    click OP "https://github.com/twpayne/chezmoi/blob/master/internal/cmd/onepasswordtemplatefuncs.go"
    click BW "https://github.com/twpayne/chezmoi/blob/master/internal/cmd/bitwardentemplatefuncs.go"
    click LP "https://github.com/twpayne/chezmoi/blob/master/internal/cmd/lastpasstemplatefuncs.go"
    click KP "https://github.com/twpayne/chezmoi/blob/master/internal/cmd/keepassxctemplatefuncs.go"
    click GI "https://github.com/twpayne/chezmoi/blob/master/internal/chezmoigit/chezmoigit.go"
    click VT "https://github.com/twpayne/chezmoi/blob/master/internal/cmd/vaulttemplatefuncs.go"
    click AWS "https://github.com/twpayne/chezmoi/blob/master/internal/cmd/awssecretsmanagertemplatefuncs.go"
    click AZ "https://github.com/twpayne/chezmoi/blob/master/internal/cmd/azurekeyvaulttemplatefuncs.go"
    click PS "https://github.com/twpayne/chezmoi/blob/master/internal/chezmoi/persistentstate.go"
    click RS "https://github.com/twpayne/chezmoi/blob/master/internal/chezmoi/realsystem.go"
    click DS "https://github.com/twpayne/chezmoi/blob/master/internal/chezmoi/dryrunsystem.go"
    click DBS "https://github.com/twpayne/chezmoi/blob/master/internal/chezmoi/debugsystem.go"
    click TF "https://github.com/twpayne/chezmoi/blob/master/internal/cmd/templatefuncs.go"
    click TPR "https://github.com/twpayne/chezmoi/blob/master/internal/chezmoi/template.go"
    click AH "https://github.com/twpayne/chezmoi/blob/master/internal/chezmoi/archive.go"
    click AR "https://github.com/twpayne/chezmoi/blob/master/internal/chezmoi/archivereadersystem.go"

    classDef core fill:#e1f5fe,stroke:#01579b
    classDef cmd fill:#fff3e0,stroke:#ff6f00
    classDef ext fill:#f3e5f5,stroke:#7b1fa2
    classDef si fill:#e8f5e9,stroke:#2e7d32
    classDef tp fill:#fce4ec,stroke:#c2185b
    classDef ar fill:#ede7f6,stroke:#4527a0

    class SS,TS,TE,EL,PS core
    class CH,CI cmd
    class OP,BW,LP,KP,VT,AWS,AZ,GI ext
    class RS,DS,DBS si
    class TF,TPR tp
    class AH,AR ar
```
