github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/config/config.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"runtime"
    23  	"strings"
    24  
    25  	"github.com/op/go-logging"
    26  	"github.com/spf13/viper"
    27  
    28  	"github.com/hyperledger/fabric/bccsp/factory"
    29  	"github.com/hyperledger/fabric/msp"
    30  )
    31  
    32  // Config the config wrapper structure
    33  type Config struct {
    34  }
    35  
    36  var configLogger = logging.MustGetLogger("config")
    37  
    38  func init() {
    39  
    40  }
    41  
    42  // SetupTestLogging setup the logging during test execution
    43  func SetupTestLogging() {
    44  	level, err := logging.LogLevel(viper.GetString("logging.peer"))
    45  	if err == nil {
    46  		// No error, use the setting
    47  		logging.SetLevel(level, "main")
    48  		logging.SetLevel(level, "server")
    49  		logging.SetLevel(level, "peer")
    50  	} else {
    51  		configLogger.Warningf("Log level not recognized '%s', defaulting to %s: %s", viper.GetString("logging.peer"), logging.ERROR, err)
    52  		logging.SetLevel(logging.ERROR, "main")
    53  		logging.SetLevel(logging.ERROR, "server")
    54  		logging.SetLevel(logging.ERROR, "peer")
    55  	}
    56  }
    57  
    58  // SetupTestConfig setup the config during test execution
    59  func SetupTestConfig(pathToOpenchainYaml string) {
    60  	flag.Parse()
    61  
    62  	// Now set the configuration file
    63  	viper.SetEnvPrefix("HYPERLEDGER")
    64  	viper.AutomaticEnv()
    65  	replacer := strings.NewReplacer(".", "_")
    66  	viper.SetEnvKeyReplacer(replacer)
    67  	viper.SetConfigName("core")              // name of config file (without extension)
    68  	viper.AddConfigPath(pathToOpenchainYaml) // path to look for the config file in
    69  	err := viper.ReadInConfig()              // Find and read the config file
    70  	if err != nil {                          // Handle errors reading the config file
    71  		panic(fmt.Errorf("Fatal error config file: %s \n", err))
    72  	}
    73  
    74  	SetupTestLogging()
    75  
    76  	// Set the number of maxprocs
    77  	var numProcsDesired = viper.GetInt("peer.gomaxprocs")
    78  	configLogger.Debugf("setting Number of procs to %d, was %d\n", numProcsDesired, runtime.GOMAXPROCS(2))
    79  
    80  	// Init the BCCSP
    81  	var bccspConfig *factory.FactoryOpts
    82  	err = viper.UnmarshalKey("peer.BCCSP", &bccspConfig)
    83  	if err != nil {
    84  		bccspConfig = nil
    85  	}
    86  
    87  	msp.SetupBCCSPKeystoreConfig(bccspConfig, viper.GetString("peer.mspConfigPath")+"/keystore")
    88  
    89  	err = factory.InitFactories(bccspConfig)
    90  	if err != nil {
    91  		panic(fmt.Errorf("Could not initialize BCCSP Factories [%s]", err))
    92  	}
    93  }