# yamayo/ec2ssh architecture

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

- Interactive diagram: https://gitdiagram.com/yamayo/ec2ssh
- Repository: https://github.com/yamayo/ec2ssh

## Overview

##### Main Components
- CLI Layer: The entry point of the application (e.g., ec2ssh.go at the root) which parses command-line options (like --profile, --region, --user) as described in the README.
- AWS EC2 Interaction: The “ec2” package (with files like ec2/service.go) encapsulates the logic for interacting with AWS, handling the retrieval of EC2 instance data using the provided AWS credentials and region settings.
- Interactive Selector (peco Integration): The “peco” component (including peco/runner.go and peco/internal/util/) integrates with peco to allow the user to interactively filter and select an EC2 instance.
- SSH Connection Handler: The “ssh” package (with config.go and run.go) contains the logic to form and execute the SSH command using the selected instance information, user details, and private key configurations.
- General Utilities: The “util” directory (and internal util in peco) provides helper functions that support common tasks across the modules.

##### Relationships and Interactions
- The CLI layer receives user input and environment variables (like AWS credentials and region) then directs the flow:
- It calls the AWS EC2 interaction module to fetch the list of available instances.
- The list is piped or passed to the peco integration module, which allows the user to choose the desired instance interactively.
- Once selected, the CLI ties together the chosen instance data with the SSH module, which formats the SSH command and initiates the connection.
- There are clear dependencies from the CLI to each of the specialized modules (ec2, peco, ssh), while utility functions are used by all to avoid duplication.

##### Architectural Patterns and Design Principles
- Modular design: The project is clearly arranged into distinct modules (AWS interaction, UI selection, SSH execution) which simplifies maintenance and testing.
- Separation of concerns: Each module is responsible for a specific task, ensuring that responsibilities such as fetching instance metadata, user interaction, and connection setup remain decoupled.
- Reusability: The presence of separate utility libraries (under util and peco/internal/util) suggests a design that encourages reusing common helper functions.

##### Technologies and External Dependencies
- AWS SDK (implied through use of AWS credentials and instance interaction).
- peco: An external interactive filtering tool integrated for instance selection (as referenced in both the README and the “peco” directory).
- SSH: The built-in SSH libraries/tools (managed via the ssh package) are used to handle actual connection execution.
- Environment Variables & Configuration Files: AWS credentials and configurations are processed as described in the README.

## Diagram (Mermaid)

```mermaid
flowchart TD
    CLI["CLI Interface (ec2ssh.go)"]:::cli
    AWS["AWS EC2 Module (ec2/service.go)"]:::aws

    CLI -->|"fetchInstances"| AWS

    subgraph "Interactive Selector"
        Peco["Peco Integration (peco/runner.go)"]:::peco
        Peco_Util["Peco Internal Util (peco/internal/util/util.go)"]:::peco
    end

    AWS -->|"instanceList"| Peco

    subgraph "SSH Connection Handler"
        SSH_Config["SSH Config (ssh/config.go)"]:::ssh
        SSH_Run["SSH Run (ssh/run.go)"]:::ssh
    end

    Peco -->|"selectedInstance"| SSH_Config
    Peco -->|"selectedInstance"| SSH_Run

    Util["General Utilities (util/util.go)"]:::util

    %% Dotted connections to General Utilities
    CLI -.-> Util
    AWS -.-> Util
    Peco -.-> Util
    Peco_Util -.-> Util
    SSH_Config -.-> Util
    SSH_Run -.-> Util

    %% External dependencies
    AWS_Cred["AWS Credentials (~/.aws/credentials)"]:::external
    AWS_ConfigFile["AWS Config (~/.aws/config)"]:::external
    SSH_Keys["SSH Key Storage (~/.ssh)"]:::external
    AWS_Service["AWS EC2 Service"]:::external

    CLI -.-> AWS_Cred
    CLI -.-> AWS_ConfigFile
    AWS -->|"calls"| AWS_Service
    SSH_Config -.-> SSH_Keys
    SSH_Run -.-> SSH_Keys

    %% Click Events
    click CLI "https://github.com/yamayo/ec2ssh/blob/master/ec2ssh.go"
    click AWS "https://github.com/yamayo/ec2ssh/blob/master/ec2/service.go"
    click Peco "https://github.com/yamayo/ec2ssh/blob/master/peco/runner.go"
    click Peco_Util "https://github.com/yamayo/ec2ssh/blob/master/peco/internal/util/util.go"
    click SSH_Config "https://github.com/yamayo/ec2ssh/blob/master/ssh/config.go"
    click SSH_Run "https://github.com/yamayo/ec2ssh/blob/master/ssh/run.go"
    click Util "https://github.com/yamayo/ec2ssh/blob/master/util/util.go"

    %% Styles
    classDef cli fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef aws fill:#FFFF99,stroke:#333,stroke-width:2px;
    classDef peco fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef ssh fill:#FFB6C1,stroke:#333,stroke-width:2px;
    classDef util fill:#DDA0DD,stroke:#333,stroke-width:2px;
    classDef external fill:#F0E68C,stroke:#333,stroke-width:2px;

    class CLI cli
    class AWS aws
    class Peco peco
    class Peco_Util peco
    class SSH_Config ssh
    class SSH_Run ssh
    class Util util
    class AWS_Cred external
    class AWS_ConfigFile external
    class SSH_Keys external
    class AWS_Service external
```
