config

package module
v0.0.0-...-0a9be90 Latest Latest
Warning

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

Go to latest
Published: May 28, 2020 License: MIT Imports: 9 Imported by: 7

README

Config Loader for Golang

Load config into go struct from shell environment and docker/kubernetes secrets.

Install

go get github.com/hack-fan/config

Features

  • Load from shell environment variables
  • Load from Docker/Kubernetes secrets
  • Default values support
  • Required check support
  • Simple and easy to use, no other features

Load Order

Default -> ENV -> Secret -> Value exists in struct

Right side will overwrite left side.

Quick Start

package main

import (
    "fmt"
    "github.com/hack-fan/config"
)

type Settings struct {
    AppName string `default:"app"` // env APP_NAME will overwrite default value
    DB      struct {
        Name     string
        User     string `required:"true"`
        Password string `secret:"mysql_db_password"` // default secret name is 'db_password',change it use tag
        Port     int    `default:"3306" env:"MYSQL_DB_PORT"` // default env name is 'DB_PORT',change it use tag
    }
}

func main() {
    var settings = new(Settings)
    config.MustLoad(settings)
    fmt.Printf("%+v",settings)
}

Name Conversion

  • ENV will use ALL_CAP_SNAKE_CASE
  • Secret will use snake_case

Tags

  • default set default value
  • env custom shell env variable names
  • secret custom secret file name
  • required set attr as required

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(dst interface{}) error

Load config to `dst` struct pointer from shell env variables and docker secrets.

func LoadEnv

func LoadEnv(dst interface{}) error

LoadEnv load config to `dst` struct pointer from shell env variables only

func LoadEnvAndDockerSecret

func LoadEnvAndDockerSecret(dst interface{}) error

LoadEnvAndDockerSecret load config to `dst` struct pointer from shell env variables and docker secrets.

func LoadEnvAndKubernetesSecret

func LoadEnvAndKubernetesSecret(dst interface{}) error

LoadEnvAndKubernetesSecret load config to `dst` struct pointer from shell env variables and kubernetes secrets.

func LoadEnvAndSecret

func LoadEnvAndSecret(dst interface{}, secretPath string) error

LoadEnvAndSecret load config to `dst` struct pointer from shell env variables and container secrets.

func MustLoad

func MustLoad(dst interface{})

MustLoad load config to `dst` struct pointer from shell env variables and docker secrets. It panic when an error occurs.

Types

This section is empty.

Jump to

Keyboard shortcuts

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