envexample

command module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2023 License: MIT Imports: 4 Imported by: 0

README

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/envexample@v0.1.0

All Installation Options has the full list of options.

Usage

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

package main

import (
	"fmt"

	"github.com/caarlos0/env/v8"
	"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.

Generate

You can also use go generate using one of the following styles:

// directly run latest
//go:generate go run github.com/miniscruff/envexample@latest -struct Config
// with a specific version
//go:generate go run github.com/miniscruff/envexample@v0.1.0 -struct Config
// with our installed version
//go:generate envexample -struct Config

CLI Arguments

-dir directory
    directory our config struct is located in (default ".")
-dry
    output to stdout instead of writing to file
-export filepath
    filepath to export generated example to (default ".env.example")
-h    show help
-prefix string
    prefix config when parsing in env
-required-if-no-def
    required if no default config when parsing in env
-tag string
    tag name config when parsing in env (default "env")
-type struct
    struct to build example from
-use-field-name
    use field name by default config when parsing in env
-v    show version

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/envexample@v0.1.0

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 ./...

License

Distributed under the MIT License.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL