# abhixdd/ghgrab architecture

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

- Interactive diagram: https://gitdiagram.com/abhixdd/ghgrab
- Repository: https://github.com/abhixdd/ghgrab

## Overview

1. **Project shape**
   - `ghgrab` is primarily a **Rust CLI application** that exposes one core binary and a library crate.
   - The repo also contains **distribution wrappers** for npm, Python, and Nix, but they all ultimately point to the same Rust implementation.

2. **Top-level command model**
   - The user-facing surface has three main modes:
     - **Interactive browsing** of GitHub repositories and files.
     - **Release download** for GitHub release assets.
     - **Agent mode** for machine-readable, non-interactive automation.
   - The README and file layout suggest these modes are routed from a shared CLI entrypoint into separate command handlers.

3. **Rust core boundary**
   - The Rust source under `src/` is the architectural center.
   - `src/lib.rs` likely exposes shared logic, while `src/main.rs` and `src/bin/ghg.rs` are executable entrypoints.
   - `src/cli.rs` is the command-line parsing layer that dispatches into higher-level operations.

4. **GitHub data access layer**
   - `src/github.rs` is the integration boundary to GitHub APIs and repo/release metadata.
   - This layer is responsible for repo discovery, repository tree enumeration, search, and release asset lookup.
   - Authentication is supported through saved config, `GHGRAB_GITHUB_TOKEN`, `GITHUB_TOKEN`, or one-shot `--token` overrides.

5. **Download and file retrieval pipeline**
   - `src/download.rs` is the file transfer boundary.
   - It handles downloading repository paths, folders, and release assets, plus special handling for larger or special-case content.
   - The README’s GitHub LFS support indicates this layer also resolves large-file download behavior, not just simple content fetches.

6. **Release selection subsystem**
   - `src/release.rs` is a dedicated workflow for GitHub releases.
   - It selects the best asset based on OS/architecture, optional regex, file type, prerelease rules, and user overrides.
   - When selection is ambiguous, the CLI falls back to an interactive choice flow rather than guessing silently.

7. **Interactive TUI subsystem**
   - `src/ui/` contains the terminal interface built on the `ratatui` ecosystem.
   - Components are split by responsibility: browser, input, preview, repo search, searching state, toast notifications, and syntax highlighting.
   - This UI is the main boundary between the user and the underlying repo/download logic, providing browsing, selection, preview, and keyboard-driven navigation.

8. **App state and orchestration**
   - `src/agent.rs` and the UI modules imply a shared application state model coordinating user input, navigation, search, preview, and download actions.
   - The TUI is not just presentation; it orchestrates transitions between home, repo search, browser, and preview states.

9. **Agent mode / automation boundary**
   - The `agent` command is a separate non-interactive API-like surface.
   - It prints a stable JSON envelope with `api_version`, `ok`, `command`, and either `data` or `error`, making it suitable for scripts, CI, and other agents.
   - This is a clean architectural boundary: human-oriented TUI vs machine-oriented structured output.

10. **Configuration and theming**
    - `src/config.rs` likely owns local settings such as download path and token persistence.
    - Theme customization is handled through TOML files, with defaults falling back to a built-in Tokyo Night palette via `src/ui/theme.rs`.
    - This separates runtime preferences from command execution and keeps the UI styling configurable without changing code.

11. **Packaging and runtime distribution**
    - The presence of `package.json`, `bin/ghgrab.js`, `setup.py`, `pyproject.toml`, and `scripts/install.js` shows the Rust binary is packaged for multiple ecosystems.
    - These wrappers are distribution-only layers; they do not define product behavior, but they materially affect how the binary is installed and launched.

12. **Testing and CI**
    - `tests/` covers CLI behavior, app state, and release selection, which aligns with the project’s main risk areas.
    - GitHub Actions workflows in `.github/workflows/` handle build and CI validation, reinforcing that the repo is organized around a single Rust core with packaging and release automation around it.

13. **How the main data flow works**
    - Input starts as either a repo URL, search term, or release target.
    - The CLI/agent layer parses the command, the GitHub layer resolves metadata, the UI may present choices, and the download layer writes files locally.
    - Configuration and tokens influence all of these stages, but the execution path remains centered on GitHub discovery → selection → download.

14. **Architecture summary for a diagram**
    - A good diagram should show one central **Rust core** with four major internal boundaries: **CLI/dispatch**, **GitHub API integration**, **download/release logic**, and **TUI/agent presentation**.
    - Around that core are **local config/theme storage**, **GitHub as the external service**, and **distribution wrappers** for npm/Python/Nix.

Main entry points: [`src/cli.rs`](https://github.com/abhixdd/ghgrab/blob/main/src/cli.rs) (Command router), [`src/ui/mod.rs`](https://github.com/abhixdd/ghgrab/blob/main/src/ui/mod.rs) (TUI shell), [`src/main.rs`](https://github.com/abhixdd/ghgrab/blob/main/src/main.rs) (Main CLI), [`src/github.rs`](https://github.com/abhixdd/ghgrab/blob/main/src/github.rs) (GitHub client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Rust core"]
  node_lib["Library core<br/>rust lib<br/>[lib.rs]"]
  node_main["Main CLI<br/>binary entry<br/>[main.rs]"]
  node_ghg["Ghg binary<br/>binary entry<br/>[ghg.rs]"]
  node_cli["Command router<br/>arg parsing<br/>[cli.rs]"]
  node_agent["Agent mode<br/>json api<br/>[agent.rs]"]
  node_config["Config store<br/>settings<br/>[config.rs]"]
  node_github["GitHub client<br/>api integration<br/>[github.rs]"]
  node_download["Download engine<br/>transfer pipeline<br/>[download.rs]"]
  node_release["Release selector<br/>selection logic<br/>[release.rs]"]
end

subgraph group_ui["Terminal UI"]
  node_ui_mod["TUI shell<br/>terminal ui<br/>[mod.rs]"]
  node_browser["Repo browser<br/>ui component<br/>[browser.rs]"]
  node_repo_search["Repo search<br/>ui component<br/>[repo_search.rs]"]
  node_preview["Preview pane<br/>ui component<br/>[preview.rs]"]
  node_input["Input field<br/>ui component<br/>[input.rs]"]
  node_theme["Theme system<br/>ui theme<br/>[theme.rs]"]
end

subgraph group_dist["Distribution"]
  node_dist_npm["NPM wrapper<br/>package wrapper<br/>[ghgrab.js]"]
  node_dist_py["Python package<br/>package wrapper<br/>[setup.py]"]
  node_dist_nix["Nix package<br/>package wrapper<br/>[flake.nix]"]
end

subgraph group_ops["Quality"]
  node_tests["Test suite<br/>validation"]
  node_ci["CI workflows<br/>automation<br/>[ci.yml]"]
end

node_main -->|"dispatches"| node_cli
node_ghg -->|"dispatches"| node_cli
node_cli -->|"loads"| node_config
node_cli -->|"queries"| node_github
node_cli -->|"invokes"| node_download
node_cli -->|"selects"| node_release
node_cli -->|"launches"| node_ui_mod
node_cli -->|"runs"| node_agent
node_agent -->|"uses"| node_github
node_agent -->|"uses"| node_download
node_ui_mod -->|"contains"| node_browser
node_ui_mod -->|"contains"| node_repo_search
node_ui_mod -->|"contains"| node_preview
node_ui_mod -->|"contains"| node_input
node_ui_mod -->|"styles"| node_theme
node_browser -->|"browses"| node_github
node_repo_search -->|"searches"| node_github
node_preview -->|"renders"| node_download
node_release -->|"inspects"| node_github
node_release -->|"chooses"| node_download
node_config -->|"configures"| node_theme
node_dist_npm -->|"wraps"| node_main
node_dist_py -->|"wraps"| node_main
node_dist_nix -->|"wraps"| node_main
node_tests -->|"covers"| node_cli
node_tests -->|"covers"| node_agent
node_tests -->|"covers"| node_release
node_ci -->|"runs"| node_tests
node_ci -->|"builds"| node_main

click node_lib "https://github.com/abhixdd/ghgrab/blob/main/src/lib.rs"
click node_main "https://github.com/abhixdd/ghgrab/blob/main/src/main.rs"
click node_ghg "https://github.com/abhixdd/ghgrab/blob/main/src/bin/ghg.rs"
click node_cli "https://github.com/abhixdd/ghgrab/blob/main/src/cli.rs"
click node_agent "https://github.com/abhixdd/ghgrab/blob/main/src/agent.rs"
click node_config "https://github.com/abhixdd/ghgrab/blob/main/src/config.rs"
click node_github "https://github.com/abhixdd/ghgrab/blob/main/src/github.rs"
click node_download "https://github.com/abhixdd/ghgrab/blob/main/src/download.rs"
click node_release "https://github.com/abhixdd/ghgrab/blob/main/src/release.rs"
click node_ui_mod "https://github.com/abhixdd/ghgrab/blob/main/src/ui/mod.rs"
click node_browser "https://github.com/abhixdd/ghgrab/blob/main/src/ui/components/browser.rs"
click node_repo_search "https://github.com/abhixdd/ghgrab/blob/main/src/ui/components/repo_search.rs"
click node_preview "https://github.com/abhixdd/ghgrab/blob/main/src/ui/components/preview.rs"
click node_input "https://github.com/abhixdd/ghgrab/blob/main/src/ui/components/input.rs"
click node_theme "https://github.com/abhixdd/ghgrab/blob/main/src/ui/theme.rs"
click node_dist_npm "https://github.com/abhixdd/ghgrab/blob/main/bin/ghgrab.js"
click node_dist_py "https://github.com/abhixdd/ghgrab/blob/main/setup.py"
click node_dist_nix "https://github.com/abhixdd/ghgrab/blob/main/flake.nix"
click node_tests "https://github.com/abhixdd/ghgrab/tree/main/tests"
click node_ci "https://github.com/abhixdd/ghgrab/blob/main/.github/workflows/ci.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_lib,node_main,node_ghg,node_cli,node_agent,node_config,node_github,node_download,node_release toneBlue
class node_ui_mod,node_browser,node_repo_search,node_preview,node_input,node_theme toneAmber
class node_dist_npm,node_dist_py,node_dist_nix toneMint
class node_tests,node_ci toneRose
```

## Components

### Rust core

Primary application logic and runtime orchestration

- [Library core](https://github.com/abhixdd/ghgrab/blob/main/src/lib.rs): rust lib · Shared crate entry for core logic · `src/lib.rs`
- [Main CLI](https://github.com/abhixdd/ghgrab/blob/main/src/main.rs): binary entry · Primary executable entrypoint · `src/main.rs`
- [Ghg binary](https://github.com/abhixdd/ghgrab/blob/main/src/bin/ghg.rs): binary entry · Secondary executable entrypoint · `src/bin/ghg.rs`
- [Command router](https://github.com/abhixdd/ghgrab/blob/main/src/cli.rs): arg parsing · Parses commands and dispatches modes · `src/cli.rs`
- [Agent mode](https://github.com/abhixdd/ghgrab/blob/main/src/agent.rs): json api · Non-interactive structured command surface · `src/agent.rs`
- [Config store](https://github.com/abhixdd/ghgrab/blob/main/src/config.rs): settings · Local config, tokens, and download settings · `src/config.rs`
- [GitHub client](https://github.com/abhixdd/ghgrab/blob/main/src/github.rs): api integration · Repo search, tree lookup, metadata, and auth · `src/github.rs`
- [Download engine](https://github.com/abhixdd/ghgrab/blob/main/src/download.rs): transfer pipeline · Fetches files, folders, assets, and LFS content · `src/download.rs`
- [Release selector](https://github.com/abhixdd/ghgrab/blob/main/src/release.rs): selection logic · Chooses release assets by platform and filters · `src/release.rs`

### Terminal UI

Interactive browsing and preview experience

- [TUI shell](https://github.com/abhixdd/ghgrab/blob/main/src/ui/mod.rs): terminal ui · Top-level interactive application shell · `src/ui/mod.rs`
- [Repo browser](https://github.com/abhixdd/ghgrab/blob/main/src/ui/components/browser.rs): ui component · Navigates repositories and file trees · `src/ui/components/browser.rs`
- [Repo search](https://github.com/abhixdd/ghgrab/blob/main/src/ui/components/repo_search.rs): ui component · Searches repositories and surfaces results · `src/ui/components/repo_search.rs`
- [Preview pane](https://github.com/abhixdd/ghgrab/blob/main/src/ui/components/preview.rs): ui component · Shows file and asset previews · `src/ui/components/preview.rs`
- [Input field](https://github.com/abhixdd/ghgrab/blob/main/src/ui/components/input.rs): ui component · Keyboard input and command entry · `src/ui/components/input.rs`
- [Theme system](https://github.com/abhixdd/ghgrab/blob/main/src/ui/theme.rs): ui theme · Color palette and styling defaults · `src/ui/theme.rs`

### Distribution

Packaging and installer wrappers

- [NPM wrapper](https://github.com/abhixdd/ghgrab/blob/main/bin/ghgrab.js): package wrapper · JavaScript install/launch wrapper · `bin/ghgrab.js`
- [Python package](https://github.com/abhixdd/ghgrab/blob/main/setup.py): package wrapper · Python packaging surface for the binary · `setup.py`
- [Nix package](https://github.com/abhixdd/ghgrab/blob/main/flake.nix): package wrapper · Nix-based build and install packaging · `flake.nix`

### Quality

Tests and CI validation

- [Test suite](https://github.com/abhixdd/ghgrab/tree/main/tests): validation · CLI, state, and release behavior tests · `tests`
- [CI workflows](https://github.com/abhixdd/ghgrab/blob/main/.github/workflows/ci.yml): automation · Build and continuous integration checks · `.github/workflows/ci.yml`

## Connections

- Main CLI → Command router: dispatches · Main executable hands off to command routing
- Ghg binary → Command router: dispatches · Secondary binary uses the same CLI layer
- Command router → Config store: loads · Reads settings and token sources
- Command router → GitHub client: queries · Resolves repositories, trees, and metadata
- Command router → Download engine: invokes · Starts file and asset retrieval
- Command router → Release selector: selects · Routes release-target commands into asset selection
- Command router → TUI shell: launches · Interactive commands enter the TUI shell
- Command router → Agent mode: runs · Agent commands produce structured output
- Agent mode → GitHub client: uses · Automation still relies on GitHub metadata access
- Agent mode → Download engine: uses · Agent mode can trigger downloads
- TUI shell → Repo browser: contains · Browser state is part of the interactive shell
- TUI shell → Repo search: contains · Repo search is a core UI mode
- TUI shell → Preview pane: contains · Previewing is integrated into the shell
- TUI shell → Input field: contains · Keyboard input drives UI navigation
- TUI shell → Theme system: styles · Shell uses the theme system for presentation
- Repo browser → GitHub client: browses · Fetches and navigates repository content
- Repo search → GitHub client: searches · Finds repositories through GitHub APIs
- Preview pane → Download engine: renders · Preview content is sourced from downloaded data
- Release selector → GitHub client: inspects · Finds and filters release assets and metadata
- Release selector → Download engine: chooses · Selected asset proceeds into transfer pipeline
- Config store → Theme system: configures · Local settings can override UI appearance
- NPM wrapper → Main CLI: wraps · NPM installer launches the Rust binary
- Python package → Main CLI: wraps · Python packaging points at the Rust executable
- Nix package → Main CLI: wraps · Nix build/install targets the Rust binary
- Test suite → Command router: covers · Validates command behavior
- Test suite → Agent mode: covers · Checks JSON envelope behavior
- Test suite → Release selector: covers · Verifies release asset selection
- CI workflows → Test suite: runs · Workflows execute the validation suite
- CI workflows → Main CLI: builds · CI builds the primary binary
