Tag-based environment configuration for structs

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
Similar Resources

A CLI tool that masks and obscurates your environment variables for demos

envo - Mask your environment variables envo (environment variable obscuration) is a CLI tool that masks/obscurates your environment variables. Use cas

Dec 3, 2022

Terraform gitlab environment

terraform-gitlab-environment terraform-gitlab-environment for gitlab Requirements This is a list of plugins that need to be installed previously to en

Feb 23, 2022

Run any shell command in a temporary proxy environment.

Run any shell command in a temporary proxy environment.

Jun 2, 2022

Envp - ENVP is cli wrapper that sets environment variables by profile when you execute the command line

ENVP ENVP is cli wrapper that sets environment variables by profile based config

Nov 7, 2022

Go-test-app - Test application to verify environment deployment and reachability over HTTP

Test app Test application to verify environment deployment and reachability over

May 23, 2022

Golang library with POSIX-compliant command-line UI (CLI) and Hierarchical-configuration. Better substitute for stdlib flag.

Golang library with POSIX-compliant command-line UI (CLI) and Hierarchical-configuration. Better substitute for stdlib flag.

cmdr cmdr is a POSIX-compliant, command-line UI (CLI) library in Golang. It is a getopt-like parser of command-line options, be compatible with the ge

Oct 28, 2022

Reads from existing Cloud Providers (reverse Terraform) and generates your infrastructure as code on Terraform configuration

Reads from existing Cloud Providers (reverse Terraform) and generates your infrastructure as code on Terraform configuration

TerraCognita Imports your current Cloud infrastructure to an Infrastructure As Code Terraform configuration (HCL) or/and to a Terraform State. At Cycl

Dec 30, 2022

A CLI Tool to easily generate your Terraform configuration

Tf Tf is a command line tool to easily generate your Terraform configuration with an interactive prompt. Inspiration Boredom in Covid-19 Installation

Sep 30, 2022

HAProxy configuration parser

HAProxy configuration parser

HAProxy configuration parser autogenerated code if you change types/types.go you need to run go run generate/go-generate.go $(pwd) Contributing For co

Dec 14, 2022
Comments
  • int default support?

    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

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

    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

    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.

Related tags
Substitute environment variables from command line for template driven configuration files.
Substitute  environment variables from command line for template driven configuration files.

Substitute Variables (subvars) is a small utility which provides a way to render any Go templates from command line recognizing the object being passed in and drawing attributes from the object to create wanted text. It is very useful for template driven configuration files.

Jun 3, 2022
Utility CLI to convert Spring Boot Yaml configuration into external configuration

boot-config-export Utility CLI to convert Spring Boot Yaml configuration into external configuration (as environment variables). The variables are tra

Nov 17, 2021
Interactive prompt to set and push a semver tag
Interactive prompt to set and push a semver tag

cutver For when you know what version to tag, and you want to cut a release in the annotated tag format. Installation go install github.com/roryq/cutv

Nov 15, 2022
A golang tag key value parser

tag_parser A golang tag key value parser Installation go get github.com/gvassili/tag_parser Example package main import ( "fmt" "github.com/gvass

Nov 24, 2021
Needlessly complex program to parse, increment, and push the latest version number tag on github. Made with ๐Ÿ˜ซ , ๐Ÿ˜“ & ๐Ÿ˜ญ

increment What is this? Needlessly complex program to parse, increment, and push the latest version number tag on github. Err handling by Sentry Made

Feb 28, 2022
Integrated console application library, using Go structs as commands, with menus, completions, hints, history, Vim mode, $EDITOR usage, and more ...
Integrated console application library, using Go structs as commands, with menus, completions, hints, history, Vim mode, $EDITOR usage, and more ...

Gonsole - Integrated Console Application library This package rests on a readline console library, (giving advanced completion, hint, input and histor

Nov 20, 2022
An excellent tool for converting json files to structs or classes in any programming language.
An excellent tool for converting json files to structs or classes in any programming language.

Explore Usage ยป Report Bug ยท Request Feature Table of Contents About The Project Supported Languages Getting Started Usage Parameters Set Up Your Own

Dec 10, 2022
Envopts - Provides a code generator for turning env structs into functional options

envopts Provides a code generator to turn structs annotated for the popular env

Jan 10, 2022
Optinator - Idiomatic way to fill structs with options logic

optinator Go packages are generally start with a main struct and the package ini

Mar 15, 2022
Cli app to quicky add gitignore files to your current environment
Cli app to quicky add gitignore files to your current environment

Gitignorer Cli app to quickly add gitignore files to your current environment/projects Usage Make sure golang s installed on your machine. After that

Jul 18, 2022