easycfg

package module
v1.0.0 Latest Latest
Warning

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

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

README

EasyCfg

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

go get github.com/chiayu0816/easycfg

Usage

Generate Configuration Structs
# Basic usage
go run cmd/easycfg/main.go -yaml path/to/config.yml

# Specify output directory
go run cmd/easycfg/main.go -yaml path/to/config.yml -output myconfig

# Specify package name
go run cmd/easycfg/main.go -yaml path/to/config.yml -package myconfig

# Monitor configuration file changes
go run cmd/easycfg/main.go -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

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
example command
examples
complete command

Jump to

Keyboard shortcuts

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