Skip to content

Commit 79793c8

Browse files
committed
set config data
1 parent 66a5024 commit 79793c8

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ build:
2323
tests:
2424
devtool test
2525

26-
.PHONY: pre-commite
27-
pre-commite: setup lint build tests
26+
.PHONY: pre-commit
27+
pre-commit: setup lint build tests
2828

2929
.PHONY: ci
3030
ci: install setup lint build tests

application.go

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,26 @@ type Grape interface {
3333
ExitFunc(call func(code int)) Grape
3434
}
3535

36-
type _grape struct {
37-
appName string
38-
configFilePath string
39-
pidFilePath string
40-
resolvers []config.Resolver
41-
configs Modules
42-
modules Modules
43-
packages container.TContainer
44-
logHandler *_log
45-
logger logx.Logger
46-
appContext xc.Context
47-
exitFunc func(code int)
48-
}
36+
type (
37+
_conf struct {
38+
FileName string
39+
Data string
40+
DataExt string
41+
}
42+
_grape struct {
43+
appName string
44+
config _conf
45+
pidFilePath string
46+
resolvers []config.Resolver
47+
configs Modules
48+
modules Modules
49+
packages container.TContainer
50+
logHandler *_log
51+
logger logx.Logger
52+
appContext xc.Context
53+
exitFunc func(code int)
54+
}
55+
)
4956

5057
// New create application
5158
func New(appName string) Grape {
@@ -82,7 +89,18 @@ func (a *_grape) Modules(modules ...interface{}) Grape {
8289

8390
// ConfigFile set config file path
8491
func (a *_grape) ConfigFile(filename string) Grape {
85-
a.configFilePath = filename
92+
a.config = _conf{
93+
FileName: filename,
94+
}
95+
return a
96+
}
97+
98+
// ConfigData set config data
99+
func (a *_grape) ConfigData(data, ext string) Grape {
100+
a.config = _conf{
101+
Data: data,
102+
DataExt: ext,
103+
}
86104
return a
87105
}
88106

@@ -218,12 +236,15 @@ func (a *_grape) prepareConfig(interactive bool) {
218236

219237
// read config file
220238
resolver := config.New(a.resolvers...)
221-
if len(a.configFilePath) > 0 {
222-
console.FatalIfErr(resolver.OpenFile(a.configFilePath), "Open config file: %s", a.configFilePath)
239+
switch true {
240+
case len(a.config.Data) > 0:
241+
resolver.OpenBlob(a.config.Data, a.config.DataExt)
242+
case len(a.config.FileName) > 0:
243+
console.FatalIfErr(resolver.OpenFile(a.config.FileName), "Open config file: %s", a.config)
223244
}
224-
console.FatalIfErr(resolver.Build(), "Prepare config file: %s", a.configFilePath)
245+
console.FatalIfErr(resolver.Build(), "Prepare config file: %s", a.config)
225246
if !interactive {
226-
console.FatalIfErr(resolver.Decode(appConfig), "Decode config file: %s", a.configFilePath)
247+
console.FatalIfErr(resolver.Decode(appConfig), "Decode config file: %s", a.config)
227248
}
228249

229250
// init logger
@@ -242,7 +263,7 @@ func (a *_grape) prepareConfig(interactive bool) {
242263
configs, err = reflect.TypingPtr(a.configs, func(c interface{}) error {
243264
return resolver.Decode(c)
244265
})
245-
console.FatalIfErr(err, "Decode config file: %s", a.configFilePath)
266+
console.FatalIfErr(err, "Decode config file: %s", a.config)
246267
a.modules = a.modules.Add(configs...)
247268

248269
if !interactive && len(a.pidFilePath) > 0 {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.21
55
require (
66
go.osspkg.com/algorithms v1.4.1
77
go.osspkg.com/casecheck v0.3.0
8-
go.osspkg.com/config v0.1.2
8+
go.osspkg.com/config v0.1.3
99
go.osspkg.com/console v0.3.3
1010
go.osspkg.com/errors v0.3.1
1111
go.osspkg.com/events v0.3.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ go.osspkg.com/algorithms v1.4.1 h1:yCXiNHu+MGiUYU7wFsCg2Q0drLPnu1q13cIrPAHDlXU=
66
go.osspkg.com/algorithms v1.4.1/go.mod h1:JarWCfCmgxBkk2tA/ABeHScK9WN8azu+bEc27+dF/Gw=
77
go.osspkg.com/casecheck v0.3.0 h1:x15blEszElbrHrEH5H02JIIhGIg/lGZzIt1kQlD3pwM=
88
go.osspkg.com/casecheck v0.3.0/go.mod h1:TRFXDMFJEOtnlp3ET2Hix3osbxwPWhvaiT/HfD3+gBA=
9-
go.osspkg.com/config v0.1.2 h1:CxDEVGoYUx4BfuG6xmKlYp6y2hOvbUBf+5muEu34QQo=
10-
go.osspkg.com/config v0.1.2/go.mod h1:+LdWszZhwqASETll0nUJLlozqR8DEdtmGxWLXHygHRg=
9+
go.osspkg.com/config v0.1.3 h1:LAgTTYFzoIf4VNBIpFtjG81rR0/3SG29r+1D8I/YBzs=
10+
go.osspkg.com/config v0.1.3/go.mod h1:MyqvXDgpHLZlO2SM/rqf1zogpUZiXvkZlc/Z+0L7alA=
1111
go.osspkg.com/console v0.3.3 h1:UB/pPoPsgWbyNFix8pEMQHbsXdMv/UK/dgsbRknCH2A=
1212
go.osspkg.com/console v0.3.3/go.mod h1:IknBCliH6mX/ogHa6wbycnGDFYixCGH3WuNc5W5tQe8=
1313
go.osspkg.com/errors v0.3.1 h1:F9m/EEd/Ot2jba/TV7tvVRIpWXzIpNLc7vRJKcBD86A=

0 commit comments

Comments
 (0)