ini

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"
	"os"
	"time"

	"github.com/zpatrick/cfg"
	"github.com/zpatrick/cfg/ini"
	"github.com/zpatrick/cfg/internal"
)

type Config struct {
	Timeout    time.Duration
	ServerPort int64
	ServerAddr string
}

func main() {
	const data = `
timeout = "5s"

[server]
port = 8080
addr = "localhost"
`

	path, err := internal.WriteTempFile("", data)
	if err != nil {
		panic(err)
	}
	defer os.Remove(path)

	iniFile, err := ini.Load(path)
	if err != nil {
		panic(err)
	}

	var c Config
	if err := cfg.Load(context.Background(), cfg.Schemas{
		"timeout": cfg.Schema[time.Duration]{
			Dest:     &c.Timeout,
			Provider: iniFile.Duration("", "timeout"),
		},
		"server.port": cfg.Schema[int64]{
			Dest:     &c.ServerPort,
			Provider: iniFile.Int64("server", "port"),
		},
		"server.addr": cfg.Schema[string]{
			Dest:     &c.ServerAddr,
			Provider: iniFile.String("server", "addr"),
		},
	}); err != nil {
		panic(err)
	}

	fmt.Printf("Timeout: %s ServerPort: %d ServerAddr: %s", c.Timeout, c.ServerPort, c.ServerAddr)
}
Output:

Timeout: 5s ServerPort: 8080 ServerAddr: localhost

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Provide

func Provide[T any](p *Provider, section string, key string, convert func(k *ini.Key) (T, error)) cfg.Provider[T]

Provide loads a *ini.Key from the given section and key from p. The convert paramter is then called to convert the *ini.Key into a T. If the key does not exist, a cfg.NoValueProvidedError error will be returned.

Types

type Provider

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

A Provider loads configuration values from a file in ini format.

func Load

func Load(path string) (*Provider, error)

Load reads and parses the ini file at the given path.

func New

func New(f *ini.File) (*Provider, error)

New returns a Provider which loads values from f.

func (*Provider) Bool

func (p *Provider) Bool(section, key string) cfg.Provider[bool]

Bool returns a bool configuration value at the given section and key.

func (*Provider) Duration

func (p *Provider) Duration(section, key string) cfg.Provider[time.Duration]

Duration returns a duration configuration value at the given section and key.

func (*Provider) Float64

func (p *Provider) Float64(section, key string) cfg.Provider[float64]

Float64 returns a float64 configuration value at the given section and key.

func (*Provider) Int

func (p *Provider) Int(section, key string) cfg.Provider[int]

Int returns an int configuration value at the given section and key.

func (*Provider) Int64

func (p *Provider) Int64(section, key string) cfg.Provider[int64]

Int64 returns an int64 configuration value at the given section and key.

func (*Provider) String

func (p *Provider) String(section, key string) cfg.Provider[string]

String returns a string configuration value at the given section and key.

func (*Provider) Uint64

func (p *Provider) Uint64(section, key string) cfg.Provider[uint64]

UInt64 returns an uint64 configuration value at the given section and key.

Jump to

Keyboard shortcuts

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