Skip to content

taybart/env

Repository files navigation

env

test

Easy environments in go!

package main

import (
  "fmt"
  "os"
  "github.com/taybart/env"
)

func main() {
  // set some env
  os.Setenv("ENVS_ARE_FUN", "true")
  os.Setenv("WOOT", `{ "yes": "even_json" }`)
  // Declare our env for this file
  env.Add([]string{
      "ENVS_ARE_FUN",
      "WOOT",
      "PORT=8080", // default values
      "INSECURE?", // optional values, default to go "zero values"
  })


  config := struct{
    Yes string `json:"yes"`
  }{}

  env.JSON("WOOT", &config)

  fmt.Println(config.Yes)

  // check if vars are defined
  if !env.Has("INSECURE") {
    fmt.Println("INSECURE is not defined")
  }
  // default zero values
  if !env.Bool("INSECURE") {
    fmt.Println("This is super secure now")
  }

  if env.Bool("ENVS_ARE_FUN") {
    fmt.Println("They really are...")
  }

  // look up random envs
  home := env.Get("HOME")
  fmt.Printf("HOME=%s\n", home)
}

Generate env requirements with the CLI

Installation

go install github.com/taybart/env/cmd/scanenv@latest

Generate env file

Single File:

$ scanenv -f ./main.go
ENVS_ARE_FUN=""
ENVS_ARE_COMPLICATED=""
PORT="8080"
INSECURE="Value marked as optional"

Recursive Directory:

$ scanenv -d .
ENVS_ARE_FUN=""
ENVS_ARE_COMPLICATED=""
PORT="8080"
INSECURE="Value marked as optional"

Get where env is declared

$ scanenv -d . -p
# ./main.go
ENVS_ARE_FUN=""
ENVS_ARE_COMPLICATED=""
PORT="8080"
INSECURE="Value marked as optional"

# ./util/redis.go
REDIS_PORT=""

Validate .env file

$ scanenv -d ./ -validate .env
[ERROR] Missing required env
ENV
[WARN] Using default value for PORT=69