gconfig

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

README

GConfig

Build Status Go Report Card Go Reference

GConfig is a go library to handle configuration file and environment variables.

It uses viper to handle env vars and configuration file.

Install

go get -u github.com/riccardotzr/gconfig

Usage

Read Environment variables
type EnvironmentVariables struct {
    LogLevel string
    HTTPPort string
}

var envVariablesConfig = []gconfig.EnvConfig{
    {
        Key:      "LOG_LEVEL",
        Variable: "LogLevel",
    },
    {
        Key:      "HTTP_PORT",
        Variable: "HTTPPort",
    },
}

var env EnvironmentVariables

if err := gconfig.GetEnvVariables(envVariablesConfig, &env); err != nil {
    panic(err.Error())
}
Read from file
type ConfigurationFileVariables struct {
    LogLevel string
    HTTPPort string
}

var configuration ConfigurationFileVariables

if err := gconfig.GetConfigFromFile("my/path", "file", &configuration); err != nil {
    panic(err.Error())
}
    

License

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConfigFromFile added in v1.0.1

func GetConfigFromFile(path string, fileName string, output interface{}) error

GetConfigFromFile read configuration from file and save in output interface

func GetEnvVariables

func GetEnvVariables(envVariablesConfig []EnvConfig, output interface{}) error

GetEnvVariables extracts configured environment variables and unmarshals them in provided `output` interface.

Types

type EnvConfig

type EnvConfig struct {
	Key          string
	Variable     string
	DefaultValue string
	Required     bool
}

EnvConfig struct to access env variables

Jump to

Keyboard shortcuts

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