# meian/rev-callgraph architecture

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

- Interactive diagram: https://gitdiagram.com/meian/rev-callgraph
- Repository: https://github.com/meian/rev-callgraph

## Overview

##### Project type and purpose
- rev-callgraph is a command-line tool written in Go.
- It analyzes Go code in a workspace and produces a reverse call graph for a specified function or method.

##### File structure
- Top-level:
- cmd/ and main.go – CLI entrypoint and command definitions (cobra-style).
- internal/ – core packages not exposed externally:
- astquery – AST‐based caller extraction
- callgraph – reverse call graph traversal
- format – output serializers (tree, JSON, dot)
- gomod – module scanning and workspace resolution
- grep – text-based search (fallback or test)
- symbol – symbol parsing and representation
- testdata/ – sample Go modules and code for e2e tests
- e2e_test.go – end-to-end test harness
- config/devcontainer, .github, Makefile, etc. – CI/dev env support

##### README for additional insights
- Describes CLI flags, supported output formats, examples.
- No pre-drawn architecture but clarifies the core flow: directory scan → parsing → graph build → formatting.

a. Main components:
1. CLI Frontend
2. Workspace Scanner (gomod)
3. AST Query Engine (astquery + symbol)
4. Call Graph Analyzer (callgraph)
5. Formatter (format)
6. File System / Go Toolchain (external)
b. Relationships & data flow:
- User invokes CLI → parses flags/target.
- CLI calls Workspace Scanner to locate modules and package roots.
- For each package, invoke AST Query Engine to locate call sites and build symbol table.
- Feed AST‐based results into Call Graph Analyzer for reverse traversal up to max-depth.
- Pass final graph structure into Formatter for desired output (tree, JSON, dot).
- Formatter writes to STDOUT (or file).
c. Architectural patterns/design principles:
- Separation of concerns – each internal/ package has a single responsibility.
- Layered pipeline – scanner → parser → analyzer → serializer.
- Pure Go, no external services or databases.
d. Key technologies/frameworks:
- Go 1.24+ language and standard library (go/parser, ast, etc.)
- Cobra/Viper style CLI in cmd/ (implicit from root.go)
- JSON/DOT serialization in internal/format
- Go modules for workspace discovery

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "I/O & CLI Layer"
        CLI["CLI Frontend"]:::io
        Formatter["Formatter"]:::io
    end

    subgraph "Core Library"
        Scanner["Workspace Scanner"]:::core
        AST["AST Query Engine"]:::core
        Symbol["Symbol Parser"]:::core
        CGAnalyzer["Call Graph Analyzer"]:::core
    end

    subgraph "External Resources"
        FS["File System & Go Toolchain"]:::external
        STDOUT["STDOUT"]:::external
    end

    CLI -->|"flags/target"| Scanner
    Scanner -->|"package list"| AST
    Symbol -->|"symbols"| AST
    AST -->|"AST nodes"| CGAnalyzer
    CGAnalyzer -->|"reverse graph"| Formatter
    Formatter -->|"serialized output"| STDOUT
    FS -->|"source code"| Scanner

    click CLI "https://github.com/meian/rev-callgraph/blob/main/cmd/root.go"
    click CLI "https://github.com/meian/rev-callgraph/blob/main/main.go"
    click Scanner "https://github.com/meian/rev-callgraph/blob/main/internal/gomod/module.go"
    click Scanner "https://github.com/meian/rev-callgraph/blob/main/internal/gomod/scan.go"
    click AST "https://github.com/meian/rev-callgraph/blob/main/internal/astquery/astquery.go"
    click Symbol "https://github.com/meian/rev-callgraph/blob/main/internal/symbol/parse.go"
    click Symbol "https://github.com/meian/rev-callgraph/blob/main/internal/symbol/function.go"
    click Symbol "https://github.com/meian/rev-callgraph/blob/main/internal/symbol/callnode.go"
    click CGAnalyzer "https://github.com/meian/rev-callgraph/blob/main/internal/callgraph/callgraph.go"
    click Formatter "https://github.com/meian/rev-callgraph/blob/main/internal/format/tree.go"
    click Formatter "https://github.com/meian/rev-callgraph/blob/main/internal/format/json.go"
    click Formatter "https://github.com/meian/rev-callgraph/blob/main/internal/format/dot.go"
    click Formatter "https://github.com/meian/rev-callgraph/blob/main/internal/format/printer.go"
    click STDOUT "https://github.com/meian/rev-callgraph/blob/main/e2e_test.go"
    click STDOUT "https://github.com/meian/rev-callgraph/tree/main/testdata/"

    classDef external fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef core fill:#90EE90,stroke:#000,stroke-width:1px
    classDef io fill:#FFA500,stroke:#000,stroke-width:1px
```
