configlib

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 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

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())
}
Get env variables

This feature is deprecated. Please use another lib, like this.

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 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 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 deprecated

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

Deprecated: use a library to handle env variables, such as [env](https://github.com/caarlos0/env). 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