config

package module
v0.0.0-...-243334d Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2025 License: MIT Imports: 16 Imported by: 3

README

= config
Matt Nicholls <transientvariable@protonmail.com>
:keywords: golang,config,configuration
:experimental: true
:icons: font
:iconfont-cdn: //cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/fontawesome.min.css
:imagesdir: docs/image
:sectanchors: true
:source-highlighter: prettify
:toc: left
:toclevels: 3
:toc-title: Contents

ifdef::env-github[]
:tip-caption: :bulb:
:note-caption: :information_source:
:important-caption: :heavy_exclamation_mark:
:caution-caption: :fire:
:warning-caption: :warning:
endif::[]

== Overview

Compact, no frills, configuration library for Go.

== Installation

.Prerequisites
* The link:https://golang.org/dl/[Golang Runtime], version 1.24.x or later

[source%nowrap,bash]
----
❯ go get -u github.com/transientvariable/config-go
----

== License
This project is licensed under the link:LICENSE[MIT License].

Documentation

Index

Constants

View Source
const (
	ErrNotInitialized = configErr("not initialized")
	ErrPathNotFound   = configErr("path not found")
)

Enumeration of errors that may be returned by configuration operations.

Variables

This section is empty.

Functions

func Bool

func Bool(path string) (bool, error)

Bool retrieves the boolean value for the provided path.

The returned error will be non-nil if the value corresponding to the provided path:

  • could not be found
  • was found, but could not be parsed as a boolean

func BoolMustResolve

func BoolMustResolve(path string) bool

BoolMustResolve is similar behavior to Bool, but panics if an error occurs.

func Duration

func Duration(path string) (time.Duration, error)

Duration retrieves the time.Duration value for the provided path.

The returned error will be non-nil if the value corresponding to the provided path:

  • could not be found
  • was found, but could not be parsed as a Duration

func DurationMustResolve

func DurationMustResolve(path string) time.Duration

DurationMustResolve is similar behavior to Duration, but panics if an error occurs.

func Float

func Float(path string) (float64, error)

Float retrieves the float value for the provided path.

The returned error will be non-nil and the returned float value will be set to 0 if the value corresponding to the provided path:

  • could not be found
  • was found, but could not be parsed as a float

func FloatMustResolve

func FloatMustResolve(path string) float64

FloatMustResolve is similar behavior to Float, but panics if an error occurs.

func HasPath

func HasPath(path string) (bool, error)

HasPath checks whether a configuration value is present for the provided path.

func Int

func Int(path string) (int, error)

Int retrieves the integer value for the provided path.

The returned error will be non-nil and the returned integer value will be set to 0 if the value corresponding to the provided path:

  • could not be found
  • was found, but could not be parsed as an integer

func IntMustResolve

func IntMustResolve(path string) int

IntMustResolve is similar behavior to Int, but panics if an error occurs.

func Load

func Load(options ...func(*Option)) error

Load reads and parses the configuration using the provided optional properties.

If an error occurs during read/parse operations, error will be non-nil.

func Multiaddr

func Multiaddr(path string) (multiaddr.Multiaddr, error)

Multiaddr retrieves the multiaddr.Multiaddr value for the provided path.

The returned error will be non-nil if the value corresponding to the provided path:

  • could not be found
  • was found, but could not be parsed as a multiaddr.Multiaddr value

func MultiaddrMustResolve

func MultiaddrMustResolve(path string) multiaddr.Multiaddr

MultiaddrMustResolve is similar in behavior to Multiaddr, but panics if an error occurs.

func Set

func Set(path string, value string) (bool, error)

Set sets or replaces a configuration value for the provided path.

Returns:

  • true if the value corresponding to the provided path was successfully replaced
  • false if the configuration does not contain the specified path or otherwise could not replace the value

func Size

func Size(path string) (int64, error)

Size retrieves the size value for the provided path.

The returned error will be non-nil if the value corresponding to the provided path:

  • could not be found
  • was found, but could not be parsed as a size value

func SizeMustResolve

func SizeMustResolve(path string) int64

SizeMustResolve is similar behavior to Size, but panics if an error occurs.

func String

func String() string

String returns a string representation of the configuration.

func Time

func Time(path string) (time.Time, error)

Time retrieves the time value for the provided path.

The returned error will be non-nil if the value corresponding to the provided path:

  • could not be found
  • was found, but could not be parsed as a time.Time value

func TimeMustResolve

func TimeMustResolve(path string) time.Time

TimeMustResolve is similar behavior to Time, but panics if an error occurs.

func URL

func URL(path string) (*url.URL, error)

URL retrieves the url.URL value for the provided path.

The returned error will be non-nil if the value corresponding to the provided path:

  • could not be found
  • was found, but could not be parsed as a url.URL value

func URLMustResolve

func URLMustResolve(path string) *url.URL

URLMustResolve is similar in behavior to URL, but panics if an error occurs.

func Value

func Value(path string) (string, error)

Value retrieves the value for the provided path.

The returned error will be non-nil if:

  • the configuration has not been initialized
  • the value corresponding to the provided path could not be found

func ValueMustResolve

func ValueMustResolve(path string) string

ValueMustResolve is similar behavior to Value, but panics if an error occurs.

func ValuesMustResolve

func ValuesMustResolve(path string) []string

ValuesMustResolve is similar behavior to values, but panics if an error occurs.

func WithFilePath

func WithFilePath(filePath string) func(*Option)

WithFilePath sets the file path Option for the configuration. If the file path is not provided, the root of the project directory will be used for loading the configuration.

Types

type Option

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

Option is a container for optional properties that can be used for initializing the configuration.

type Path

type Path string

Path represents a Configuration path.

func Root

func Root() Path

Root returns the root configuration Path.

func Sub

func Sub(path string) ([]Path, error)

Sub returns the sub-paths for the provided path.

func (Path) Base

func (p Path) Base() string

Base returns last path element for the Configuration Path. For example, if Path.String returns `foo.bar.gopher`, then Base would return `gopher`.

func (Path) Depth

func (p Path) Depth() int

Depth returns the number of elements contained in the Path.

func (Path) Empty

func (p Path) Empty() bool

Empty returns whether the Path is empty

func (Path) Equals

func (p Path) Equals(path Path) bool

Equals returns whether the provided path equals Path.

func (Path) Join

func (p Path) Join(path Path) Path

Join joins Path with the provided Path.

func (Path) MarshalText

func (p Path) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler for the Path.

func (Path) String

func (p Path) String() string

String returns the raw string value for the Configuration Path.

type PathError

type PathError struct {
	Err       error
	Operation string
	Path      string
}

PathError is used for recording errors that may occur when parsing values from configuration paths.

func (*PathError) Error

func (e *PathError) Error() string

Error returns the error message for the PathError.

func (*PathError) Unwrap

func (e *PathError) Unwrap() error

Jump to

Keyboard shortcuts

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