forked from appleboy/goConfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
129 lines (106 loc) · 2.77 KB
/
config.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//Package goconfig uses a struct as input and populates the
//fields of this struct with parameters fom command
//line, environment variables and configuration file.
package goconfig
import (
"errors"
"fmt"
"path"
"path/filepath"
"github.com/triamazikamno/goconfig/goenv"
"github.com/triamazikamno/goconfig/goflags"
)
// Tag to set main name of field
var Tag = "cfg"
// TagDefault to set default value
var TagDefault = "cfgDefault"
// Path sets default config path
var Path string
// File name of default config file
var File string
// FileRequired config file required
var FileRequired bool
// HelpString temporarily saves help
var HelpString string
// PrefixFlag is a string that would be placed at the beginning of the generated Flag tags.
var PrefixFlag string
// PrefixEnv is a string that would be placed at the beginning of the generated Event tags.
var PrefixEnv string
// ErrFileFormatNotDefined Is the error that is returned when there is no defined configuration file format.
var ErrFileFormatNotDefined = errors.New("file format not defined")
//Usage is a function to show the help, can be replaced by your own version.
var Usage func()
// Fileformat struct holds the functions to Load and Save the file containing the settings
type Fileformat struct {
Extension string
Save func(config interface{}) (err error)
Load func(config interface{}) (err error)
PrepareHelp func(config interface{}) (help string, err error)
}
// Formats is the list of registered formats.
var Formats []Fileformat
func findFileFormat(extension string) (format Fileformat, err error) {
format = Fileformat{}
for _, f := range Formats {
if f.Extension == extension {
format = f
return
}
}
err = ErrFileFormatNotDefined
return
}
func init() {
Usage = DefaultUsage
Path = "./"
File = ""
FileRequired = false
}
// Parse configuration
func Parse(config interface{}) (err error) {
ext := path.Ext(File)
if ext != "" {
var format Fileformat
format, err = findFileFormat(ext)
if err != nil {
return
}
err = format.Load(config)
if err != nil {
return
}
HelpString, err = format.PrepareHelp(config)
if err != nil {
return
}
}
goenv.Prefix = PrefixEnv
goenv.Setup(Tag, TagDefault)
err = goenv.Parse(config)
if err != nil {
return
}
goflags.Prefix = PrefixFlag
goflags.Setup(Tag, TagDefault)
goflags.Usage = Usage
goflags.Preserve = true
err = goflags.Parse(config)
if err != nil {
return
}
return
}
// PrintDefaults print the default help
func PrintDefaults() {
if File != "" {
fmt.Printf("Config file %q:\n", filepath.Join(Path, File))
fmt.Println(HelpString)
}
}
// DefaultUsage is assigned for Usage function by default
func DefaultUsage() {
fmt.Println("Usage")
goflags.PrintDefaults()
goenv.PrintDefaults()
PrintDefaults()
}