config

package
v1.8.4 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package config to merge run options: command line arguments and ini-file content. Command line arguments take precedence over ini-file

Index

Constants

View Source
const (
	IniFile      = "OpenM.IniFile" // ini-file path
	IniFileShort = "ini"           // ini-file path (short form)
)

Standard config keys to get values from ini-file or command line arguments

View Source
const (
	LogToConsole      = "OpenM.LogToConsole"     // if true then log to standard output
	LogToConsoleShort = "v"                      // if true then log to standard output (short form)
	LogToFile         = "OpenM.LogToFile"        // if true then log to file
	LogFilePath       = "OpenM.LogFilePath"      // log file path, default = current/dir/exeName.log
	LogUseTs          = "OpenM.LogUseTimeStamp"  // if true then use time-stamp in log file name
	LogUsePid         = "OpenM.LogUsePidStamp"   // if true then use pid-stamp in log file name
	LogUseDaily       = "OpenM.LogUseDailyStamp" // if true then use daily-stamp in log file name
	LogSql            = "OpenM.LogSql"           // if true then log sql statements into log file
)
Log config keys.

Log can be enabled/disabled for two independent streams:

console  => standard output stream
log file => log file, truncated on every run, (optional) unique "stamped" name

"Stamped" file name produced by adding time-stamp and/or pid-stamp, i.e.:

exeName.log => exeName.2012_08_17_16_04_59_148.123.log

Variables

This section is empty.

Functions

func New

func New(encodingKey string, optFs []FullShort) (*RunOptions, *LogOptions, []string, error)

New combines command-line arguments and ini-file options.

encodingKey, if not empty, is a name of command-line option to specify encoding (code page) of source text files, for example: -dbcopy.CodePage=windows-1252. If encoding value specified then ini-file and csv files converted from such encoding to utf-8. If encoding not specified then auto-detection and default values are used (see helper.FileToUtf8())

Return 1. *RunOptions: is merge of command line key=value and ini-file section.key=value options. 2. *LogOptions: openM++ log file settings, also merge of command line and ini-file. 3. Args []string: remaining command line arguments, after last recognized key=value 4. error or nil on success

func NewIni

func NewIni(iniPath string, encodingName string) (map[string]string, error)

NewIni read ini-file content into map of (section.key)=>value. It is very light and able to parse:

dsn = "DSN='server'; UID='user'; PWD='pas#word';"   ; comments are # here

Section and key are trimed and cannot contain comments ; or # chars inside. Key and values trimed and "unquoted". Multi-line values not supported. Key or value escaped with "double" or 'single' quotes can include spaces or ; or # chars

Example:

; comments can start from ; or
# from # and empty lines are skipped

 [section test]  ; section comment
 val = no comment
 rem = ; comment only and empty value
 nul =
 dsn = "DSN='server'; UID='user'; PWD='pas#word';" ; quoted value
 t w = the "# quick #" brown 'fox ; jumps' over    ; escaped: ; and # chars
 " key "" 'quoted' here " = some value
 qts = " allow ' unbalanced quotes                 ; with comment

Types

type FullShort

type FullShort struct {
	Full  string // full option name
	Short string // short option name
}

FullShort is pair of full option name and short option name

type LogOptions

type LogOptions struct {
	LogPath   string // path to log file
	IsConsole bool   // if true then log to standard output, default: true
	IsFile    bool   // if true then log to file
	IsLogSql  bool   // if true then log sql statements
	TimeStamp string // log timestamp string, ie: 2012_08_17_16_04_59_148
	IsDaily   bool   // if true then use daily log file names, ie: exeName.20120817.log
}

LogOptions for console and log file output

type RunOptions

type RunOptions struct {
	KeyValue        map[string]string // (key=>value) from command line arguments and ini-file
	DefaultKeyValue map[string]string // default (key=>value), if non-empty default for command line argument
	// contains filtered or unexported fields
}

RunOptions is (key,value) map of command line arguments and ini-file. For ini-file options key is combined as section.key

func (*RunOptions) Bool

func (opts *RunOptions) Bool(key string) bool

Bool return boolean value by key. If value not defined by command line argument or ini-file option or cannot be converted to boolean (see strconv.ParseBool) then return false

func (*RunOptions) Float

func (opts *RunOptions) Float(key string, defaultValue float64) float64

Float return 64 bit float value by key. If value not defined by command line argument or ini-file option or cannot be converted to float64 then default is returned

func (*RunOptions) Int

func (opts *RunOptions) Int(key string, defaultValue int) int

Int return integer value by key. If value not defined by command line argument or ini-file option or cannot be converted to integer then default is returned

func (*RunOptions) Int64

func (opts *RunOptions) Int64(key string, defaultValue int64) int64

Int64 return 64 bit integer value by key. If value not defined by command line argument or ini-file option or cannot be converted to int64 then default is returned

func (*RunOptions) IsExist

func (opts *RunOptions) IsExist(key string) bool

IsExist return true if key is defined as command line argument or ini-file option.

func (*RunOptions) String

func (opts *RunOptions) String(key string) string

String return value by key. It can be defined as command line argument or ini-file option or command line default

func (*RunOptions) StringExist

func (opts *RunOptions) StringExist(key string) (val string, isExist, isDefaultArg bool)

StringExist return value by key and boolean flags: isExist=true if value defined as command line argument or ini-file option, isDefault=true if value defined as non-empty default for command line argument.

func (*RunOptions) Uint64

func (opts *RunOptions) Uint64(key string, defaultValue uint64) uint64

Uint64 return unsigned 64 bit integer value by key. If value not defined by command line argument or ini-file option or cannot be converted to uint64 then default is returned

Jump to

Keyboard shortcuts

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