goconfig

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: Apache-2.0 Imports: 12 Imported by: 11

README

GoConfig

Build Status Go Report Card GoDoc Coverage Status

Goconfig is a Go (Golang) configuration initialization module that provides simple and efficient functionality for loading configurations based on struct definitions. It supports multiple configuration sources such as defaults, environment variables, command-line arguments, and configuration files in various formats (JSON, YAML, HCL).

Table of Contents

Features

  • Struct-based Configuration: Define your configuration using Go structs with tags for JSON, YAML, CLI, and environment variables.
  • Multiple Sources: Load configuration from defaults, environment variables, command-line arguments, and configuration files.
  • Flexible File Support: Supports JSON, YAML, and HCL configuration file formats.
  • Extensible: Easily extendable to support additional configuration sources or formats.

Installation

go get github.com/demdxx/goconfig

Usage

Defining Configuration Structures

Define your configuration using Go structs. Use struct tags to specify default values and mappings for different configuration sources.

package config

import "time"

type ServerConfig struct {
 HTTP struct {
  Listen       string        `default:":8080"  json:"listen" yaml:"listen" cli:"http-listen" env:"SERVER_HTTP_LISTEN"`
  ReadTimeout  time.Duration `default:"120s"   json:"read_timeout" yaml:"read_timeout" env:"SERVER_HTTP_READ_TIMEOUT"`
  WriteTimeout time.Duration `default:"120s"   json:"write_timeout" yaml:"write_timeout" env:"SERVER_HTTP_WRITE_TIMEOUT"`
 }
 GRPC struct {
  Listen  string        `default:"tcp://:8081" json:"listen" yaml:"listen" cli:"grpc-listen" env:"SERVER_GRPC_LISTEN"`
  Timeout time.Duration `default:"120s"        json:"timeout" yaml:"timeout" env:"SERVER_GRPC_TIMEOUT"`
 }
 Profile struct {
  Mode   string `json:"mode" yaml:"mode" default:"" env:"SERVER_PROFILE_MODE"`
  Listen string `json:"listen" yaml:"listen" default:"" env:"SERVER_PROFILE_LISTEN"`
 }
}

type ConfigType struct {
 ServiceName     string       `json:"service_name" yaml:"service_name" env:"SERVICE_NAME" default:"disk"`
 DatacenterName  string       `json:"datacenter_name" yaml:"datacenter_name" env:"DC_NAME" default:"??"`
 Hostname        string       `json:"hostname" yaml:"hostname" env:"HOSTNAME" default:""`
 Hostcode        string       `json:"hostcode" yaml:"hostcode" env:"HOSTCODE" default:""`

 LogAddr         string       `default:"" env:"LOG_ADDR"`
 LogLevel        string       `default:"debug" env:"LOG_LEVEL"`

 Server          ServerConfig `json:"server" yaml:"server"`
}

var Config ConfigType
Loading Configuration

Use the Load function to initialize your configuration. You can specify various options such as loading defaults, environment variables, command-line arguments, and configuration files.

Loading with Default Options

By default, Load will attempt to load configuration from defaults, environment variables, command-line arguments, and configuration files.

package main

import (
 "log"

 configLoader "github.com/demdxx/goconfig"
 "your_project/config"
)

func init() {
 if err := configLoader.Load(&config.Config); err != nil {
  log.Fatalf("Failed to load configuration: %v", err)
 }
}

func main() {
 // Your application code
}
Loading with Specific Options

You can customize the loading process by specifying options such as WithDefaults, WithEnv, WithArgs, and WithFile.

package main

import (
 "log"

 "github.com/demdxx/goconfig"
 "your_project/config"
)

func init() {
 // Example: Load configuration with defaults and environment variables only
 options := []goconfig.Option{
  goconfig.WithDefaults(),
  goconfig.WithEnv(),
 }

 if err := goconfig.Load(&config.Config, options...); err != nil {
  log.Fatalf("Failed to load configuration: %v", err)
 }
}

func main() {
 // Your application code
}

Available Options:

  • WithDefaults(): Sets default values for the configuration.
  • WithEnv(): Parses environment variables.
  • WithArgs(...string): Parses command-line arguments.
  • WithFile(path string): Loads configuration from a specified file.

Example

config.go
package config

import "time"

type ServerConfig struct {
 HTTP struct {
  Listen       string        `default:":8080"  json:"listen" yaml:"listen" cli:"http-listen" env:"SERVER_HTTP_LISTEN"`
  ReadTimeout  time.Duration `default:"120s"   json:"read_timeout" yaml:"read_timeout" env:"SERVER_HTTP_READ_TIMEOUT"`
  WriteTimeout time.Duration `default:"120s"   json:"write_timeout" yaml:"write_timeout" env:"SERVER_HTTP_WRITE_TIMEOUT"`
 }
 GRPC struct {
  Listen  string        `default:"tcp://:8081" json:"listen" yaml:"listen" cli:"grpc-listen" env:"SERVER_GRPC_LISTEN"`
  Timeout time.Duration `default:"120s"        json:"timeout" yaml:"timeout" env:"SERVER_GRPC_TIMEOUT"`
 }
 Profile struct {
  Mode   string `json:"mode" yaml:"mode" default:"" env:"SERVER_PROFILE_MODE"`
  Listen string `json:"listen" yaml:"listen" default:"" env:"SERVER_PROFILE_LISTEN"`
 }
}

type ConfigType struct {
 ServiceName     string       `json:"service_name" yaml:"service_name" env:"SERVICE_NAME" default:"disk"`
 DatacenterName  string       `json:"datacenter_name" yaml:"datacenter_name" env:"DC_NAME" default:"??"`
 Hostname        string       `json:"hostname" yaml:"hostname" env:"HOSTNAME" default:""`
 Hostcode        string       `json:"hostcode" yaml:"hostcode" env:"HOSTCODE" default:""`

 LogAddr         string       `default:"" env:"LOG_ADDR"`
 LogLevel        string       `default:"debug" env:"LOG_LEVEL"`

 Server          ServerConfig `json:"server" yaml:"server"`
}

var Config ConfigType
main.go
package main

import (
 "log"

 "github.com/demdxx/goconfig"
 "your_project/config"
)

func init() {
 // Load configuration with defaults, environment variables, and a specific config file
 options := []goconfig.Option{
  goconfig.WithDefaults(),
  goconfig.WithEnv(),
  goconfig.WithFile("config.yaml"),
 }

 if err := goconfig.Load(&config.Config, options...); err != nil {
  log.Fatalf("Failed to load configuration: %v", err)
 }
}

func main() {
 // Your application code
}

Dependencies

  • github.com/caarlos0/env
  • github.com/hashicorp/hcl
  • github.com/mcuadros/go-defaults

Contributing

Contributions are welcome! Please open issues and submit pull requests for any features, bug fixes, or improvements.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

TODO

  • Add support for environment variable prefixes

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(cfg config, opts ...Option) error

Load data from file

Types

type Option added in v1.3.0

type Option func(*options)

func WithArgs added in v1.1.0

func WithArgs() Option

WithArgs parse command line arguments

func WithCustomArgs added in v1.3.1

func WithCustomArgs(args ...string) Option

WithCustomArgs parse custom arguments

func WithDefaults added in v1.1.0

func WithDefaults() Option

WithDefaults set defaults for config

func WithEnv added in v1.1.0

func WithEnv() Option

WithEnv parse environment variables

func WithFile added in v1.1.0

func WithFile(path string) Option

WithFile parse config from file

Jump to

Keyboard shortcuts

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