# superfly/flyctl architecture

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

- Interactive diagram: https://gitdiagram.com/superfly/flyctl
- Repository: https://github.com/superfly/flyctl

## Overview

##### Main Components (Top Level)

a) Command Layer
- Main command groups (apps, auth, deploy, machine, etc.)
- Use a hierarchical structure to show command organization

b) Core Services Layer
- Internal services like config management, logging, metrics, and authentication
- Agent system for machine management

c) Integration Layer
- External services integration (Docker, Wireguard, PostgreSQL, Redis)
- API clients and networking components

##### Key Architectural Elements

a) Command Processing Pipeline
- Flow from command input → parsing → execution → output
- Context management system
- Error handling pathways

b) Configuration Management
- Relationship between:
- Local config files (fly.toml)
- Environment variables
- Remote configuration
- Secrets management

c) Build and Deploy System
- Components for:
- Image source handling
- Build processes
- Deployment strategies
- Machine management

##### Important Subsystems

a) Authentication System
- Auth flow
- Token management
- Session handling

b) Networking Layer
- WireGuard integration
- Proxy management
- SSH connectivity

c) Monitoring and Logging
- Metrics collection
- Log management
- Status reporting

##### Specific Implementation Details

- CLI interface components
- Business logic
- Infrastructure integration

b) Files and packages:
- /internal/command/* (command implementations)
- /internal/config/* (configuration management)
- /agent/* (machine management)

##### Component Relationships

- Clear dependencies between components
- Optional vs. required connections
- Extension points for plugins

##### Notes Section

Annotations for:
- Key design decisions
- Critical paths
- Security boundaries
- Performance considerations

Remember to emphasize:
- The command-line interface as the primary entry point
- The extensive plugin architecture
- The integration with fly.io's infrastructure
- The security and authentication mechanisms
- The configuration management system

This system design should help developers understand both the high-level architecture and the detailed interactions between components, while maintaining clarity about the project's core purpose as a CLI tool for managing fly.io resources.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph CLI["Command Layer"]
        CMD[Command Processing]:::cmd
        APPS["Apps Commands"]:::cmd
        AUTH["Auth Commands"]:::cmd
        DEPLOY["Deploy Commands"]:::cmd
        MACH["Machine Commands"]:::cmd
        
        CMD --> APPS
        CMD --> AUTH
        CMD --> DEPLOY
        CMD --> MACH
    end

    subgraph CORE["Core Services Layer"]
        CONFIG[Configuration Management]:::core
        LOG[Logging System]:::core
        METRICS[Metrics System]:::core
        APPCONF[App Configuration]:::core
    end

    subgraph BUILD["Build System"]
        IMGSRC[Image Source Handler]:::build
        BUILDKIT[Build Configuration]:::build
        DOCKER[Docker Integration]:::build
    end

    subgraph AUTHSYS["Authentication System"]
        AUTHIMPL[Auth Implementation]:::auth
        TOKEN[Token Management]:::auth
        WEBAUTH[Web Authentication]:::auth
    end

    subgraph AGENT["Agent System"]
        AGCORE[Agent Core]:::agent
        AGSRV[Agent Server]:::agent
        AGCMD[Agent Commands]:::agent
    end

    subgraph NET["Networking Layer"]
        WG[WireGuard]:::net
        PROXY[Proxy Management]:::net
        SSH[SSH Implementation]:::net
    end

    subgraph MON["Monitoring and Logging"]
        LOGIMPL[Logging Implementation]:::mon
        METCOL[Metrics Collection]:::mon
        STATUS[Status Logging]:::mon
    end

    subgraph CLIIF["CLI Interface"]
        MAINCLI[Main CLI]:::cli
        CMDCTX[Command Context]:::cli
        IO[IO Streams]:::cli
    end

    %% Core relationships
    MAINCLI --> CMD
    CMD --> CONFIG
    CMD --> LOG
    DEPLOY --> BUILD
    AUTH --> AUTHSYS
    MACH --> AGENT
    AGENT --> NET
    CORE --> MON

    %% Style definitions
    classDef cmd fill:#2374ab
    classDef core fill:#2a9d8f
    classDef build fill:#e9c46a
    classDef auth fill:#f4a261
    classDef agent fill:#e76f51
    classDef net fill:#264653
    classDef mon fill:#8ab17d
    classDef cli fill:#457b9d

    %% Click events
    click CMD "https://github.com/superfly/flyctl/blob/master/internal/command/command.go"
    click APPS "https://github.com/superfly/flyctl/tree/master/internal/command/apps/"
    click AUTH "https://github.com/superfly/flyctl/tree/master/internal/command/auth/"
    click DEPLOY "https://github.com/superfly/flyctl/tree/master/internal/command/deploy/"
    click MACH "https://github.com/superfly/flyctl/tree/master/internal/command/machine/"
    click CONFIG "https://github.com/superfly/flyctl/tree/master/internal/config/"
    click LOG "https://github.com/superfly/flyctl/tree/master/internal/logger/"
    click METRICS "https://github.com/superfly/flyctl/tree/master/internal/metrics/"
    click APPCONF "https://github.com/superfly/flyctl/tree/master/internal/appconfig/"
    click IMGSRC "https://github.com/superfly/flyctl/tree/master/internal/build/imgsrc/"
    click BUILDKIT "https://github.com/superfly/flyctl/blob/master/internal/build/imgsrc/buildkit.go"
    click DOCKER "https://github.com/superfly/flyctl/blob/master/internal/build/imgsrc/docker.go"
    click AUTHIMPL "https://github.com/superfly/flyctl/tree/master/internal/command/auth/"
    click TOKEN "https://github.com/superfly/flyctl/tree/master/internal/command/tokens/"
    click WEBAUTH "https://github.com/superfly/flyctl/tree/master/internal/command/auth/webauth/"
    click AGCORE "https://github.com/superfly/flyctl/tree/master/agent/"
    click AGSRV "https://github.com/superfly/flyctl/tree/master/agent/server/"
    click AGCMD "https://github.com/superfly/flyctl/tree/master/internal/command/agent/"
    click WG "https://github.com/superfly/flyctl/tree/master/internal/wireguard/"
    click PROXY "https://github.com/superfly/flyctl/tree/master/proxy/"
    click SSH "https://github.com/superfly/flyctl/tree/master/ssh/"
    click LOGIMPL "https://github.com/superfly/flyctl/tree/master/logs/"
    click METCOL "https://github.com/superfly/flyctl/tree/master/internal/metrics/"
    click STATUS "https://github.com/superfly/flyctl/tree/master/internal/statuslogger/"
    click MAINCLI "https://github.com/superfly/flyctl/tree/master/internal/cli/"
    click CMDCTX "https://github.com/superfly/flyctl/tree/master/internal/command_context/"
    click IO "https://github.com/superfly/flyctl/tree/master/iostreams/"
```
