|
| 1 | +- [Distrobox](README.md) |
| 2 | + - [Distrobox Go Rewrite: Architecture and Design](#distrobox-go-rewrite-architecture-and-design) |
| 3 | + - [Overview](#overview) |
| 4 | + - [Directory Structure](#directory-structure) |
| 5 | + - [Architecture Layers](#architecture-layers) |
| 6 | + - [Dependency Injection Pattern](#dependency-injection-pattern) |
| 7 | + - [Configuration System](#configuration-system) |
| 8 | + - [Shell Scripts](#shell-scripts) |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +# Distrobox Go Rewrite: Architecture and Design |
| 13 | + |
| 14 | +This document describes the architecture of the Distrobox Go rewrite, explaining how |
| 15 | +different layers interact and the design decisions behind the codebase. It's meant to |
| 16 | +help contributors understand the system and know where to make changes. |
| 17 | + |
| 18 | +## Overview |
| 19 | + |
| 20 | +The Distrobox Go rewrite is designed with **clear separation of concerns** in mind. |
| 21 | +The rewrite followed these principles: |
| 22 | + |
| 23 | +- CLI layer handles command-line parsing and user interaction; |
| 24 | +- Container manager implementations are interchangeable; |
| 25 | +- UI components can evolve independently; |
| 26 | +- The codebase must remains testable and maintainable; |
| 27 | +- Dependencies must be kept at minimum. |
| 28 | + |
| 29 | +## Directory Structure |
| 30 | + |
| 31 | +```text |
| 32 | +distrobox |
| 33 | +├── cmd/distrobox/ |
| 34 | +│ └── main.go # Entry point |
| 35 | +├── internal/ |
| 36 | +│ ├── cli/ # CLI layer (command definitions) |
| 37 | +│ │ ├── root.go # Root command with global flags |
| 38 | +│ │ ├── create.go, list.go, etc. # Individual commands |
| 39 | +│ │ └── helpers.go |
| 40 | +│ ├── config/ # Configuration management |
| 41 | +│ ├── inside-distrobox/ |
| 42 | +│ │ └── assets/ # Embedded shell scripts |
| 43 | +├── pkg/ |
| 44 | +│ ├── commands/ # Business logic layer |
| 45 | +│ │ ├── create.go, list.go, etc. # Command implementations |
| 46 | +│ ├── containermanager/ # Container abstraction |
| 47 | +│ │ ├── containermanager.go # Interface definitions |
| 48 | +│ │ └── providers/ # Implementations |
| 49 | +│ │ ├── podman.go |
| 50 | +│ │ └── docker.go |
| 51 | +│ ├── ui/ # UI components |
| 52 | +│ │ ├── progress.go |
| 53 | +│ │ ├── printer.go |
| 54 | +│ │ └── prompt.go |
| 55 | +│ └── manifest/ # Manifest parsing |
| 56 | +``` |
| 57 | + |
| 58 | +## Architecture Layers |
| 59 | + |
| 60 | +### 1. CLI Layer (`internal/cli/`) |
| 61 | + |
| 62 | +The CLI layer handles command-line argument parsing, global flag processing, and |
| 63 | +command dispatch. It is also responsible for binding the application to the shell's stdin/stdout, |
| 64 | +for loading the configuration, and for instantiating the components. |
| 65 | + |
| 66 | +Among other things, the concerete `ContainerManager` implementation is selected and instantiated in the cli layer. |
| 67 | + |
| 68 | +### 2. Command Layer (`pkg/commands/`) |
| 69 | + |
| 70 | +The command layer contains the business logic for each distrobox operation. Commands |
| 71 | +are independent of CLI specifics and can be tested and reused independently. |
| 72 | + |
| 73 | +Commands should be **pure orchestrators**. They coordinate between |
| 74 | +the container manager abstraction and UI components, but don't contain low-level |
| 75 | +implementation details. |
| 76 | + |
| 77 | +Each command is implemented by a `Execute` method that takes a context and options struct. |
| 78 | + |
| 79 | +### 3. Container Manager Layer (`pkg/containermanager/`) |
| 80 | + |
| 81 | +The container manager is the abstraction over different container runtimes. This design |
| 82 | +allows distrobox to work with Docker, Podman, and other container managers without |
| 83 | +duplicating logic. |
| 84 | + |
| 85 | +### 4. UI Layer (`pkg/ui/`) |
| 86 | + |
| 87 | +The UI layer provides simple components for user interaction and output formatting. |
| 88 | +These are instantiated in the CLI layer and passed to commands. |
| 89 | + |
| 90 | +- **Progress**: Tracks multi-step operations with status indicators |
| 91 | +- **Printer**: Formats and displays structured output |
| 92 | +- **Prompter**: Gets user confirmation or input |
| 93 | + |
| 94 | +## Dependency Injection Pattern |
| 95 | + |
| 96 | +The architecture uses **context-based dependency injection** to pass the container |
| 97 | +manager from the root command to all subcommands. |
| 98 | + |
| 99 | +**Flow:** |
| 100 | + |
| 101 | +```text |
| 102 | +main() |
| 103 | + ↓ |
| 104 | +LoadConfig() |
| 105 | + ↓ |
| 106 | +NewRootCommand().Run() |
| 107 | + ↓ |
| 108 | +beforeAction() [global hooks] |
| 109 | + ↓ Creates container manager |
| 110 | + ↓ Stores in context |
| 111 | + ↓ |
| 112 | +Specific command action (e.g., createAction) |
| 113 | + ↓ Extracts container manager from context |
| 114 | + ↓ Creates UI tools |
| 115 | + ↓ Delegates to command layer |
| 116 | +``` |
| 117 | + |
| 118 | +This pattern ensures: |
| 119 | + |
| 120 | +- Container manager is available to all commands without global state |
| 121 | +- UI tools are created fresh for each invocation |
| 122 | +- Testing can substitute different implementations via context |
| 123 | + |
| 124 | +## Configuration System |
| 125 | + |
| 126 | +Configuration is loaded once at startup in `main()`. Configuration sources (in order of precedence): |
| 127 | + |
| 128 | +1. Command-line flags |
| 129 | +2. Environment variables (prefixed with `DBX_`) |
| 130 | +3. Config file (`~/.config/distrobox/distrobox.conf`) |
| 131 | +4. Defaults |
| 132 | + |
| 133 | +This centralized approach makes it easy to understand where values come from and ensures consistency across commands. |
| 134 | + |
| 135 | +## Shell Scripts |
| 136 | + |
| 137 | +When a container is created, part of the `Distrobox` application is loaded in the container |
| 138 | +as it is meant to be executed inside it: |
| 139 | + |
| 140 | +- `distrobox-init` serves as the container entrypoint |
| 141 | +- `distrobox-export` to expose binaries and applications to the host |
| 142 | +- `distrobox-host-exec` to execute host's commands from inside the distrobox |
| 143 | + |
| 144 | +Such commands are POSIX shell scripts that are included as assets in `internal/inside-distrobox/assets` |
0 commit comments