Class EnvReader

java.lang.Object
codes.grow.packages.betterstack.core.EnvReader

public final class EnvReader extends Object
Reads configuration from environment variables (SPEC section 2.2).

Each lookup tries <prefix><NAME> first (for example GROW_<KEY>_URL), then the vendor variables in the given order. An empty value counts as unset. A present but invalid value raises ConfigException naming the variable, never its value. The environment map is injectable so tests never touch the real process environment.

  • Field Details

  • Constructor Details

    • EnvReader

      public EnvReader(String prefix, Map<String,String> env)
      Creates a reader over an explicit environment map.
      Parameters:
      prefix - prefix for SDK variables, usually DEFAULT_PREFIX
      env - variable map
  • Method Details

    • of

      public static EnvReader of(Map<String,String> env)
      Creates a reader with DEFAULT_PREFIX over an explicit map.
      Parameters:
      env - variable map
      Returns:
      a new reader
    • system

      public static EnvReader system()
      Creates a reader with DEFAULT_PREFIX over the process environment.
      Returns:
      a new reader
    • source

      public Optional<String> source(String name, String... vendorNames)
      Returns the name of the first variable that is set, for error messages.
      Parameters:
      name - SDK variable name without prefix
      vendorNames - vendor variables in precedence order
      Returns:
      the variable name, or empty
    • string

      public Optional<String> string(String name, String... vendorNames)
      Reads a string.
      Parameters:
      name - SDK variable name without prefix, e.g. URL
      vendorNames - vendor variables in precedence order
      Returns:
      the value, or empty when unset
    • duration

      public Optional<Duration> duration(String name, String... vendorNames)
      Reads a duration (integer milliseconds or Go-style units such as 1m30s).
      Parameters:
      name - SDK variable name without prefix
      vendorNames - vendor variables in precedence order
      Returns:
      the duration, or empty when unset
      Throws:
      ConfigException - when the value is invalid
    • bool

      public Optional<Boolean> bool(String name, String... vendorNames)
      Reads a boolean (true/false/1/0/yes/no/on/off, case-insensitive).
      Parameters:
      name - SDK variable name without prefix
      vendorNames - vendor variables in precedence order
      Returns:
      the value, or empty when unset
      Throws:
      ConfigException - when the value is invalid
    • integer

      public OptionalInt integer(String name, String... vendorNames)
      Reads an integer.
      Parameters:
      name - SDK variable name without prefix
      vendorNames - vendor variables in precedence order
      Returns:
      the value, or empty when unset
      Throws:
      ConfigException - when the value is invalid
    • parseDuration

      public static Optional<Duration> parseDuration(String text)
      Parses a duration: integer milliseconds (1500) or Go-style units (500ms, 10s, 2m, 1h, 1m30s).
      Parameters:
      text - the value
      Returns:
      the duration, or empty when the text is not a valid non-negative duration
    • parseBool

      public static Optional<Boolean> parseBool(String text)
      Parses a boolean: true/false/1/0/yes/no/on/off, case-insensitive.
      Parameters:
      text - the value
      Returns:
      the boolean, or empty when not recognised