Languages

Envlet for Go

envlet.Inject sets the process environment from one token. Then os.Getenv, like always.

Gogithub.com/selcor/envlet-go

Set up

Three steps. The host keeps one secret and everything else arrives at boot.

  1. 01

    Install

    One module, standard library HTTP.

    Terminal
    go get github.com/selcor/envlet-go@v0.1.0
  2. 02

    Set one secret

    ENVLET_TOKEN in the environment of the binary.

    Go / production / environment variables
    ENVLET_TOKENenvlet_••••••••the only secret the host keeps
    DATABASE_URL••••••••moved to Envlet
    STRIPE_KEY••••••••moved to Envlet
    SENTRY_DSN••••••••moved to Envlet
  3. 03

    Inject at boot

    Call envlet.Inject(ctx) first thing in main.

    main.go
    package main
    
    import (
        "context"
        "log"
        "os"
    
        "github.com/selcor/envlet-go"
    )
    
    func main() {
        if err := envlet.Inject(context.Background()); err != nil {
            log.Fatal(err)
        }
    
        stripe.Key = os.Getenv("STRIPE_KEY")
    }

Three calls

inject for most apps. load when you want a map. get when you want one value.

package main

import (
    "context"
    "log"
    "os"

    "github.com/selcor/envlet-go"
)

func main() {
    if err := envlet.Inject(context.Background()); err != nil {
        log.Fatal(err)
    }

    stripe.Key = os.Getenv("STRIPE_KEY")
}

Typed

envlet types writes this file from the variables in your environment, with a load() that fills the shape below. Run it with --check in CI and a renamed variable fails the build.

Terminal
envlet types
envlet types --check
envlet_env.go
// Generated by envlet types for the "production" environment.

package envletconfig

import (
	"context"
	"slices"

	envlet "github.com/selcor/envlet-go"
)

type Env struct {
	DatabaseURL string `envlet:"DATABASE_URL"`
	Debug bool `envlet:"DEBUG"`
	Flags any `envlet:"FLAGS"`
	Mode string `envlet:"MODE"`
	Port int64 `envlet:"PORT"`
	StripeKey string `envlet:"STRIPE_KEY"`
}

Questions

Will my app start if Envlet is down?

No. The SDK retries for a few seconds, then fails so you notice. It never starts your app with half an environment.

What does the host need?

One variable, ENVLET_TOKEN. Everything else arrives at boot.

Can I still set some values on the Go host?

Yes. A value the host already set wins. Pass override if you want Envlet to win instead.

Stop pasting .env files.

One command. The right values for whoever runs it.