env

package
v1.32.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package env builds a RuleSet from an environment variable holding a source/terms string, so an already-built binary can be faulted at process start with no recompile. It NEVER reads the environment in init(): the caller decides when and whether, keeping production opt-in. Pairs with engine.WithProductionGuard.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromEnv

func FromEnv(varName string, opts ...terms.Option) (engine.RuleSet, error)

FromEnv reads the environment variable named varName (or "CHAOTIC_RULES" when varName is ""), parses its value with source/terms, and returns a RuleSet. An empty or unset variable yields an empty RuleSet, so the engine stays a no-op. Options are forwarded to terms.Compile — pass terms.WithLint to apply the blast-radius check to rules arriving from the environment.

Example
package main

import (
	"fmt"
	"os"

	"github.com/RomanAgaltsev/chaotic/engine"
	"github.com/RomanAgaltsev/chaotic/source/env"
)

func main() {
	os.Setenv("CHAOTIC_RULES", `kind(http_client)=error("boom")`)
	defer os.Unsetenv("CHAOTIC_RULES")

	rs, err := env.FromEnv("CHAOTIC_RULES")
	if err != nil {
		panic(err)
	}
	// Pair with a production guard so a real binary stays opt-in.
	eng := engine.New(engine.WithRuleSource(rs))
	fmt.Println(eng.Enabled())
}
Output:
true
Example (Lint)
package main

import (
	"errors"
	"fmt"
	"os"

	"github.com/RomanAgaltsev/chaotic/engine"
	"github.com/RomanAgaltsev/chaotic/source/env"
	"github.com/RomanAgaltsev/chaotic/source/terms"
)

func main() {
	os.Setenv("CHAOTIC_RULES", `wipeout: panic("boom")`)
	defer os.Unsetenv("CHAOTIC_RULES")

	_, err := env.FromEnv("", terms.WithLint(engine.LintReject))
	fmt.Println(errors.Is(err, engine.ErrLintRejected))
}
Output:
true

Types

This section is empty.

Jump to

Keyboard shortcuts

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