properties

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2022 License: MIT Imports: 4 Imported by: 0

README

Properties Module

GoDoc

A centralized configuration endpoint.

Why ?

We have a lot configuration possibilities with Golang, we need something open with large capabilities like Merge, subset, provider order, flags; and completly integrate with standard tools like Consul and etcd.

How ?

To simplify, it's a spf13/Viper extension and helper.

Usages

Take a look at functional properties_test.go in properties_test and properties_test.go in properties unit test.

Sample usage can be resume at :

To read Mode from command line and by default "prod", where resx is configuration directory store with app.json and prod.app.json . app.json and prod.app.json (or any profile as command line argument) will be merge.

''' // Create a configuration Object with default Env and flags c := properties.DefaultConfig() // customize some default value c.ConfigPathes = []string{"common/configs/server"} c.DefaultConfigMode = "production" // here a production.app.json will be search instead of prod.app.json

// Read flags, env, remote... and load properties
props := properties.New(c)
// load and merge mode properties
props.LoadModeProperties(true)

'''

Can be use to unmarshal any configuration from spf13/Viper:


type DummyConfig struct {
		BaseUrl    string
		SuccessUrl string
	}

    var C DummyConfig

	err := props.Unmarshal(&C)
	

Add some functions, like GetOrDie, GetStringOrDefault, ...etc.

Documentation

Index

Constants

View Source
const (
	// Default config type
	DefaultConfigType = "json"

	// Default config file name
	DefaultConfigName = "app"

	// Default config storage directory
	DefaultConfigDir = ""

	// Default config mode
	DefaultConfigMode = "prod"
	// Default test config mode spelling
	DefaultTestModeTag = "test"
)
View Source
const (
	ConfigNameTag = "config-name"
	ConfigDirTag  = "config-dir"
	ModeTag       = "mode"
	ConfigTypeTag = "config-type"
)

Flags Tag referrer e.g. ConfigNameTag imply => myexec --config-name "xxx"

Variables

This section is empty.

Functions

func CheckRunInTestEnvironment

func CheckRunInTestEnvironment() bool

CheckRunInTestEnvironment return true if this application is running with 'go test'

Types

type Config

type Config struct {

	// Define the config files type for K/V stores, allowed types are :
	// ["json", "toml", "yaml", "yml", "properties", "props", "prop"]
	// Default: json
	ConfigType string

	// Define to Environement variables to look up
	EnvVars []string

	// Define the name of config files (without extension) to look up for.
	// Default: "config"
	ConfigName string

	// Define the pathes where to lookup for config files
	ConfigPathes []string

	// Define the remote provides names:
	Providers []RemoteProvider

	// Define the flags to lookup for
	Flags []Flag

	// Overridable Mode Tag to use for test session by default set to DefaultTestModeTag
	TestModeTag string

	// Overridable default mode
	DefaultConfigMode string
}

Config is a struct that allows to initialize the Properties type with user defined values

func DefaultConfig

func DefaultConfig() (c Config)

DefaultConfig return a default configuration with Flags and Env already set...

func NewConfig

func NewConfig() Config

func (Config) GetDefaultModeProperties

func (c Config) GetDefaultModeProperties() *Properties

GetDefaultModeProperties get A Default Property set for classic app based on Flag with Config file : app.json in current directory from Flag With HOME and PWD from env

func (*Config) InitConfig

func (c *Config) InitConfig()

InitConfig init config with default value if not set

type Flag

type Flag struct {

	// Flag Name used in command line
	Name string

	// When gives the default flag value
	Default string

	// Usage string shown in the help
	Usage string
}

Flag is a struct that stores flags configuration

type Properties

type Properties struct {
	*viper.Viper
	Config Config
}

Properties is a struct that allow to handle application properties seting and acessing. It wraps spf13/viper library

func New

func New(config ...Config) *Properties

Properties constructor Settings default values if need

func (Properties) GetOrDie

func (p Properties) GetOrDie(key string) interface{}

GetOrDie get key, if not found panic

func (Properties) GetStringOrDefault

func (p Properties) GetStringOrDefault(key string, dlft string) string

GetStringOrDefault get string or a default value

func (Properties) GetSubOrDie

func (p Properties) GetSubOrDie(key string) *viper.Viper

GetOrDie get key, if not found panic

func (*Properties) LoadModeProperties

func (props *Properties) LoadModeProperties(panicOnModeLoad bool) *Properties

Helper to Load Properties and merge it with mode related Properties path will be use by default if user not provide a ConfigDirTag in command line defaultMode will be use by default if user not provide a ModeTag in command line props is used as properties base. panicOnModeLoad if true, when loading mode properties failed call "panic" otherwise "warning"

func (Properties) TryLoadRemoteProperties

func (p Properties) TryLoadRemoteProperties()

TryLoadRemoteProperties try load configuration from remote througth Viper

type RemoteProvider

type RemoteProvider struct {

	// Set the provider's name | must be "ectd" or "consul"
	Name string

	// Set the provider's url : "http://ip:port" for "etcd", "ip:port" for "consul"
	Url string

	// Set the path in the k/v store to retrieve configuration
	Path string

	// If set check the remote provider with encryption using the defined keyFile
	KeyFile string
}

Privider is a struct that hold remote providers data

Jump to

Keyboard shortcuts

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