envutil

package
v0.68.4 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 5 Imported by: 0

README

envutil Package

The envutil package provides utilities for reading and validating environment variables with bounds checking.

Overview

This package centralizes the pattern of reading integer-valued environment variables, validating them against configured minimum and maximum bounds, and falling back to a default value when the variable is absent or out of range. It emits warning messages to stderr when an invalid value is encountered, following the console formatting conventions of the rest of the codebase.

Usage

GetIntFromEnv
import (
    "github.com/github/gh-aw/pkg/envutil"
    "github.com/github/gh-aw/pkg/logger"
)

var log = logger.New("mypackage:config")

// Read GH_AW_MAX_CONCURRENT_DOWNLOADS, constrained to [1, 20], default 5
concurrency := envutil.GetIntFromEnv("GH_AW_MAX_CONCURRENT_DOWNLOADS", 5, 1, 20, log)

Behavior:

  • Returns defaultValue when the environment variable is not set.
  • Returns defaultValue and emits a warning when the value cannot be parsed as an integer.
  • Returns defaultValue and emits a warning when the value is outside [minValue, maxValue].
  • Logs the accepted value at debug level when log is non-nil.
  • Pass nil for log to suppress debug output.
Parameters
Parameter Type Description
envVar string Environment variable name (e.g. "GH_AW_TIMEOUT")
defaultValue int Value returned when env var is absent or invalid
minValue int Minimum allowed value (inclusive)
maxValue int Maximum allowed value (inclusive)
log *logger.Logger Optional logger for debug output; pass nil to disable

Design Notes

  • Warning messages use console.FormatWarningMessage so they render consistently in terminals.
  • All warnings go to os.Stderr to avoid polluting structured stdout output.
  • The function only handles integers; floating-point or string env vars should be read directly via os.Getenv.

Documentation

Overview

Package envutil provides utilities for reading and validating environment variables.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetIntFromEnv

func GetIntFromEnv(envVar string, defaultValue, minValue, maxValue int, log *logger.Logger) int

GetIntFromEnv is a generic helper that reads an integer value from an environment variable, validates it against min/max bounds, and returns a default value if invalid. This follows the configuration helper pattern from pkg/workflow/config_helpers.go.

Parameters:

  • envVar: The environment variable name (e.g., "GH_AW_MAX_CONCURRENT_DOWNLOADS")
  • defaultValue: The default value to return if env var is not set or invalid
  • minValue: Minimum allowed value (inclusive)
  • maxValue: Maximum allowed value (inclusive)
  • log: Optional logger for debug output

Returns the parsed integer value, or defaultValue if:

  • Environment variable is not set
  • Value cannot be parsed as an integer
  • Value is outside the [minValue, maxValue] range

Invalid values trigger warning messages to stderr.

Types

This section is empty.

Jump to

Keyboard shortcuts

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