envconfig

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 12 Imported by: 5

Documentation

Overview

Package envconfig contains utilities to load configuration from files and/or environment variables.

Index

Constants

View Source
const DefaultConfigFileProfile = "default"

DefaultConfigFileProfile is the default profile used.

Variables

This section is empty.

Functions

func DefaultConfigFilePath

func DefaultConfigFilePath() string

DefaultConfigFilePath is the default config file path used. It is os.UserConfigDir/temporalio/temporal.toml. If the path does not exist, fallback to an empty file path.

func LoadClientOptions

func LoadClientOptions(options LoadClientOptionsRequest) (client.Options, error)

LoadClientOptions loads client options from file and then applies environment variable overrides. This will not fail if the config file does not exist. This is effectively a shortcut for LoadClientConfigProfile + ClientConfigProfile.ToClientOptions. See LoadClientOptionsRequest and ClientConfigProfile on how files and environment variables are applied.

func LoadDefaultClientOptions

func LoadDefaultClientOptions() (client.Options, error)

LoadDefaultClientOptions loads client options using default information from config files and environment variables. This just delegates to LoadClientOptions with the default options set. See that function and associated options for where/how values are loaded.

func MustLoadDefaultClientOptions

func MustLoadDefaultClientOptions() client.Options

MustLoadDefaultClientOptions invokes LoadDefaultClientOptions and panics on error.

func NormalizeGRPCMetaKey

func NormalizeGRPCMetaKey(k string) string

NormalizeGRPCMetaKey converts the given key to lowercase and replaces underscores with hyphens.

Types

type ClientConfig

type ClientConfig struct {
	// Profiles, keyed by profile name.
	Profiles map[string]*ClientConfigProfile
}

ClientConfig represents a client config file.

func LoadClientConfig

func LoadClientConfig(options LoadClientConfigOptions) (ClientConfig, error)

LoadClientConfig loads the client configuration structure from TOML. Does not load values from environment variables (but may use environment variables to get which config file to load). This will not fail if the file does not exist. See ClientConfig.FromTOML for details on format.

func (*ClientConfig) FromTOML

func (c *ClientConfig) FromTOML(b []byte, options ClientConfigFromTOMLOptions) error

FromTOML converts from TOML to the client config. This will replace all profiles within, it does not do any form of merging.

func (*ClientConfig) ToClientOptions

func (c *ClientConfig) ToClientOptions(profile string, options ToClientOptionsRequest) (client.Options, error)

ToClientOptions converts the given profile to client options that can be used to create an SDK client. Defaults to "default" profile if profile is empty string. Will fail if profile not found.

func (*ClientConfig) ToTOML

func (c *ClientConfig) ToTOML(options ClientConfigToTOMLOptions) ([]byte, error)

ToTOML converts the client config to TOML. Note, this may not be byte-for-byte exactly what may have been set in ClientConfig.FromTOML.

type ClientConfigCodec

type ClientConfigCodec struct {
	// Remote endpoint for the codec.
	Endpoint string
	// Auth for the codec.
	Auth string
}

ClientConfigCodec is codec configuration for a client.

type ClientConfigFromTOMLOptions

type ClientConfigFromTOMLOptions struct {
	// If true, will error if there are unrecognized keys.
	Strict bool
	// If non-nil, populated with additional (unrecognized) profile fields.
	// Key is profile name, value is map of field name to field value.
	// This allows callers to preserve custom profile fields without modifying
	// this package. Note, if Strict is true the additional fields will cause an
	// error before they can be captured here.
	AdditionalProfileFields map[string]map[string]any
}

type ClientConfigProfile

type ClientConfigProfile struct {
	// Client address.
	Address string
	// Client namespace.
	Namespace string
	// Client API key. If present and TLS field is nil or present but without Disabled as true, TLS is defaulted to
	// enabled.
	APIKey string
	// Optional client TLS config.
	TLS *ClientConfigTLS
	// Optional client codec config.
	Codec *ClientConfigCodec
	// Client gRPC metadata (aka headers). When loading from TOML and env var, or writing to TOML, the keys are
	// lowercased and hyphens are replaced with underscores. This is used for deduplicating/overriding too, so manually
	// set values that are not normalized may not get overridden with [ClientConfigProfile.ApplyEnvVars].
	GRPCMeta map[string]string
}

ClientConfigProfile is profile-level configuration for a client.

func LoadClientConfigProfile

func LoadClientConfigProfile(options LoadClientConfigProfileOptions) (ClientConfigProfile, error)

LoadClientConfigProfile loads a specific client config profile from file and then applies environment variable overrides. This will not fail if the config file does not exist. This is effectively a shortcut for LoadClientConfig + ClientConfigProfile.ApplyEnvVars. See LoadClientOptionsRequest and ClientConfigProfile on how files and environment variables are applied.

func (*ClientConfigProfile) ApplyEnvVars

func (c *ClientConfigProfile) ApplyEnvVars(env EnvLookup) error

ApplyEnvVars overwrites any values in the profile with environment variables if the environment variables are set and non-empty. If env lookup is nil, defaults to EnvLookupOS

func (*ClientConfigProfile) ToClientOptions

func (c *ClientConfigProfile) ToClientOptions(options ToClientOptionsRequest) (client.Options, error)

ToClientOptions converts this profile to client options that can be used to create an SDK client.

type ClientConfigTLS

type ClientConfigTLS struct {
	// If true, TLS is explicitly disabled. If false/unset, whether TLS is enabled or not depends on other factors such
	// as whether this struct is present or nil, and whether API key exists (which enables TLS by default).
	Disabled bool
	// Path to client mTLS certificate. Mutually exclusive with ClientCertData.
	ClientCertPath string
	// PEM bytes for client mTLS certificate. Mutually exclusive with ClientCertPath.
	ClientCertData []byte
	// Path to client mTLS key. Mutually exclusive with ClientKeyData.
	ClientKeyPath string
	// PEM bytes for client mTLS key. Mutually exclusive with ClientKeyPath.
	ClientKeyData []byte
	// Path to server CA cert override. Mutually exclusive with ServerCACertData.
	ServerCACertPath string
	// PEM bytes for server CA cert override. Mutually exclusive with ServerCACertPath.
	ServerCACertData []byte
	// SNI override.
	ServerName string
	// True if host verification should be skipped.
	DisableHostVerification bool
}

ClientConfigTLS is TLS configuration for a client.

type ClientConfigToTOMLOptions

type ClientConfigToTOMLOptions struct {
	// Defaults to two-space indent.
	OverrideIndent *string
	// If non-nil, these additional fields will be serialized with each profile.
	// Key is profile name, value is map of field name to field value.
	AdditionalProfileFields map[string]map[string]any
}

ClientConfigToTOMLOptions are options for ClientConfig.ToTOML.

type EnvLookup

type EnvLookup interface {
	// Environ gets all environment variables in the same manner as [os.Environ].
	Environ() []string
	// Getenv gets a single environment variable in the same manner as [os.LookupEnv].
	LookupEnv(string) (string, bool)
}

EnvLookup abstracts environment variable lookup for ClientConfigProfile.ApplyEnvVars. EnvLookupOS is the common implementation.

var EnvLookupOS EnvLookup = envLookupOS{}

EnvLookupOS implements EnvLookup for os.

type LoadClientConfigOptions

type LoadClientConfigOptions struct {
	// Override the file path to use to load the TOML file for config. Defaults to TEMPORAL_CONFIG_FILE environment
	// variable or if that is unset/empty, defaults to [os.UserConfigDir]/temporalio/temporal.toml. If ConfigFileData is
	// set, this cannot be set and no file loading from disk occurs.
	ConfigFilePath string

	// TOML data to load for config. If set, this overrides any file loading. Cannot be set if ConfigFilePath is set.
	ConfigFileData []byte

	// If true, will error if there are unrecognized keys.
	ConfigFileStrict bool

	// Override the environment variable lookup (only used to determine which config file to load). If nil,
	// defaults to [EnvLookupOS].
	EnvLookup EnvLookup
}

LoadClientConfigOptions are options for LoadClientConfig.

type LoadClientConfigProfileOptions

type LoadClientConfigProfileOptions struct {
	// Override the file path to use to load the TOML file for config. Defaults to TEMPORAL_CONFIG_FILE environment
	// variable or if that is unset/empty, defaults to [os.UserConfigDir]/temporalio/temporal.toml. If ConfigFileData is
	// set, this cannot be set and no file loading from disk occurs. Ignored if DisableFile is true.
	ConfigFilePath string

	// TOML data to load for config. If set, this overrides any file loading. Cannot be set if ConfigFilePath is set.
	// Ignored if DisableFile is true.
	ConfigFileData []byte

	// Specific profile to use after file is loaded. Defaults to TEMPORAL_PROFILE environment variable or if that is
	// unset/empty, defaults to "default". If either this or the environment variable are set, load will fail if the
	// profile isn't present in the config. Ignored if DisableFile is true.
	ConfigFileProfile string

	// If true, will error if there are unrecognized keys.
	ConfigFileStrict bool

	// If true, will not do any TOML loading from file or data. This and DisableEnv cannot both be true.
	DisableFile bool

	// If true, will not apply environment variables on top of file config for the client options, but
	// TEMPORAL_CONFIG_FILE and TEMPORAL_PROFILE environment variables may still by used to populate defaults in this
	// options structure.
	DisableEnv bool

	// Override the environment variable lookup. If nil, defaults to [EnvLookupOS].
	EnvLookup EnvLookup
}

LoadClientConfigProfileOptions are options for LoadClientConfigProfile.

type LoadClientOptionsRequest

type LoadClientOptionsRequest struct {
	// Override the file path to use to load the TOML file for config. Defaults to TEMPORAL_CONFIG_FILE environment
	// variable or if that is unset/empty, defaults to [os.UserConfigDir]/temporalio/temporal.toml. If ConfigFileData is
	// set, this cannot be set and no file loading from disk occurs. Ignored if DisableFile is true.
	ConfigFilePath string

	// TOML data to load for config. If set, this overrides any file loading. Cannot be set if ConfigFilePath is set.
	// Ignored if DisableFile is true.
	ConfigFileData []byte

	// Specific profile to use after file is loaded. Defaults to TEMPORAL_PROFILE environment variable or if that is
	// unset/empty, defaults to "default". If either this or the environment variable are set, load will fail if the
	// profile isn't present in the config. Ignored if DisableFile is true.
	ConfigFileProfile string

	// If true, will error if there are unrecognized keys.
	ConfigFileStrict bool

	// If true, will not do any TOML loading from file or data. This and DisableEnv cannot both be true.
	DisableFile bool

	// If true, will not apply environment variables on top of file config for the client options, but
	// TEMPORAL_CONFIG_FILE and TEMPORAL_PROFILE environment variables may still by used to populate defaults in this
	// options structure.
	DisableEnv bool

	// If true and a codec is configured, the data converter of the client will point to the codec remotely. Users
	// should usually not set this and rather configure the codec locally. Users should especially not enable this for
	// clients used by workers since they call the codec repeatedly even during workflow replay.
	IncludeRemoteCodec bool

	// Override the environment variable lookup. If nil, defaults to [EnvLookupOS].
	EnvLookup EnvLookup
}

LoadClientOptionsRequest are options for LoadClientOptions.

type ToClientOptionsRequest

type ToClientOptionsRequest struct {
	// If true and a codec is configured, the data converter of the client will point to the codec remotely. Users
	// should usually not set this and rather configure the codec locally. Users should especially not enable this for
	// clients used by workers since they call the codec repeatedly even during workflow replay.
	IncludeRemoteCodec bool
}

ToClientOptionsRequest are options for ClientConfig.ToClientOptions and ClientConfigProfile.ToClientOptions.

Jump to

Keyboard shortcuts

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