2021-01-07 01:17:03 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-04-03 09:59:15 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gcfg
|
|
|
|
|
|
|
|
import (
|
2020-03-15 19:56:07 +08:00
|
|
|
"fmt"
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/container/gmap"
|
2019-04-03 09:59:15 +08:00
|
|
|
)
|
|
|
|
|
2019-04-03 23:39:31 +08:00
|
|
|
const (
|
|
|
|
// Default group name for instance usage.
|
2021-01-07 01:17:03 +08:00
|
|
|
DefaultName = "config"
|
2019-04-03 23:39:31 +08:00
|
|
|
)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
2019-04-03 09:59:15 +08:00
|
|
|
var (
|
2020-03-15 19:56:07 +08:00
|
|
|
// Instances map containing configuration instances.
|
2019-07-23 23:20:27 +08:00
|
|
|
instances = gmap.NewStrAnyMap(true)
|
2019-04-03 09:59:15 +08:00
|
|
|
)
|
|
|
|
|
2019-04-03 23:39:31 +08:00
|
|
|
// Instance returns an instance of Config with default settings.
|
2020-03-15 19:56:07 +08:00
|
|
|
// The parameter <name> is the name for the instance. But very note that, if the file "name.toml"
|
|
|
|
// exists in the configuration directory, it then sets it as the default configuration file. The
|
|
|
|
// toml file type is the default configuration file type.
|
2019-06-19 09:06:52 +08:00
|
|
|
func Instance(name ...string) *Config {
|
2020-12-14 13:02:08 +08:00
|
|
|
key := DefaultName
|
2019-07-28 17:37:13 +08:00
|
|
|
if len(name) > 0 && name[0] != "" {
|
2019-04-03 23:39:31 +08:00
|
|
|
key = name[0]
|
2019-04-03 09:59:15 +08:00
|
|
|
}
|
2019-04-03 23:39:31 +08:00
|
|
|
return instances.GetOrSetFuncLock(key, func() interface{} {
|
2020-03-15 19:56:07 +08:00
|
|
|
c := New()
|
2020-12-14 13:02:08 +08:00
|
|
|
for _, fileType := range supportedFileTypes {
|
|
|
|
if file := fmt.Sprintf(`%s.%s`, key, fileType); c.Available(file) {
|
|
|
|
c.SetFileName(file)
|
|
|
|
break
|
|
|
|
}
|
2020-03-15 19:56:07 +08:00
|
|
|
}
|
|
|
|
return c
|
2019-04-03 09:59:15 +08:00
|
|
|
}).(*Config)
|
|
|
|
}
|