Skip to content

JustSteveKing/config-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

config-example

A minimal example CLI application demonstrating Go interfaces and dependency injection patterns.

This project showcases how to structure a Go CLI application around small, behavior-driven interfaces. It manages a developer configuration file (storing a GitHub token) and serves as a reference for clean architecture principles.

Features

  • Initialize and manage a YAML configuration file
  • Store and retrieve GitHub API tokens
  • Prompt for secrets interactively when needed
  • Fully testable architecture with pluggable implementations

Installation

go install github.com/juststeveking/config-example@latest

Or clone and build locally:

git clone https://github.com/juststeveking/config-example
cd config-example
go build -o config-example main.go

Usage

Initialize configuration

Create or update the config file with an optional token:

config-example init --token your-github-token

Get current config

Display the current configuration:

config-example get

Set GitHub token

Update the GitHub token (prompts if not provided):

config-example set [token]

Project Structure

.
├── cmd/              # CLI command implementations
│   ├── root.go       # Root command and dependency setup
│   ├── init.go       # Initialize config command
│   ├── get.go        # Get config command
│   └── set.go        # Set token command
├── internal/
│   ├── adapters/     # Concrete implementations
│   │   ├── os_paths.go      # OS-based path resolver
│   │   ├── fs_store.go      # File system store
│   │   ├── yaml.go          # YAML serializer
│   │   └── prompt.go        # stdin prompter
│   ├── app/          # Business logic
│   │   └── app.go    # Service implementation
│   ├── config/       # Domain types
│   │   └── types.go  # Config structures
│   └── ports/        # Interface definitions
│       └── interfaces.go     # Port interfaces
├── main.go           # Entry point
├── go.mod            # Go module definition
├── go.sum            # Dependency checksums
└── README.md         # This file

Architecture

This project demonstrates:

  • Ports & Adapters: Small, focused interfaces (ports) that define capabilities, with concrete implementations (adapters) that handle I/O
  • Dependency Injection: Interfaces are injected into the service, making it easy to swap implementations
  • Test Seams: All external dependencies are abstracted, enabling deterministic unit tests without filesystem access

Key Interfaces

  • PathResolver: Determines where config files live
  • ConfigStore: Handles file I/O operations
  • Serializer: Marshals/unmarshals config to/from bytes
  • Prompter: Prompts for user input (secrets)

See article.md for a detailed walkthrough of the design patterns used.

Configuration

The config file is stored at ~/.config/config-example/config.yml:

version: "1.0"
github:
  token: "your-token-here"
repositories:
  example-repo:
    enabled: true

Development

Build

go build -o config-example main.go

Test

go test ./...

Format & Lint

go fmt ./...
go vet ./...

Dependencies

  • github.com/spf13/cobra - CLI framework
  • github.com/charmbracelet/fang - Enhanced error handling
  • gopkg.in/yaml.v3 - YAML serialization

License

MIT

Contributing

Feel free to fork and submit pull requests for any improvements!

About

A minimal example CLI application demonstrating Go interfaces and dependency injection patterns.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages