All Projects → ilyakaznacheev → Cleanenv

ilyakaznacheev / Cleanenv

Licence: mit
✨Clean and minimalistic environment configuration reader for Golang

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Cleanenv

Environs
simplified environment variable parsing
Stars: ✭ 631 (+157.55%)
Mutual labels:  hacktoberfest, environment-variables, configuration
Dynaconf
Configuration Management for Python ⚙
Stars: ✭ 2,082 (+749.8%)
Mutual labels:  hacktoberfest, environment-variables, configuration
Fsconfig
FsConfig is a F# library for reading configuration data from environment variables and AppSettings with type safety.
Stars: ✭ 108 (-55.92%)
Mutual labels:  environment-variables, configuration
Config
A lightweight yet powerful config package for Go projects
Stars: ✭ 126 (-48.57%)
Mutual labels:  environment-variables, configuration
Phpdotenv
Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
Stars: ✭ 11,648 (+4654.29%)
Mutual labels:  configuration, environment-variables
React Env
Runtime environment variables for react apps.
Stars: ✭ 90 (-63.27%)
Mutual labels:  environment-variables, configuration
Startup
🔧 R package: startup - Friendly R Startup Configuration
Stars: ✭ 107 (-56.33%)
Mutual labels:  environment-variables, configuration
Fig
A minimalist Go configuration library
Stars: ✭ 142 (-42.04%)
Mutual labels:  environment-variables, configuration
Ohai
Ohai profiles your system and emits JSON
Stars: ✭ 641 (+161.63%)
Mutual labels:  hacktoberfest, configuration
Dot Hammerspoon
My personal Hammerspoon configuration - mirrored from GitLab
Stars: ✭ 165 (-32.65%)
Mutual labels:  hacktoberfest, configuration
Env
Simple lib to parse environment variables to structs
Stars: ✭ 2,164 (+783.27%)
Mutual labels:  environment-variables, configuration
Aconfig
Simple, useful and opinionated config loader.
Stars: ✭ 187 (-23.67%)
Mutual labels:  environment-variables, configuration
Dotenv Java
🗝️ Dotenv is a no-dep, pure Java module that loads environment variables from a .env file
Stars: ✭ 72 (-70.61%)
Mutual labels:  hacktoberfest, environment-variables
Conf
Go package for loading program configuration from multiple sources.
Stars: ✭ 70 (-71.43%)
Mutual labels:  environment-variables, configuration
Pureconfig
A boilerplate-free library for loading configuration files
Stars: ✭ 1,114 (+354.69%)
Mutual labels:  hacktoberfest, configuration
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+657.14%)
Mutual labels:  environment-variables, configuration
Anyway config
Configuration library for Ruby gems and applications
Stars: ✭ 409 (+66.94%)
Mutual labels:  hacktoberfest, configuration
Config
🛠 A configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP
Stars: ✭ 203 (-17.14%)
Mutual labels:  environment-variables, configuration
Configurate
A simple configuration library for Java applications providing a node structure, a variety of formats, and tools for transformation
Stars: ✭ 148 (-39.59%)
Mutual labels:  hacktoberfest, configuration
React Native Dotenv
Load react native environment variables using import statements for multiple env files.
Stars: ✭ 190 (-22.45%)
Mutual labels:  hacktoberfest, environment-variables

Clean Env

Clean Env

Minimalistic configuration reader

Mentioned in Awesome Go GoDoc Go Report Card Coverage Status Build Status Release License

Overview

This is a simple configuration reading tool. It just does the following:

  • reads and parses configuration structure from the file
  • reads and overwrites configuration structure from environment variables
  • writes a detailed variable list to help output

Content

Installation

To install the package run

go get -u github.com/ilyakaznacheev/cleanenv

Usage

The package is oriented to be simple in use and explicitness.

The main idea is to use a structured configuration variable instead of any sort of dynamic set of configuration fields like some libraries does, to avoid unnecessary type conversions and move the configuration through the program as a simple structure, not as an object with complex behavior.

There are just several actions you can do with this tool and probably only things you want to do with your config if your application is not too complicated.

  • read configuration file
  • read environment variables
  • read some environment variables again

Read Configuration

You can read a configuration file and environment variables in a single function call.

import github.com/ilyakaznacheev/cleanenv

type ConfigDatabase struct {
    Port     string `yaml:"port" env:"PORT" env-default:"5432"`
    Host     string `yaml:"host" env:"HOST" env-default:"localhost"`
    Name     string `yaml:"name" env:"NAME" env-default:"postgres"`
    User     string `yaml:"user" env:"USER" env-default:"user"`
    Password string `yaml:"password" env:"PASSWORD"`
}

var cfg ConfigDatabase

err := cleanenv.ReadConfig("config.yml", &cfg)
if err != nil {
    ...
}

This will do the following:

  1. parse configuration file according to YAML format (yaml tag in this case);
  2. reads environment variables and overwrites values from the file with the values which was found in the environment (env tag);
  3. if no value was found on the first two steps, the field will be filled with the default value (env-default tag) if it is set.

Read Environment Variables Only

Sometimes you don't want to use configuration files at all, or you may want to use .env file format instead. Thus, you can limit yourself with only reading environment variables:

import github.com/ilyakaznacheev/cleanenv

type ConfigDatabase struct {
    Port     string `env:"PORT" env-default:"5432"`
    Host     string `env:"HOST" env-default:"localhost"`
    Name     string `env:"NAME" env-default:"postgres"`
    User     string `env:"USER" env-default:"user"`
    Password string `env:"PASSWORD"`
}

var cfg ConfigDatabase

err := cleanenv.ReadEnv(&cfg)
if err != nil {
    ...
}

Update Environment Variables

Some environment variables may change during the application run. To get the new values you need to mark these variables as updatable with the tag env-upd and then run the update function:

import github.com/ilyakaznacheev/cleanenv

type ConfigRemote struct {
    Port     string `env:"PORT" env-upd`
    Host     string `env:"HOST" env-upd`
    UserName string `env:"USERNAME"`
}

var cfg ConfigRemote

cleanenv.ReadEnv(&cfg)

// ... some actions in-between

err := cleanenv.UpdateEnv(&cfg)
if err != nil {
    ...
}

Here remote host and port may change in a distributed system architecture. Fields cfg.Port and cfg.Host can be updated in the runtime from corresponding environment variables. You can update them before the remote service call. Field cfg.UserName will not be changed after the initial read, though.

Description

You can get descriptions of all environment variables to use them in the help documentation.

import github.com/ilyakaznacheev/cleanenv

type ConfigServer struct {
    Port     string `env:"PORT" env-description:"server port"`
    Host     string `env:"HOST" env-description:"server host"`
}

var cfg ConfigRemote

help, err := cleanenv.GetDescription(&cfg, nil)
if err != nil {
    ...
}

You will get the following:

Environment variables:
  PORT  server port
  HOST  server host

Model Format

Library uses tags to configure the model of configuration structure. There are the following tags:

  • env="<name>" - environment variable name (e.g. env="PORT");
  • env-upd - flag to mark a field as updatable. Run UpdateEnv(&cfg) to refresh updatable variables from environment;
  • env-required - flag to mark a field as required. If set will return an error during environment parsing when the flagged as required field is empty (default Go value). Tag env-default is ignored in this case;
  • env-default="<value>" - default value. If the field wasn't filled from the environment variable default value will be used instead;
  • env-separator="<value>" - custom list and map separator. If not set, the default separator , will be used;
  • env-description="<value>" - environment variable description;
  • env-layout="<value>" - parsing layout (for types like time.Time);

Supported types

There are following supported types:

  • int (any kind);
  • float (any kind);
  • string;
  • boolean;
  • slices (of any other supported type);
  • maps (of any other supported type);
  • time.Duration;
  • time.Time (layout by default is RFC3339, may be overridden by env-layout);
  • any type implementing cleanenv.Setter interface.

Custom Functions

To enhance package abilities you can use some custom functions.

Custom Value Setter

To make custom type allows to set the value from the environment variable, you need to implement the Setter interface on the field level:

type MyField string

func (f *MyField) SetValue(s string) error  {
    if s == "" {
        return fmt.Errorf("field value can't be empty")
    }
    *f = MyField("my field is: "+ s)
    return nil
}

type Config struct {
    Field MyField `env="MY_VALUE"`
}

SetValue method should implement conversion logic from string to custom type.

Custom Value Update

You may need to execute some custom field update logic, e.g. for remote config load.

Thus, you need to implement the Updater interface on the structure level:

type Config struct {
    Field string
}

func (c *Config) Update() error {
    newField, err := SomeCustomUpdate()
    f.Field = newField
    return err
}

Supported File Formats

There are several most popular config file formats supported:

  • YAML
  • JSON
  • TOML
  • ENV
  • EDN

Integration

The package can be used with many other solutions. To make it more useful, we made some helpers.

Flag

You can use the cleanenv help together with Golang flag package.

// create some config structure
var cfg config 

// create flag set using `flag` package
fset := flag.NewFlagSet("Example", flag.ContinueOnError)

// get config usage with wrapped flag usage
fset.Usage = cleanenv.FUsage(fset.Output(), &cfg, nil, fset.Usage)

fset.Parse(os.Args[1:])

Examples

type Config struct {
    Port string `yaml:"port" env:"PORT" env-default:"8080"`
    Host string `yaml:"host" env:"HOST" env-default:"localhost"`
}

var cfg Config

err := ReadConfig("config.yml", &cfg)
if err != nil {
    ...
}

This code will try to read and parse the configuration file config.yml as the structure is described in the Config structure. Then it will overwrite fields from available environment variables (PORT, HOST).

For more details check the example directory.

Contribution

The tool is open-sourced under the MIT license.

If you will find some error, want to add something or ask a question - feel free to create an issue and/or make a pull request.

Any contribution is welcome.

Thanks

Big thanks to a project kelseyhightower/envconfig for inspiration.

The logo was made by alexchoffy.

Blog Posts

Clean Configuration Management in Golang.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].