configfile

package module
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2023 License: MIT Imports: 8 Imported by: 13

README

configfile

Build Status codecov Go Report Card GoDoc

Read config from file, useful when read data from kubernetes configmaps, and secret.

Example

package main

import (
    "fmt"
    "net/http"

    "github.com/acoshift/configfile"
    "github.com/garyburd/redigo/redis"
)

var config = configfile.NewReader("config")

var (
    addr      = config.StringDefault("addr", ":8080")
    redisAddr = config.MustString("redis_addr")
    redisPass = config.String("redis_pass")
    redisDB   = config.Int("redis_db")
)

func main() {
    pool := redis.Pool{
        Dial: func() (redis.Conn, error) {
            return redis.Dial(
                "tcp",
                redisAddr,
                redis.DialPassword(redisPass),
                redis.DialDatabase(redisDB),
            )
        },
    }

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        c := pool.Get()
        defer c.Close()
        cnt, err := redis.Int64(c.Do("INCR", "cnt"))
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
        fmt.Fprintf(w, "count: %d", cnt)
    })

    http.ListenAndServe(addr, nil)
}

Example YAML

package main

import (
    "log"
    "net/http"

    "github.com/acoshift/configfile"
)

func main() {
    var config = configfile.NewReader("testdata/config.yaml")
    // or use NewYAMLReader
    var config = configfile.NewYAMLReader("testdata/config.yaml")

    log.Println(config.Bool("data1")) // true
    log.Println(config.String("data2")) // false
    log.Println(config.Int("data3")) // 9
    log.Println(config.Int("data4")) // 0
    log.Println(config.String("empty")) // ""
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadDotEnv added in v1.6.0

func LoadDotEnv(filename ...string) error

Types

type Reader

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

Reader is the config reader

func NewDirReader added in v1.2.0

func NewDirReader(base string) *Reader

NewDirReader creates new config dir reader

func NewEnvReader added in v1.4.0

func NewEnvReader() *Reader

NewEnvReader creates new env reader

func NewReader

func NewReader(base string) *Reader

NewReader creates new config reader

func NewYAMLReader added in v1.2.0

func NewYAMLReader(filename string) *Reader

NewYAMLReader creates new yaml reader from file

func NewYAMLReaderFromReader added in v1.7.0

func NewYAMLReaderFromReader(r io.Reader) *Reader

NewYAMLReaderFromReader creates new yaml reader from io.Reader

func (*Reader) Base64 added in v1.5.0

func (r *Reader) Base64(name string) []byte

Base64 reads string from config file then decode using base64

func (*Reader) Base64Default added in v1.5.0

func (r *Reader) Base64Default(name string, def []byte) []byte

Base64Default reads string from config file then decode using base64 if error, will return default value

func (*Reader) Bool

func (r *Reader) Bool(name string) bool

Bool reads bool from config file, see BoolDefault

func (*Reader) BoolDefault

func (r *Reader) BoolDefault(name string, def bool) bool

BoolDefault reads bool from config file with default value, result is false if lower case data is "", "0", or "false", otherwise true

func (*Reader) Bytes

func (r *Reader) Bytes(name string) []byte

Bytes reads bytes from config file

func (*Reader) BytesDefault

func (r *Reader) BytesDefault(name string, def []byte) []byte

BytesDefault reads bytes from config file with default value

func (*Reader) Duration added in v1.3.0

func (r *Reader) Duration(name string) time.Duration

Duration reads string then parse as duration from config file

func (*Reader) DurationDefault added in v1.3.0

func (r *Reader) DurationDefault(name string, def time.Duration) time.Duration

DurationDefault reads string then parse as duration from config file with default value

func (*Reader) Fallback added in v1.5.0

func (r *Reader) Fallback(f *Reader) *Reader

func (*Reader) Float32 added in v1.9.0

func (r *Reader) Float32(name string) float32

Float32 reads float32 from config file

func (*Reader) Float32Default added in v1.9.0

func (r *Reader) Float32Default(name string, def float32) float32

Float32Default reads float32 from config file with default value

func (*Reader) Float64 added in v1.9.0

func (r *Reader) Float64(name string) float64

Float64 reads float64 from config file

func (*Reader) Float64Default added in v1.9.0

func (r *Reader) Float64Default(name string, def float64) float64

Float64Default reads float64 from config file with default value

func (*Reader) Int

func (r *Reader) Int(name string) int

Int reads int from config file

func (*Reader) Int64 added in v1.3.0

func (r *Reader) Int64(name string) int64

Int64 reads int from config file

func (*Reader) Int64Default added in v1.3.0

func (r *Reader) Int64Default(name string, def int64) int64

Int64Default reads int64 from config file with default value

func (*Reader) IntDefault

func (r *Reader) IntDefault(name string, def int) int

IntDefault reads int from config file with default value

func (*Reader) MustBase64 added in v1.5.0

func (r *Reader) MustBase64(name string) []byte

MustBase64 reads string from config file then decode using base64 if error, will panic

func (*Reader) MustBool

func (r *Reader) MustBool(name string) bool

MustBool reads bool from config file, see BoolDefault, panic if file not exists

func (*Reader) MustBytes added in v1.1.0

func (r *Reader) MustBytes(name string) []byte

MustBytes reads bytes from config file, panic if file not exists

func (*Reader) MustDuration added in v1.3.0

func (r *Reader) MustDuration(name string) time.Duration

MustDuration reads string then parse as duration from config file, panic if file not exists

func (*Reader) MustFloat32 added in v1.9.0

func (r *Reader) MustFloat32(name string) float32

MustFloat32 reads float32 from config file, panic if file not exists or data can not parse to float32

func (*Reader) MustFloat64 added in v1.9.0

func (r *Reader) MustFloat64(name string) float64

MustFloat64 reads float64 from config file, panic if file not exists or data can not parse to float64

func (*Reader) MustInt

func (r *Reader) MustInt(name string) int

MustInt reads int from config file, panic if file not exists or data can not parse to int

func (*Reader) MustInt64 added in v1.3.0

func (r *Reader) MustInt64(name string) int64

MustInt64 reads int64 from config file, panic if file not exists or data can not parse to int64

func (*Reader) MustString

func (r *Reader) MustString(name string) string

MustString reads string from config file, panic if file not exists

func (*Reader) String

func (r *Reader) String(name string) string

String reads string from config file

func (*Reader) StringDefault

func (r *Reader) StringDefault(name string, def string) string

StringDefault reads string from config file with default value

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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