configparser

package
v1.24.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package configparser contains the code required to fill a Go structure representing the configuration information from several sources like:

  1. a struct containing the default values
  2. a map from string to strings (e.g. the one that can be extracted for a Kubernetes ConfigMap)
  3. an environment source like the system environment variables or a "fake" one suitable for testing.

To map the struct fields with the sources, the "env" tag is being used like in the following example:

type Data struct {
	// WebhookCertDir is the directory where the certificates for the webhooks
	// need to written. This is different between plain Kubernetes and OpenShift
	WebhookCertDir string `json:"webhookCertDir" env:"WEBHOOK_CERT_DIR"`

	// WatchNamespace is the namespace where the operator should watch and
	// is configurable via environment variables of via the OpenShift console
	WatchNamespace string `json:"watchNamespace" env:"WATCH_NAMESPACE"`
}

The ReadConfigMap function will look for an "WEBHOOK_CERT_DIR" entry inside the passed map or a corresponding environment variable. This function can be used to implement a method in the previous struct like in the following example:

func (config *Data) ReadConfigMap(data map[string]string) {
	configparser.ReadConfigMap(config, &Data{}, data, configparser.OsEnvironment{})
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadConfigMap

func ReadConfigMap(target interface{}, defaults interface{}, data map[string]string, env EnvironmentSource)

ReadConfigMap reads the configuration from the environment and the passed in data map. Config and defaults are supposed to be pointers to structs of the same type

Types

type EnvironmentSource

type EnvironmentSource interface {
	Getenv(key string) string
}

EnvironmentSource is an interface to identify an environment values source.

type OsEnvironment

type OsEnvironment struct{}

OsEnvironment is an EnvironmentSource that fetch data from the OS environment.

func (OsEnvironment) Getenv

func (OsEnvironment) Getenv(key string) string

Getenv retrieves the value of the environment variable named by the key.

Jump to

Keyboard shortcuts

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