environment

package
v0.0.0-...-2e7a380 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package environment implements helpers to handle environment varialbes and other function for startup.

Index

Constants

This section is empty.

Variables

View Source
var (
	EnvDevelopment = NewVariable("OPENSLIDES_DEVELOPMENT", "false", "If set, the service uses the default secrets.")
)

Environment variables used to configure the environment.

Functions

func InterruptContext

func InterruptContext() (context.Context, context.CancelFunc)

InterruptContext works like signal.NotifyContext. It returns a context that is canceled, when a signal is received.

It listens on os.Interrupt and unix.SIGTERM. If the signal is received two times, os.Exit(2) is called.

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

ParseDuration is like time.ParseDuration but uses second as default unit.

func ReadSecret

func ReadSecret(lookup Environmenter, pathVariable Variable) (string, error)

ReadSecret reads a secret from a file given by an environment variable.

If OPENSLIDES_DEVELOPMENT is set, then this will always return the string 'openslides'

func ReadSecretWithDefault

func ReadSecretWithDefault(lookup Environmenter, pathVariable Variable, defaultValue string) (string, error)

ReadSecretWithDefault is like ReadSecret, but it allows to set another default value then "openslides".

Types

type Environmenter

type Environmenter interface {
	Getenv(key string) string
	UseVariable(v Variable)
}

Environmenter is an type, that can return the value for environment variables.

It also saves the used environment variables.

type ForDocu

type ForDocu struct {
	// contains filtered or unexported fields
}

ForDocu is an environment to gether the used environment variables.

It does not access the real environment varialbes but returns an empty string so that the default value is used.

func (*ForDocu) BuildDoc

func (e *ForDocu) BuildDoc() (string, error)

BuildDoc create the environment documentation with all used variables.

func (*ForDocu) Getenv

func (e *ForDocu) Getenv(key string) string

Getenv returns an empty string.

But activates development mode to use the default values for secrets.

func (*ForDocu) UseVariable

func (e *ForDocu) UseVariable(v Variable)

UseVariable saves the used Variables

type ForProduction

type ForProduction struct{}

ForProduction is an environment used for production.

It fetches the environment variables from os.Getenv().

func (*ForProduction) Getenv

func (e *ForProduction) Getenv(key string) string

Getenv calls os.Getenv.

func (*ForProduction) UseVariable

func (e *ForProduction) UseVariable(v Variable)

UseVariable saves the used Variables

type ForTests

type ForTests map[string]string

ForTests is a map that simulates environment variables.

func (ForTests) Getenv

func (e ForTests) Getenv(key string) string

Getenv returns the fake enironment variable for a key.

func (ForTests) UseVariable

func (e ForTests) UseVariable(v Variable)

UseVariable does nothing for tests.

type Variable

type Variable struct {
	Key         string
	Default     string
	Description string
}

Variable represents a environment variable. It can be used by the packages for configuration.

It is only allowed to use an environment variable at startup time.

func NewVariable

func NewVariable(key, defaultValue, description string) Variable

NewVariable initializes a environment.Variable

func (Variable) Value

func (v Variable) Value(lookup Environmenter) string

Value returns the value for an environment.Variable using a Getenver.

Jump to

Keyboard shortcuts

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