func getEnv(key string, defaultValue string) string {
	val := os.Getenv(key)
	if len(val) == 0 {
		return defaultValue
	}
	return val
}
Read environment variable with default values
package main
import (
	"fmt"
	"os"
	"strconv"
)
type Config struct {
	Concurrency          int
	DNSOverride          string
	Host                 string
	PrintFailedTestsOnly bool
	TestDirectory        string
}
func FromEnv() (*Config, error) {
	concurrency, err := strconv.Atoi(getEnv("TEST_CONCURRENCY", "2"))
	if err != nil {
		return nil, fmt.Errorf("invalid concurrency value: %s", err)
	}
	if concurrency < 1 {
		return nil, fmt.Errorf("invalid concurrency value: %d", concurrency)
	}
	printFailedOnly := false
	if getEnv("TEST_PRINT_FAILED_ONLY", "false") == "true" {
		printFailedOnly = true
	}
	return &Config{
		Concurrency:          concurrency,
		Host:                 getEnv("TEST_HOST", ""),
		DNSOverride:          getEnv("TEST_DNS_OVERRIDE", ""),
		PrintFailedTestsOnly: printFailedOnly,
		TestDirectory:        getEnv("TEST_DIRECTORY", "tests"),
	}, nil
}
func ApplyConfig(config *Config) error {
	if len(config.DNSOverride) > 0 {
		if len(config.Host) < 1 {
			return fmt.Errorf("TEST_HOST is required to use DNS override")
		}
		return AppendHostsFile(fmt.Sprintf("%s %s", config.DNSOverride, config.Host))
	}
	return nil
}