forked from miguelbranco80/goconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
67 lines (47 loc) · 1.86 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
This package implements a parser for configuration files.
This allows easy reading and writing of structured configuration files.
Given a sample configuration file:
[default]
host=www.example.com
protocol=http://
base-url=%(protocol)s%(host)s
[service-1]
url=%(base-url)s/some/path
mysecret=$(SECRET_ENV)s
delegation : on
maxclients=200 # do not set this higher
comments=This is a multi-line
entry ; And this is a comment
To read this configuration file, do:
c, err := configfile.ReadConfigFile("config.cfg");
// result is string :http://www.example.com/some/path"
c.GetString("service-1", "url");
c.GetString("service-1", "mysecret"); // result is os.Getenv("SECRET_ENV")
c.GetInt64("service-1", "maxclients"); // result is int 200
c.GetBool("service-1", "delegation"); // result is bool true
// result is string "This is a multi-line\nentry"
c.GetString("service-1", "comments");
Note the support for unfolding variables (such as %(base-url)s), which are
read from the special (reserved) section name [default].
Also note the support for environment variables (such as $(MY_ENV_VAR)s), which
are read from the system environment variables.
A new configuration file can also be created with:
c := configfile.NewConfigFile();
c.AddSection("section");
c.AddOption("section", "option", "value");
// use 0644 as file permission
c.WriteConfigFile("config.cfg", 0644, "A header for this file");
This results in the file:
# A header for this file
[section]
option=value
Note that sections and options are case-insensitive (values are
case-sensitive) and are converted to lowercase when saved to a file.
The functionality and workflow is loosely based on the configparser.py
package of the Python Standard Library.
To install:
go get "github.com/msbranco/goconfig"
To test:
go test
To use:
import "github.com/msbranco/goconfig"