GitHub - phankieuphu/go-hexagonal-cli: Go CLI that generates Hexagonal Architecture project scaffolding using templates.

# Hexagonal Project Generator (Go CLI)


A command-line tool that scaffolds production-ready **Hexagonal Architecture (Ports & Adapters)** services in Go.

Reference: [Hexagon](https://en.wikipedia.org/wiki/Hexagonal_architecture_(software))

This generator creates:

- Full domain layer  
- Application/use-case layer  
- Infrastructure adapters  
- HTTP controller & router  
- External client adapter  
- DynamoDB repository  
- Configuration loader  
- `go.mod`  
- Makefile  
- Complete project folder structure  
- Dynamic naming based on the service name  

Everything is produced using `.tmpl` template files and rendered into a clean Go project.
---

## ๐Ÿš€ Features

- Generate an entire Go project with **one command**
- Full Hexagonal Architecture (Domain โ†’ Application โ†’ Infrastructure)
- Dynamic templates using Goโ€™s `text/template`
- Automatic folder creation
- AWS DynamoDB repository scaffolding
- HTTP controller + router
- External service adapter (generic)
- Config loader
- Makefile included
- Ready to `go build` and `go run`

---

## ๐Ÿ›  Installation

Install globally:

```bash
go install github.com/<your-github>/go-hexagonal-cli@latest

Demo Make sure your Go bin is in PATH:

export PATH=$PATH:$(go env GOPATH)/bin

๐Ÿ— Usage

Generate a new hexagonal Go service:

hexagon init your--service

This creates a folder:

with all layers pre-configured.


๐Ÿ“ Generated Folder Structure

your--service/
โ”‚
โ”œโ”€โ”€ go.mod
โ”œโ”€โ”€ Makefile
โ”‚
โ”œโ”€โ”€ cmd/
โ”‚   โ””โ”€โ”€ your--service/
โ”‚       โ””โ”€โ”€ main.go
โ”‚
โ””โ”€โ”€ internal/
    โ”œโ”€โ”€ domain/
    โ”‚   โ””โ”€โ”€ your-service/
    โ”‚       โ”œโ”€โ”€ entity.go
    โ”‚       โ”œโ”€โ”€ repository.go
    โ”‚       โ””โ”€โ”€ service.go
    โ”‚
    โ”œโ”€โ”€ application/
    โ”‚   โ””โ”€โ”€ your-service/
    โ”‚       โ”œโ”€โ”€ handler.go
    โ”‚       โ”œโ”€โ”€ request.go
    โ”‚       โ””โ”€โ”€ command.go
    โ”‚
    โ””โ”€โ”€ infrastructure/
        โ”œโ”€โ”€ http/
        โ”‚   โ”œโ”€โ”€ controller.go
        โ”‚   โ””โ”€โ”€ router.go
        โ”‚
        โ”œโ”€โ”€ external/
        โ”‚   โ””โ”€โ”€ client.go
        โ”‚
        โ”œโ”€โ”€ repository/
        โ”‚   โ””โ”€โ”€ dynamo.go
        โ”‚
        โ””โ”€โ”€ config/
            โ””โ”€โ”€ config.go

๐ŸŽ› Dynamic Naming

Example:

hexagon init your--service

Automatically generates:

Input Output
your--service project module: your--service
your--service package name: your-service
your--service service name: your-Service

Template variables:

  • {{ .Project }}
  • {{ .Package }}
  • {{ .Service }}

Are injected into all template files.


โš™๏ธ Build & Run

Build:

Run:

Dev mode:


๐Ÿงช Testing


๐Ÿ”ง Development (contributing to the CLI)

Run CLI directly from source:

go run cmd/hexagon/main.go init demo

Build binary:

Clean artifacts:


๐Ÿ“ฆ Template System

All templates live in:

Examples:

  • domain/entity.go.tmpl
  • application/handler.go.tmpl
  • infrastructure/http/controller.go.tmpl
  • go.mod.tmpl

These are standard Go templates processed by text/template.


๐Ÿ™Œ Contributions

Pull requests are welcome! Template improvements, new adapters, codegen features, and documentation PRs are appreciated.


๐Ÿ“„ License

MIT Free to use, modify, and distribute.