Giter Club home page Giter Club logo

env's Introduction

env

Tag-based environment configuration for structs.

Godoc Build Status Go Report Card

Installation

$ go get -u github.com/codingconcepts/env

Usage

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/codingconcepts/env"
)

type config struct {
	Secret            []byte        `env:"SECRET" required:"true"`
	Region            string        `env:"REGION"`
	Port              int           `env:"PORT" required:"true"`
	Peers             []string      `env:"PEERS"` // you can use `delimiter` tag to specify separator, for example `delimiter:" "` 
	ConnectionTimeout time.Duration `env:"TIMEOUT" default:"10s"`
}

func main() {
	c := config{}
	if err := env.Set(&c); err != nil {
		log.Fatal(err)
	}

	...
}
$ ID=1 SECRET=shh PORT=1234 PEERS=localhost:1235,localhost:1236 TIMEOUT=5s go run main.go

Supported field types

  • bool and []bool
  • string and []string
  • []byte
  • int, int8, int16, int32, int64, []int, []int8, []int16, []int32, and []int64
  • uint, uint8, uint16, uint32, uint64, []uint, []uint8, []uint16, []uint32, and []uint64
  • float32, float64, []float32, and []float64
  • time.Duration and []time.Duration

env's People

Contributors

codingconcepts avatar hans-m-song avatar nicklanng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

env's Issues

Empty ENV variable eg. FOO= mapped as []string gives slice with one item

Code example:

package main

import "fmt"
import "github.com/codingconcepts/env"

type Test struct {
    Foo []string `env:"FOO" required:"true"`
}

func main() {
  var test Test
  env.Set(&test)

  fmt.Println(test.Foo)
  fmt.Println(len(test.Foo))
  fmt.Println(test.Foo[0])
  fmt.Println(len(test.Foo[0]))
}

gives

> FOO= go run main.go
[]
1

0

It's probably due to the fact how os.LookupEnv works, yet wondering should it be understood like that in terms of this library?


In my code I could go (pun intented:) around with something like:

func isEnvReallyEmpty(val []string) bool {
    if (len(val) == 1 && len(val[0]) == 0) {
        return true
    }

    return false
}

I can provide pull-request if this will be considered as a unwanted behaviour.

Suport struc tag "choices"

I've looked at quite a few command-line and configuration packages. My selection criteria - in order of preference - is:

  1. to focus on the use-case of writing GitHub actions (which rely on env vars). I don't need a configuration package that reads from a file, and accepts flags, and env vars - simpler is better for my needs.
  2. to support a "choices" tag to validate the env var. There is only one package out there that does this (that I could find) but doesn't use struc tags to do it.
  3. to find something relatively simple and well written that I can modify to do what I need.

After looking at the code in this project, I think it meets all my basic needs - except the "choices" struc tag - and doesn't look very hard to modify. I will fork this project and create a pull request when I have something complete, tested and documented.

int default support?

Hello, it looks like you only have support for default for strings. Is this correct? If not can you please document an example of how to use it? Thanks

Support prefixes for environment variable lookups

Hello, I love how simple the interface of this library is, the only thing I find missing is the ability to include a prefix in the lookup. This would allow reusable structs with different environment "namespaces".

For example:

type test struct {
	Lorem string `env:"lorem"`
	Ipsum string `env:"ipsum"`
}

var (
	foo test
	bar test
)

func main() {
	env.Set(&foo, "FOO_")
	env.Set(&bar, "BAR_")
}

I will open pull request ready with a functional change, would just need some guidance on appropriate tests.

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.