easycfg

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2025 License: MIT Imports: 9 Imported by: 0

README

EasyCfg

GitHub release

EasyCfg is a Go tool designed to simplify system configuration management. It automatically converts YAML configuration files into Go structs and uses Viper to read and monitor configuration changes.

Features

  • Automatically converts YAML configuration files to Go structs
  • Generates corresponding Go files
  • Uses Viper to read YAML configurations
  • Supports hot reloading of configurations
  • Supports monitoring configuration file changes

Installation

# Latest version
go get github.com/chiayu0816/easycfg

# Specific version
go get github.com/chiayu0816/easycfg@v1.1.0

# Install CLI tool
go install github.com/chiayu0816/easycfg/cmd/easycfgcli@v1.1.0

Usage

Generate Configuration Structs
# Using go run (if you've installed the package with go get)
go run github.com/chiayu0816/easycfg/cmd/easycfg -yaml path/to/config.yml

# Using the installed CLI tool
easycfgcli -yaml path/to/config.yml

# Specify output directory
easycfgcli -yaml path/to/config.yml -output myconfig

# Specify package name
easycfgcli -yaml path/to/config.yml -package myconfig

# Monitor configuration file changes
easycfgcli -yaml path/to/config.yml -watch
Using Generated Configurations in Your Program
package main

import (
    "fmt"
    "log"

    "github.com/chiayu0816/easycfg"
)

func main() {
    // Create a configuration struct instance
    cfg := &MyConfig{}

    // Load configuration
    if err := easycfg.LoadConfig("config.yml", cfg); err != nil {
        log.Fatalf("Failed to load configuration: %v", err)
    }

    // Use configuration
    fmt.Printf("Configuration value: %s\n", cfg.SomeField)

    // Monitor configuration changes
    easycfg.WatchConfig("config.yml", cfg, func() {
        fmt.Println("Configuration has been updated")
    })
}

Examples

Check the examples/complete directory for a complete example.

Run examples:

# Run basic example
make run-example

# Run complete example
make run-complete-example

Development

If you're working on the EasyCfg codebase, you can use the Makefile to simplify common tasks:

# Build the main tool
make build

# Build the CLI tool
make build-cli

# Install the CLI tool locally
make install

# Generate configuration from test_config.yml
make generate-config

# Generate configuration using the CLI tool
make generate-config-cli

# Watch for changes in the configuration file
make watch-config

# Clean generated files
make clean

Testing

EasyCfg includes unit tests and benchmarks to ensure code correctness and performance.

Run tests:

# Run all tests
make test

# Run verbose tests (with coverage)
make test-verbose

# Run benchmarks
make benchmark

Dependencies

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadConfig

func LoadConfig(configPath string, configStruct interface{}) error

LoadConfig loads configuration from YAML file to the specified struct using Viper

func WatchConfig

func WatchConfig(configPath string, configStruct interface{}, onChange func()) error

WatchConfig monitors configuration file changes and automatically reloads

func YamlToStruct

func YamlToStruct(yamlFilePath, outputDir, packageName string) error

YamlToStruct converts YAML file to Go struct and generates Go file

Types

This section is empty.

Directories

Path Synopsis
cmd
easycfg command
easycfgcli command
example command
examples
complete command
internal
cli

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL