Categorygithub.com/magiconair/properties
modulepackage
1.8.9
Repository: https://github.com/magiconair/properties.git
Documentation: pkg.go.dev

# README

License GoDoc

Overview

properties is a Go library for reading and writing properties files.

It supports reading from multiple files or URLs and Spring style recursive property expansion of expressions like ${key} to their corresponding value. Value expressions can refer to other keys like in ${key} or to environment variables like in ${USER}. Filenames can also contain environment variables like in /home/${USER}/myapp.properties.

Properties can be decoded into structs, maps, arrays and values through struct tags.

Comments and the order of keys are preserved. Comments can be modified and can be written to the output.

The properties library supports both ISO-8859-1 and UTF-8 encoded data.

Starting from version 1.3.0 the behavior of the MustXXX() functions is configurable by providing a custom ErrorHandler function. The default has changed from panic to log.Fatal but this is configurable and custom error handling functions can be provided. See the package documentation for details.

Read the full documentation on GoDoc

Getting Started

import (
	"flag"
	"github.com/magiconair/properties"
)

func main() {
	// init from a file
	p := properties.MustLoadFile("${HOME}/config.properties", properties.UTF8)

	// or multiple files
	p = properties.MustLoadFiles([]string{
			"${HOME}/config.properties",
			"${HOME}/config-${USER}.properties",
		}, properties.UTF8, true)

	// or from a map
	p = properties.LoadMap(map[string]string{"key": "value", "abc": "def"})

	// or from a string
	p = properties.MustLoadString("key=value\nabc=def")

	// or from a URL
	p = properties.MustLoadURL("http://host/path")

	// or from multiple URLs
	p = properties.MustLoadURL([]string{
			"http://host/config",
			"http://host/config-${USER}",
		}, true)

	// or from flags
	p.MustFlag(flag.CommandLine)

	// get values through getters
	host := p.MustGetString("host")
	port := p.GetInt("port", 8080)

	// or through Decode
	type Config struct {
		Host    string        `properties:"host"`
		Port    int           `properties:"port,default=9000"`
		Accept  []string      `properties:"accept,default=image/png;image;gif"`
		Timeout time.Duration `properties:"timeout,default=5s"`
	}
	var cfg Config
	if err := p.Decode(&cfg); err != nil {
		log.Fatal(err)
	}
}

Installation and Upgrade

$ go get -u github.com/magiconair/properties

License

2 clause BSD license. See LICENSE file for details.

ToDo

  • Dump contents with passwords and secrets obscured

# Packages

Package assert provides helper functions for testing.

# Functions

Load reads a buffer into a Properties struct.
LoadAll reads the content of multiple URLs or files in the given order into a Properties struct.
LoadFile reads a file into a Properties struct.
LoadFiles reads multiple files in the given order into a Properties struct.
LoadMap creates a new Properties struct from a string map.
LoadReader reads an io.Reader into a Properties struct.
LoadString reads an UTF8 string into a properties struct.
LoadURL reads the content of the URL into a Properties struct.
LoadURLs reads the content of multiple URLs in the given order into a Properties struct.
LogFatalHandler handles the error by logging a fatal error and exiting.
MustLoadAll reads the content of multiple URLs or files in the given order into a Properties struct.
MustLoadFile reads a file into a Properties struct and panics on error.
MustLoadFiles reads multiple files in the given order into a Properties struct and panics on error.
MustLoadSReader reads an io.Reader into a Properties struct and panics on error.
MustLoadString reads an UTF8 string into a Properties struct and panics on error.
MustLoadURL reads the content of a URL into a Properties struct and panics on error.
MustLoadURLs reads the content of multiple URLs in the given order into a Properties struct and panics on error.
NewProperties creates a new Properties struct with the default configuration for "${key}" expressions.
PanicHandler handles the error by panicking.

# Constants

ISO_8859_1 interprets the input data as ISO-8859-1.
UTF8 interprets the input data as UTF-8.

# Variables

ErrorHandler is the function which handles failures of the MustXXX() functions.
LogPrintf defines a log handler which uses log.Printf.

# Structs

A Properties contains the key/value pairs from the properties input.

# Type aliases

Encoding specifies encoding of the input data.
ErrorHandlerFunc defines the type of function which handles failures of the MustXXX() functions.
LogHandlerFunc defines the function prototype for logging errors.