1 Star 0 Fork 0

BUPT-ZKJC / fabric-sdk-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
config.go 3.76 KB
一键复制 编辑 原始数据 按行查看 历史
MJL 提交于 2021-08-06 18:40 . first commit
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package config
import (
"bytes"
"io"
"strings"
"github.com/spf13/viper"
"gitee.com/bupt-zkjc/fabric-sdk-go/pkg/common/logging"
"github.com/pkg/errors"
"gitee.com/bupt-zkjc/fabric-sdk-go/pkg/common/providers/core"
)
var logModules = [...]string{"fabsdk", "fabsdk/client", "fabsdk/core", "fabsdk/fab", "fabsdk/common",
"fabsdk/msp", "fabsdk/util", "fabsdk/context"}
type options struct {
envPrefix string
templatePath string
}
const (
cmdRoot = "FABRIC_SDK"
)
// Option configures the package.
type Option func(opts *options) error
// FromReader loads configuration from in.
// configType can be "json" or "yaml".
func FromReader(in io.Reader, configType string, opts ...Option) core.ConfigProvider {
return func() ([]core.ConfigBackend, error) {
return initFromReader(in, configType, opts...)
}
}
// FromFile reads from named config file
func FromFile(name string, opts ...Option) core.ConfigProvider {
return func() ([]core.ConfigBackend, error) {
backend, err := newBackend(opts...)
if err != nil {
return nil, err
}
if name == "" {
return nil, errors.New("filename is required")
}
// create new viper
backend.configViper.SetConfigFile(name)
// If a config file is found, read it in.
err = backend.configViper.MergeInConfig()
if err != nil {
return nil, errors.Wrapf(err, "loading config file failed: %s", name)
}
setLogLevel(backend)
return []core.ConfigBackend{backend}, nil
}
}
// FromRaw will initialize the configs from a byte array
func FromRaw(configBytes []byte, configType string, opts ...Option) core.ConfigProvider {
return func() ([]core.ConfigBackend, error) {
buf := bytes.NewBuffer(configBytes)
return initFromReader(buf, configType, opts...)
}
}
func initFromReader(in io.Reader, configType string, opts ...Option) ([]core.ConfigBackend, error) {
backend, err := newBackend(opts...)
if err != nil {
return nil, err
}
if configType == "" {
return nil, errors.New("empty config type")
}
// read config from bytes array, but must set ConfigType
// for viper to properly unmarshal the bytes array
backend.configViper.SetConfigType(configType)
err = backend.configViper.MergeConfig(in)
if err != nil {
return nil, err
}
setLogLevel(backend)
return []core.ConfigBackend{backend}, nil
}
// WithEnvPrefix defines the prefix for environment variable overrides.
// See viper SetEnvPrefix for more information.
func WithEnvPrefix(prefix string) Option {
return func(opts *options) error {
opts.envPrefix = prefix
return nil
}
}
func newBackend(opts ...Option) (*defConfigBackend, error) {
o := options{
envPrefix: cmdRoot,
}
for _, option := range opts {
err := option(&o)
if err != nil {
return nil, errors.WithMessage(err, "Error in options passed to create new config backend")
}
}
v := newViper(o.envPrefix)
//default backend for config
backend := &defConfigBackend{
configViper: v,
opts: o,
}
err := backend.loadTemplateConfig()
if err != nil {
return nil, err
}
return backend, nil
}
func newViper(cmdRootPrefix string) *viper.Viper {
myViper := viper.New()
myViper.SetEnvPrefix(cmdRootPrefix)
myViper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
myViper.SetEnvKeyReplacer(replacer)
return myViper
}
// setLogLevel will set the log level of the client
func setLogLevel(backend core.ConfigBackend) {
loggingLevelString, _ := backend.Lookup("client.logging.level")
logLevel := logging.INFO
if loggingLevelString != nil {
var err error
logLevel, err = logging.LogLevel(loggingLevelString.(string))
if err != nil {
panic(err)
}
}
// TODO: allow separate settings for each
for _, logModule := range logModules {
logging.SetLevel(logModule, logLevel)
}
}
1
https://gitee.com/bupt-zkjc/fabric-sdk-go.git
git@gitee.com:bupt-zkjc/fabric-sdk-go.git
bupt-zkjc
fabric-sdk-go
fabric-sdk-go
84f269695ead

搜索帮助

53164aa7 5694891 3bd8fe86 5694891