templig

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: MPL-2.0 Imports: 9 Imported by: 0

README

Logo
Test Pipeline Result CodeQL Pipeline Result Security Pipeline Result Go Report Card Code Coverage OpenSSF Best Practises OpenSSF Scorecard FOSSA Status FOSSA Status GoDoc Reference

templig

templig is configuration library utilizing the text templating engine and the functions best known from helm charts, that originally stem from Masterminds/sprig. Its primary goal is to enable access to the system environment to fill information using the env function. It also enables to include verifications inside the configuration.

Usage

Simple Case

Having a configuration file like the following:

id:   23
name: Interesting Name

The code to read that file would look like this:

package main

import (
	"fmt"

	"github.com/AlphaOne1/templig"
)

type Config struct {
	ID   int    `yaml:"id"`
	Name string `yaml:"name"`
}

func main() {
	c, confErr := templig.FromFile[Config]("my_config.yaml")

	fmt.Printf("read errors: %v", confErr)

	if confErr == nil {
		fmt.Printf("ID:   %v\n", c.Get().ID)
		fmt.Printf("Name: %v\n", c.Get().Name)
	}
}

The Get method gives a pointer to the internally held Config structure that the use supplied. The pinter is always non-nil, so additional nil-checks are not necessary.

Advanced Case

Having a templated configuration file like this one:

id:   23
name: Interesting Name
pass: {{ env "PASSWORD" | required "password required" | quote }}

As demonstrated, one can use the templating functionality that is best known from helm charts. The functions provided come from the aforementioned sprig-library.

package main

import (
	"fmt"
	"strings"

	"github.com/AlphaOne1/templig"
)

type Config struct {
	ID   int    `yaml:"id"`
	Name string `yaml:"name"`
	Pass string `yaml:"pass"`
}

func main() {
	c, confErr := templig.FromFile[Config]("my_config.yaml")

	fmt.Printf("read errors: %v", confErr)

	if confErr == nil {
		fmt.Printf("ID:   %v\n", c.Get().ID)
		fmt.Printf("Name: %v\n", c.Get().Name)
		fmt.Printf("Pass: %v\n", strings.Repeat("*", len(c.Get().Pass)))
	}
}

Documentation

Index

Constants

View Source
const SecretDefaultRE = "(key)|(secret)|(pass)|(password)|(cert)|(certificate)"

SecretDefaultRE is the default regular expression used to identify secret values automatically.

Variables

SecretRE is the regular expression used to identify secret values automatically. In case there are different properties to identify secrets, extend it.

Functions

This section is empty.

Types

type Config

type Config[T any] struct {
	// contains filtered or unexported fields
}

Config is the generic structure holding the configuration information for the specified type.

func From

func From[T any](r io.Reader) (*Config[T], error)

From reads a configuration from the given io.Reader.

func FromFile

func FromFile[T any](path string) (*Config[T], error)

FromFile loads a configuration from a file with the given name.

func (*Config[T]) Get

func (c *Config[T]) Get() *T

Get gives a pointer to the deserialized configuration.

func (*Config[T]) To

func (c *Config[T]) To(w io.Writer) error

To writes a configuration to the given io.Writer.

func (*Config[T]) ToFile

func (c *Config[T]) ToFile(path string) error

ToFile saves a configuration to a file with the given name, replacing it in case.

func (*Config[T]) ToSecretsHidden

func (c *Config[T]) ToSecretsHidden(w io.Writer) error

ToSecretsHidden writes the configuration to the given io.Writer and hides secret values using the SecretRE. Strings are replaced with the number of * corresponding to their length. Substructures (e.g. `"secrets": [ "a", "b", "c"]`) containing secrets, are replaced with a single *.

Directories

Path Synopsis
examples
example0 command
example1 command

Jump to

Keyboard shortcuts

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