All Projects → meowgorithm → babyenv

meowgorithm / babyenv

Licence: MIT License
Go environment var parsing, for babies

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to babyenv

angular-cli-envvars
Example project for my article "Angular CLI and OS Environment Variables"
Stars: ✭ 56 (+86.67%)
Mutual labels:  environment-variables
ionic-workflow-guide
Create a full and powerful worflow with Ionic (Unit Testing, Environment variables, Automatic documentation, Production App Server, Automatic deployment)
Stars: ✭ 46 (+53.33%)
Mutual labels:  environment-variables
arkenv
Type-safe Kotlin configuration by delegates
Stars: ✭ 15 (-50%)
Mutual labels:  environment-variables
climatecontrol
Python library for loading settings and config data from files and environment variables
Stars: ✭ 20 (-33.33%)
Mutual labels:  environment-variables
PathCleaner
Cleanup tool for polluted PATH environment variable
Stars: ✭ 18 (-40%)
Mutual labels:  environment-variables
parse it
A python library for parsing multiple types of config files, envvars & command line arguments that takes the headache out of setting app configurations.
Stars: ✭ 86 (+186.67%)
Mutual labels:  environment-variables
hocon
go implementation of lightbend's HOCON configuration library https://github.com/lightbend/config
Stars: ✭ 49 (+63.33%)
Mutual labels:  environment-variables
pyaml env
Parse YAML configuration with environment variables in Python
Stars: ✭ 36 (+20%)
Mutual labels:  environment-variables
envset
Set env vars before running your program, manage environment and secrets.
Stars: ✭ 34 (+13.33%)
Mutual labels:  environment-variables
github-env-vars-action
🚀 GitHub Action for Environment Variables
Stars: ✭ 129 (+330%)
Mutual labels:  environment-variables
vite-plugin-environment
Easily expose environment variables in Vite.js
Stars: ✭ 57 (+90%)
Mutual labels:  environment-variables
aws-export-assume-profile
Export AWS profiles to your shell environment
Stars: ✭ 40 (+33.33%)
Mutual labels:  environment-variables
fuck-env
Fuck environment variables everywhere
Stars: ✭ 14 (-53.33%)
Mutual labels:  environment-variables
acre
Lightweight configurable environment management in Python
Stars: ✭ 20 (-33.33%)
Mutual labels:  environment-variables
goodconf
Transparently load variables from environment or JSON/YAML file.
Stars: ✭ 80 (+166.67%)
Mutual labels:  environment-variables
dotenv
Load .env files in crystal
Stars: ✭ 16 (-46.67%)
Mutual labels:  environment-variables
php-env
A small and fast .env loader for PHP
Stars: ✭ 19 (-36.67%)
Mutual labels:  environment-variables
cypress-dotenv
Cypress plugin that enables compatability with dotenv
Stars: ✭ 47 (+56.67%)
Mutual labels:  environment-variables
envy
envy: Deserialize environment variables into type-safe structs
Stars: ✭ 64 (+113.33%)
Mutual labels:  environment-variables
ngx-env
Easily inject environment variables into your Angular applications
Stars: ✭ 73 (+143.33%)
Mutual labels:  environment-variables

Babyenv

GoDoc Badge

Package babyenv collects environment variables and places them in corresponding struct fields. It aims to reduce the boilerplate in reading data from the environment.

The struct should contain env tags indicating the names of corresponding environment variables. The values of those environment variables will be then collected and placed into the struct. If nothing is found, struct fields will be given their default values (for example, bools will be false).

type config struct {
    Name string `env:"NAME"`
}

Default values can also be provided in the default tag.

    type config struct {
        Name string `env:"NAME" default:"Jane"`
    }

A 'required' flag can also be set in the following format:

    type config struct {
        Name string `env:"NAME,required"`
    }

If a required flag is set the 'default' tag will be ignored.

Example

package main

import (
    "fmt"
    "os"
    "github.com/meowgorithm/babyenv"
)

type config struct {
    Debug   bool   `env:"DEBUG"`
    Port    string `env:"PORT" default:"8000"`
    Workers int    `env:"WORKERS" default:"16"`
    Name    string `env:"NAME,required"`
}

func main() {
    os.Setenv("DEBUG", "true")
    os.Setenv("WORKERS", "4")
    os.Setenv("NAME", "Jane")

    var cfg config
    if err := babyenv.Parse(&cfg); err != nil {
        log.Fatalf("could not get environment vars: %v", err)
    }

    fmt.Printf("%b\n%s\n%d\n%s", cfg.Debug, cfg.Port, cfg.Workers, cfg.Name)

    // Output:
    // true
    // 8000
    // 4
    // Jane
}

Supported Types

Currently, only the following types are supported:

  • string
  • bool
  • int
  • int64
  • []byte/[]uint8
  • *string
  • *bool
  • *int
  • *int64
  • *[]byte/*[]uint8

Pull requests are welcome, especially for new types.

Credit

This is entirely based on caarlos0’s env package. This one simply has a slightly different interface, and less functionality.

LICENSE

MIT

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].