Skip to content

Generate a .env.example from an env struct

License

Notifications You must be signed in to change notification settings

miniscruff/envexample

Repository files navigation

EnvExample

codecov Go Report Card Release GitHub Workflow Status PkgGoDev

Generate a .env.example from an env labeled struct

Installation

The easiest way to install envexample is using go install from latest or by version.

go install github.com/miniscruff/envexample@latest
go install github.com/miniscruff/[email protected]

All Installation Options has the full list of options.

CLI Arguments has all the arguments.

Usage

Given a configuration struct and using env to parse environment variables like:

package main

import (
	"fmt"

	"github.com/caarlos0/env/v9"
	"github.com/joho/godotenv"
)

// Config handles our applications configuration
type Config struct {
	// Home is our users home directory
	Home string `env:"HOME"`
	// Port is our http listeners port address
	Port int `env:"PORT" envDefault:"3000"`
	// IsProduction is whether or not we are running in production
	IsProduction bool `env:"PRODUCTION"`
}

func main() {
	// attempt to load .env file if found
	_ = godotenv.Load()

	cfg := Config{}
	if err := env.Parse(&cfg); err != nil {
		fmt.Printf("%+v\n", err)
    }

	fmt.Printf("%+v\n", cfg)
}

You can run envexample like:

envexample -struct Config

To generate an .env.example file similar to:

# Generated by envexample dev.

# Config handles our applications configuration
#
# Home is our users home directory
#HOME=""
# IsProduction is whether or not we are running in production
#PRODUCTION=""
# Port is our http listeners port address
#PORT=3000

This file should be commited to your source control, and a copy should be created locally that is then loaded by your application.

Something similar to cp .env.example .env would work.

All Installation Options

deb/rpm

Download a .deb or .rpm file from the releases page and install with dpkg -i and rpm -i respectively.

Windows Scoop

On windows you can use scoop by first adding the repo and then installing.

scoop bucket add envexample https://github.com/miniscruff/envexample
scoop install envexample

macOS with Homebrew

On macOS, you can use Homebrew to install by first tapping the repository.

brew tap miniscruff/envexample https://github.com/miniscruff/envexample
brew install envexample

Manual

  • Download from here.
  • Add executable somewhere in your path depending on your platform.

From source

Go install can be used to download envexample from the main branch or by tag.

go install github.com/miniscruff/envexample@latest
go install github.com/miniscruff/[email protected]

Tasks

Below is a list of common development tasks, these can easily be run using xc. For example xc test will run the test suite.

test

Run unit test suite with code coverage enabled.

go test ./... -coverprofile=c.out

coverage

Run unit tests and preview the html coverage results. requires: test

go tool cover -html=c.out

lint

goimports -w -local github.com/miniscruff/envexample .
golangci-lint run ./...

release

Run generation and prepare a release PR requires: test,lint

go run main.go -h > DOCS.md
changie batch auto
changie merge
git checkout --branch release-$(changie latest)
gh pr create

License

Distributed under the MIT License.