Files
gf/os/gcfg/gcfg_instance.go

42 lines
1.1 KiB
Go
Raw Normal View History

2019-04-03 09:59:15 +08:00
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// 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 (
"fmt"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/container/gmap"
2019-04-03 09:59:15 +08:00
)
const (
// Default group name for instance usage.
2019-08-19 21:02:44 +08:00
DEFAULT_NAME = "default"
)
2019-06-19 09:06:52 +08:00
2019-04-03 09:59:15 +08:00
var (
// Instances map containing configuration instances.
instances = gmap.NewStrAnyMap(true)
2019-04-03 09:59:15 +08:00
)
// Instance returns an instance of Config with default settings.
// 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 {
2019-08-19 21:02:44 +08:00
key := DEFAULT_NAME
2019-07-28 17:37:13 +08:00
if len(name) > 0 && name[0] != "" {
key = name[0]
2019-04-03 09:59:15 +08:00
}
return instances.GetOrSetFuncLock(key, func() interface{} {
c := New()
file := fmt.Sprintf(`%s.toml`, key)
if c.Available(file) {
c.SetFileName(file)
}
return c
2019-04-03 09:59:15 +08:00
}).(*Config)
}