# instill-ai/cli architecture

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

- Interactive diagram: https://gitdiagram.com/instill-ai/cli
- Repository: https://github.com/instill-ai/cli

## Overview

##### Main Components

a. Core CLI Components:
- Command Layer (pkg/cmd/*)
- API Client Layer (api/*)
- Configuration Management (internal/config/*)
- Authentication System (pkg/cmd/auth/*)
- Instance Management (internal/instance/*)

b. External Systems:
- Instill Cloud (https://api.instill.tech)
- Local Instill Core Deployment
- OAuth2 Authentication Service

##### Component Organization

Top Layer (User Interface):
- It to the command handlers (pkg/cmd/*)

Middle Layer (Business Logic):
- Factory pattern implementation (pkg/cmd/factory)
- Configuration management system
- Authentication flow handlers
- Instance management components

Bottom Layer (External Communication):
- API client implementation
- HTTP/Unix socket transport
- OAuth2 integration

##### Key Relationships to Highlight

Create distinct sections for:
a. Command Structure:
- Root command
- Subcommands (auth, config, instance, local, etc.)
- Command factory pattern

b. Runtime Components:
- IO Streams handling
- HTTP client management
- Configuration management

c. External Interactions:
- API endpoints
- Authentication flows
- Local deployment management

##### Implementation Details

- Relationship between the command pattern implementation and the actual execution flow
- Factory pattern used for command creation
- Configuration file handling system
- OAuth2 authentication flow

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph CLI["CLI Interface"]
        MAIN["Main Entry Point"]
        ROOT["Root Command Handler"]
        FACTORY["Command Factory"]
    end

    subgraph CMD["Command Layer"]
        AUTH["Authentication Commands"]
        CONFIG["Configuration Commands"]
        INSTANCE["Instance Management"]
        LOCAL["Local Deployment"]
    end

    subgraph CORE["Core Components"]
        CONF["Configuration Management"]
        OAUTH["OAuth2 System"]
        IO["IO Streams"]
        HTTP["HTTP/Unix Transport"]
        UTIL["Command Utilities"]
    end

    subgraph API["API Layer"]
        CLIENT["API Client"]
        INST_MGT["Instance Manager"]
    end

    subgraph EXT["External Services"]
        CLOUD["Instill Cloud"]
        CORE_DEP["Local Instill Core"]
        OAUTH_SVC["OAuth2 Service"]
    end

    %% Relationships
    MAIN --> ROOT
    ROOT --> FACTORY
    FACTORY --> AUTH
    FACTORY --> CONFIG
    FACTORY --> INSTANCE
    FACTORY --> LOCAL

    AUTH --> OAUTH
    AUTH --> HTTP
    CONFIG --> CONF
    INSTANCE --> INST_MGT
    LOCAL --> HTTP

    CLIENT --> HTTP
    INST_MGT --> CLIENT

    HTTP --> CLOUD
    HTTP --> CORE_DEP
    OAUTH --> OAUTH_SVC

    %% All components use these
    UTIL --> IO
    AUTH --> IO
    CONFIG --> IO
    INSTANCE --> IO
    LOCAL --> IO

    %% Styling
    classDef cli fill:#f9f,stroke:#333
    classDef cmd fill:#bbf,stroke:#333
    classDef core fill:#bfb,stroke:#333
    classDef api fill:#fbf,stroke:#333
    classDef ext fill:#fbb,stroke:#333

    class MAIN,ROOT,FACTORY cli
    class AUTH,CONFIG,INSTANCE,LOCAL cmd
    class CONF,OAUTH,IO,HTTP,UTIL core
    class CLIENT,INST_MGT api
    class CLOUD,CORE_DEP,OAUTH_SVC ext

    %% Click events
    click MAIN "https://github.com/instill-ai/cli/blob/main/cmd/inst/main.go"
    click ROOT "https://github.com/instill-ai/cli/tree/main/pkg/cmd/root/"
    click FACTORY "https://github.com/instill-ai/cli/tree/main/pkg/cmd/factory/"
    click AUTH "https://github.com/instill-ai/cli/tree/main/pkg/cmd/auth/"
    click CONFIG "https://github.com/instill-ai/cli/tree/main/pkg/cmd/config/"
    click INSTANCE "https://github.com/instill-ai/cli/tree/main/pkg/cmd/instance/"
    click LOCAL "https://github.com/instill-ai/cli/tree/main/pkg/cmd/local/"
    click CONF "https://github.com/instill-ai/cli/tree/main/internal/config/"
    click OAUTH "https://github.com/instill-ai/cli/tree/main/internal/oauth2/"
    click IO "https://github.com/instill-ai/cli/tree/main/pkg/iostreams/"
    click HTTP "https://github.com/instill-ai/cli/tree/main/internal/httpunix/"
    click UTIL "https://github.com/instill-ai/cli/tree/main/pkg/cmdutil/"
    click CLIENT "https://github.com/instill-ai/cli/tree/main/api/"
    click INST_MGT "https://github.com/instill-ai/cli/tree/main/internal/instance/"
```
