Giter VIP home page Giter VIP logo

babyenv's Introduction

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

babyenv's People

Contributors

meowgorithm avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.