configlib

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2019 License: Apache-2.0 Imports: 10 Imported by: 2

README

Config Lib

Build Status Go Report Card GoDoc

Config lib is a library to handle configuration in your program. It handles config file and environments variables.

It uses viper and koanf to handle, respectively, env variables and configuration.

This library handle json in a case sensitive mode.

Install

go get -u github.com/mia-platform/configlib

Example usage

Get env variables
type EnvVariables struct {
  StringValue string
  BoolValue   bool
}

var envVariablesConfig = []configlib.EnvConfig{
        {
		Key:          "SOME_STRING",
		Variable:     "StringValue",
		DefaultValue: "",
	},
	{
		Key:      "BOOLEAN_KEY",
		Variable: "BoolValue",
		Required: true,
	},
}

var env EnvVariables
if err := configlib.GetEnvVariables(envVariablesConfig, &env); err != nil {
  panic(err.Error())
}
Load file service json configuration - with json schema validation
type Config struct {}

func loadServiceConfiguration(path, fileName string) (Config, error) {
	jsonSchema, err := configlib.ReadFile(configSchemaPath)
	if err != nil {
		return nil, err
	}

	var config ServiceConfig
	if err := configlib.GetConfigFromFile(fileName, path, jsonSchema, &config); err != nil {
		return nil, err
	}

	return config, err
}

// Load service configuration
config, err := loadServiceConfiguration("my/path", "file")
if err != nil {
  log.Fatal(err.Error())
}
Load file service json configuration - without json schema validation
type Config struct {}

func loadServiceConfiguration(path, fileName string) (Config, error) {
	var config ServiceConfig
	if err := configlib.GetConfigFromFile(fileName, path, nil, &config); err != nil {
		return nil, err
	}

	return config, err
}

// Load service configuration
config, err := loadServiceConfiguration("my/path", "file")
if err != nil {
  log.Fatal(err.Error())
}

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use [SemVer][semver] for versioning. For the versions available, see the tags on this repository.

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

func GetConfigFromFile(configName, configPath string, jsonSchema []byte, output interface{}) error

GetConfigFromFile func 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.

func ReadFile

func ReadFile(filePath string) ([]byte, error)

ReadFile is a utility to read a file from the file system.

Types

type EnvConfig

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

EnvConfig to setup to access to env variables.

Jump to

Keyboard shortcuts

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