goconfig

package module
v0.0.0-...-c61ed6a Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2022 License: MIT Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

goconfig

Simple configuration file parser for property and yaml files. Allows for fields in config files to be environment variables with default values.

Install

go get github.com/MartinSimango/goconfig

Example

Below is an example config yaml file that goconfig can parse:

Config File (app.yaml)
# app.yaml

port: ${SERVICE_PORT,8000}
service-name: service
db: 
  host: ${DB_HOST, 127.0.0.1}
  port: ${DB_PORT,8890}

The format of a config value is:

configValue: ${ENVIRONMENT_VARIABLE,default_value}  
# OR 
configValue: ${ENVIRONMENT_VARIABLE} 
# OR 
configValue: value

Below is an example of the code that parse the config file and stores the config within a struct.

Example Program - Loading program config from yaml file (main.go)
package main

import (
	"fmt"

	"github.com/MartinSimango/goconfig"
)

type YamlServiceConfiguration struct {
	Port        int    `yaml:"port"`
	ServiceName string `yaml:"service-name"`
	DB          struct {
		Host string `yaml:"host"`
		Port int    `yaml:"port"`
	} `yaml:"db"`
}

func main() {

	fileConfig := goconfig.YamlFileConfiguration("app.yaml", &YamlServiceConfiguration{})
	fileParser := goconfig.NewConfigFileParser(fileConfig)

	config, err := fileParser.ParseConfig()

	if err != nil {
		fmt.Println(err)
	} else {
		yamlConfig := config.(*YamlServiceConfiguration) // cast if needed

		fmt.Printf("%+v", yamlConfig)
	}
}

Notes

Currently the only supported primitive types in config structs are:

  • string
  • bool
  • int
  • int8
  • int16
  • int32
  • int64
  • float32
  • float64

I plan on adding other types very shortly.

Contributing

  • Fork the repo on GitHub
  • Clone the project to your own machine
  • Create a branch with your modifications git checkout -b feature/new-feature.
  • Then commit your changes git commit -m 'Added new feature
  • Make a push to your branch git push origin feature/new-feature.
  • Submit a Pull Request so that I can review your changes

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldErrorsToString

func FieldErrorsToString(fieldErrors []FieldError) string

FieldErrorsToString coverts an array of field arrays into a string

func FileFormatToString

func FileFormatToString(fileFormat FileFormat) string

Types

type ConfigError

type ConfigError interface {
	Error
}

type ConfigFileParser

type ConfigFileParser interface {
	ParseConfig() (interface{}, error)
}

ConfigFileParser parses the config file

type ConfigFileParserImpl

type ConfigFileParserImpl struct {
	FileConfiguration *FileConfiguration
}

func NewConfigFileParser

func NewConfigFileParser(fileConfiguration *FileConfiguration) *ConfigFileParserImpl

DefaulltConfigFileParser is a constructor

func (*ConfigFileParserImpl) ParseConfig

func (cf *ConfigFileParserImpl) ParseConfig() (interface{}, error)

ParseConfig parses the file configured the FileConfiguration struct variable's FileName parameter.

type Error

type Error interface {
	error
}

type FieldError

type FieldError struct {
	Field        string
	ErrorMessage string
}

func AppendFieldError

func AppendFieldError(configFieldErrors []FieldError, fieldError *FieldError) []FieldError

AppendFieldError appends fieldError to the configFieldErrors are and returns the resulting slice.

func ToFieldError

func ToFieldError(field string, err error) *FieldError

ToFieldError converts err into a field error

func (*FieldError) Error

func (cfe *FieldError) Error() string

type FileConfigError

type FileConfigError struct {
	ConfigFile  string
	FileFormat  FileFormat
	FieldErrors []FieldError
}

func (*FileConfigError) Error

func (fce *FileConfigError) Error() string

type FileConfiguration

type FileConfiguration struct {
	FileName               string
	FileFormat             FileFormat
	FileInputConfiguration interface{}
	EnvironmentLoader      goenvloader.EnvironmentLoader
}

func JsonFileConfiguration

func JsonFileConfiguration(configFile string, config interface{}) *FileConfiguration

func NewFileConfiguration

func NewFileConfiguration(fileName string,
	fileFormat FileFormat,
	fileInputConfiguration interface{},
	environmentLoader goenvloader.EnvironmentLoader) *FileConfiguration

func PropertyFileConfiguration

func PropertyFileConfiguration(configFile string, config interface{}) *FileConfiguration

func YamlFileConfiguration

func YamlFileConfiguration(configFile string, config interface{}) *FileConfiguration

type FileFormat

type FileFormat int
const (
	YAML FileFormat = iota
	JSON
	PROPERTY
)

Directories

Path Synopsis
examples
property command
yaml command

Jump to

Keyboard shortcuts

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