Skip to content

Latest commit

 

History

History
87 lines (56 loc) · 2.36 KB

File metadata and controls

87 lines (56 loc) · 2.36 KB

Greenbone Logo

configReader Package Documentation

The configReader package provides a function ReadEnvVarsIntoStruct that can be used to read environment variables with predefined defaults.

Usage

To use the configReader package, first create a struct that you want to read environment variables into:

type Config struct {
	Port     int    `viperEnv:"port"`
	Host     string `viperEnV:"host" default:"localhost"`
	LogLevel string `viperEnv:"LOG_LEVEL" default:"info"`
}

Then, in your code, you can use the ReadEnvVarsIntoStruct function to read environment variables into the struct:

package main

import (
	"github.com/greenbone/opensight-golang-libraries/configReader"
)

func main() {
	var config Config
	err := configReader.ReadEnvVarsIntoStruct(&config)
	if err != nil {
		panic(err)
	}
	// use the config
}

Any environment variables that match the struct fields will be read into the struct. The following environment variables will be read into the Config struct:

PORT=8080
HOST=localhost
LOG_LEVEL=debug

If a field in the struct has a viperEnv tag, the environment variable will be matched to that tag. Otherwise, the field name will be used as the tag.

If a field in the struct has a default tag, a default value will be used if the environment variable is not set.

The configReader package uses Viper for reading environment variables, and Zerolog for logging.


configReader

import "github.com/greenbone/opensight-golang-libraries/pkg/configReader"

Index

func ReadEnvVarsIntoStruct(s any) (any, error)

ReadEnvVarsIntoStruct reads environment variables into a given struct

Generated by gomarkdoc

License

Copyright (C) 2022-2023 [Greenbone AG][Greenbone AG]

Licensed under the GNU General Public License v3.0 or later.