config

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package config resolves configuration once, with precedence flags, then TOKENHOUND_* environment, then user config, then project tokenhound.yaml, then defaults, into tokenhound.Options.

Index

Examples

Constants

View Source
const BaselineFile = iconfig.BaselineFile

BaselineFile is the default baseline file name at the scan root.

View Source
const ProjectFile = iconfig.ProjectFile

ProjectFile is the project config file name at the scan root.

Variables

This section is empty.

Functions

func BaselinePath

func BaselinePath(in Input) (string, error)

BaselinePath returns the absolute path of the baseline file the configuration of in names: the baseline key (a project-tier value only when it stays inside Root), or <Root>/.tokenhound-baseline.json. The baseline file itself is never read. A bad configuration returns an *Error, as Load does, and so does a project-tier or default path that leads outside Root through a symlink.

func Load

func Load(in Input) (tokenhound.Options, error)

Load resolves the configuration of one run into tokenhound.Options: flags, then TOKENHOUND_* environment variables, then the user file <user config dir>/tokenhound/config.yaml, then the project tokenhound.yaml, then defaults. The project file is untrusted: it cannot set network, alerts, hook or ping, disable verification, set a timeout or lower a limit, and its fail_on is unioned on top of every other layer (tighten only); each ignored value is an untrusted_config_key warning. The project ignore file (ignore_file, or <Root>/.tokenhound-ignore) and the user machine ignore file <user config dir>/tokenhound/ignore are parsed into Options.Ignore; a configured ignore file that is missing, or a malformed rule, is an *Error. The baseline file (baseline, or <Root>/.tokenhound-baseline.json) is parsed into Options.Baseline unless SkipBaseline is set; an absent file is no baseline (with new_only on, a baseline_missing warning), an invalid or newer one is an *Error. new_only sets FailOn.NewOnly and is ignored from the project tier. The Options carry parsed values only (a CA bundle as its bytes, never a path), every value explicitly, Network.FromEnv set, and the config warnings in Warnings. Sources and run settings (IncludeTestKeys, Deterministic, ToolVersion) are left for the caller. A bad configuration returns an *Error.

Example

ExampleLoad resolves the configuration of a project into tokenhound.Options. The project tokenhound.yaml may tighten fail_on but cannot set the network: that value is dropped with an untrusted_config_key warning. The example passes an empty environment and no user config directory, so nothing outside the project is read.

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/aiagentpro/tokenhound/pkg/config"
	tokenhound "github.com/aiagentpro/tokenhound/pkg/tokenhound"
)

func main() {
	root, err := os.MkdirTemp("", "tokenhound-config-example-*")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func() { _ = os.RemoveAll(root) }()
	project := "fail_on:\n  status: [live, unknown]\nnetwork:\n  https_proxy: https://proxy.example:8443\n"
	if err := os.WriteFile(filepath.Join(root, config.ProjectFile), []byte(project), 0o600); err != nil {
		fmt.Println(err)
		return
	}

	opts, err := config.Load(config.Input{
		Root:          root,
		LookupEnv:     func(string) (string, bool) { return "", false },
		UserConfigDir: func() (string, error) { return "", os.ErrNotExist },
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	// Sources and run settings are left for the caller.
	opts.Sources = []tokenhound.Source{tokenhound.Path(root)}

	fmt.Println("fail on:", opts.FailOn.Statuses)
	fmt.Println("verify concurrency:", opts.Verify.MaxConcurrency, "timeout:", opts.Verify.Timeout)
	fmt.Println("proxy set:", opts.Network.HTTPSProxy != "")
	for _, w := range opts.Warnings {
		fmt.Println("warning:", w.Code)
	}
}
Output:
fail on: [live unknown]
verify concurrency: 8 timeout: 10s
proxy set: false
warning: untrusted_config_key

Types

type Error

type Error struct {
	Issues []Issue
}

Error is a configuration that cannot be used: a file that cannot be read, an unsupported version or a schema violation. It lists every issue.

func (*Error) Error

func (e *Error) Error() string

Error returns one line per issue.

type Flags

type Flags struct {
	// Verify is false for --no-verify. Leave it nil for --no-verify=false, so a
	// lower layer that disables verification is not overridden.
	Verify *bool
	// FailOnStatus is --fail-on-status, for example "live,unknown". An empty string
	// is an empty list, which disables status failures; the CLI rejects an empty
	// --fail-on-status as a usage error.
	FailOnStatus *string
	// FailOnWarnings is --fail-on-warnings, for example "timeout".
	FailOnWarnings *string
	// FailOnMinDollars is --fail-on-min-dollars.
	FailOnMinDollars *string
	// Timeout is --timeout.
	Timeout *string
	// NewOnly is --new-only.
	NewOnly *bool
}

Flags are command line overrides. A nil field is not set; string fields hold the raw flag text (lists comma separated, durations like 90s).

type Input

type Input struct {
	// Root is the project root: the scan root directory ("" means the working
	// directory). The project file is <Root>/tokenhound.yaml.
	Root string
	// ConfigFile replaces <Root>/tokenhound.yaml (--config). It stays project tier,
	// because its content may come from the repository. A relative path resolves
	// against the working directory; relative paths inside the file resolve against Root. A missing file, or one that is not a regular file, is an error.
	ConfigFile string
	// Flags override every other layer, with one exception: a project-tier fail_on
	// is unioned on top of every layer including flags, so it can only add statuses
	// or warning codes, or lower the minimum.
	Flags Flags
	// LookupEnv reads an environment variable (nil means os.LookupEnv).
	LookupEnv func(string) (string, bool)
	// UserConfigDir returns the user config directory (nil means
	// os.UserConfigDir); an error skips the user tier.
	UserConfigDir func() (string, error)
	// UserOnly reads no project tier: no tokenhound.yaml (ConfigFile is ignored), no
	// project ignore file and no project baseline file; the user file, the user
	// machine ignore file, the environment and Flags still apply. The Claude Code
	// session-end hook sets it, so a repository cannot turn its scan off.
	UserOnly bool
	// SkipBaseline leaves the baseline file unread: Options.Baseline stays nil and no
	// baseline_missing warning is added. The baseline verb sets it, because the
	// baseline it writes must never depend on the one it replaces.
	SkipBaseline bool
	// IgnoreFile replaces the project ignore file (the ignore_file key of every tier,
	// or <Root>/.tokenhound-ignore) when set (--ignore-file). Its content is project
	// tier, like ConfigFile (project-scope rules, patterns relative to Root), but the
	// path is the caller's choice, so it may sit outside Root. A relative path resolves
	// against the working directory. A missing file is an error.
	IgnoreFile string
	// BaselineFile replaces the baseline file (the baseline key of every tier, or
	// <Root>/.tokenhound-baseline.json) when set (--baseline). A relative path resolves
	// against the working directory and the path may sit outside Root. A missing file
	// is no baseline (with new_only on, a baseline_missing warning), as for the
	// default file.
	BaselineFile string
}

Input says where to resolve configuration from. Nil lookups use the process environment and os.UserConfigDir.

type Issue

type Issue struct {
	// File is the config file as displayed, or the environment variable or flag
	// name; empty when the issue is about reading a file at all.
	File string
	// Line is the 1-based line in File, or 0 when File has no lines.
	Line int
	// Field is the config key, for example fail_on.status[1].
	Field string
	// Message says what is wrong. It never contains the config value.
	Message string
}

Issue is one config problem.

func (Issue) String

func (i Issue) String() string

String formats the issue as "file:line: field: message".

Jump to

Keyboard shortcuts

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