tinyviper

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2024 License: MIT Imports: 5 Imported by: 0

README

tiny-viper

ci workflow

A minimalistic approach to spf13/viper.

Features

  • Read ENV variables into a struct
  • Read a .env file into a struct
  • < 110 source lines of code
  • No dependencies

Only string fields are supported.

Usage

package main

import (
	"github.com/nobloat/tinyviper"
	"fmt"
)

type Config struct {
	UserConfig struct {
		Email    string `env:"MY_APP_EMAIL"`
		Password string `env:"MY_APP_PASSWORD"`
        someOtherProperty string
	}
	Endpoint string `env:"MY_APP_ENDPOINT"`
}

func main() {
  cfg := Config{Endpoint: "some default endpoint"}
  err := tinyviper.LoadFromResolver(&cfg, tinyviper.NewEnvResolver(), tinyviper.NewEnvFileResolver(".env.sample"))
  if err != nil {
    panic(err)
  }

  fmt.Println("%+v", cfg)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadFromResolver

func LoadFromResolver[T any](cfg *T, resolver ...Resolver) error

Types

type EnvFileResolver

type EnvFileResolver struct {
	Variables map[string]string
}

func (EnvFileResolver) Get

func (e EnvFileResolver) Get(key string) string

type EnvResolver

type EnvResolver struct{}

func (EnvResolver) Get

func (e EnvResolver) Get(key string) string

type Resolver

type Resolver interface {
	Get(key string) string
}

func NewEnvFileResolver

func NewEnvFileResolver(filename string) Resolver

func NewEnvResolver added in v0.2.0

func NewEnvResolver() Resolver

Jump to

Keyboard shortcuts

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