configs

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: Apache-2.0 Imports: 7 Imported by: 1

README

Library for working with configs

The library is designed for parsing a file with configs.

The library supports the following types of configs:

  • string
  • int
  • float
  • bool
  • duration
  • int_map
  • string_slice

How to use it

Creating two yaml files.
  • In the first one, we denote all keys types and default values for configs_keys.yml
config:
  db_host:
    type: string
    default: "localhost"
    source: yaml
  db_name:
    type: string
    default: "test"
    source: yaml
  db_user:
    type: string
    default: "root"
    source: yaml
  db_password:
    type: string
    default: "qwerty"
    source: yaml
  db_port:
    type: int
    default: "5432"
    source: yaml
  • In the second one, the real values of these configs are configs.yml
values:
  db_host: localhost
  db_name: prod
  db_user: user
  db_password: 12345678
  db_port: 5432
Example
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/porebric/configs"
)

const (
	configsKeysPath = "./configs_keys.yml"
	configsPath     = "./configs.yml"
)

func main() {
	ctx := context.Background()
	keysReader, err := os.Open(configsKeysPath)
	if err != nil {
		panic(err)
	}

	confReader, err := os.Open(configsPath)
	if err != nil {
		panic(err)
	}

	if err = configs.New().KeysReader(keysReader).YamlConfigs(confReader).Init(ctx); err != nil {
		panic(err)
	}

	fmt.Println(configs.Value(ctx, "db_host").String())
	fmt.Println(configs.Value(ctx, "db_user").String())
        fmt.Println(configs.Value(ctx, "db_password").String())
        fmt.Println(configs.Value(ctx, "db_name").String())
        fmt.Println(configs.Value(ctx, "db_port").Int())
}

P.S. The supported types and values can be seen in config_test.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Value

func Value(ctx context.Context, key string) *config.Value

func Watch

func Watch(ctx context.Context, key string, fns ...config.WatchFn) *config.Value

Types

type Storage

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

func New

func New() *Storage

func (*Storage) Init

func (s *Storage) Init(ctx context.Context) error

func (*Storage) KeysReader

func (s *Storage) KeysReader(r io.Reader) *Storage

func (*Storage) YamlConfigs

func (s *Storage) YamlConfigs(r io.Reader) *Storage

Directories

Path Synopsis
env

Jump to

Keyboard shortcuts

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