github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/examples/ccchecker/init.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 main 18 19 import ( 20 "fmt" 21 "os" 22 "strings" 23 24 "github.com/spf13/pflag" 25 "github.com/spf13/viper" 26 27 "github.com/hyperledger/fabric/peer/common" 28 ) 29 30 //This is where all initializations take place. These closley follow CLI 31 //initializations. 32 33 //read CC checker configuration from -s <jsonfile>. Defaults to ccchecker.json 34 func initCCCheckerParams(mainFlags *pflag.FlagSet) { 35 configFile := "" 36 mainFlags.StringVarP(&configFile, "config", "s", "ccchecker.json", "CC Checker config file ") 37 38 err := LoadCCCheckerParams(configFile) 39 if err != nil { 40 fmt.Printf("error unmarshalling ccchecker: %s\n", err) 41 os.Exit(1) 42 } 43 } 44 45 //read yaml file from -y <dir_to_core.yaml>. Defaults to ../../peer 46 func initYaml(mainFlags *pflag.FlagSet) { 47 // For environment variables. 48 viper.SetEnvPrefix(cmdRoot) 49 viper.AutomaticEnv() 50 replacer := strings.NewReplacer(".", "_") 51 viper.SetEnvKeyReplacer(replacer) 52 53 pathToYaml := "" 54 mainFlags.StringVarP(&pathToYaml, "yamlfile", "y", "../../peer", "Path to core.yaml defined for peer") 55 56 err := common.InitConfig(cmdRoot) 57 if err != nil { // Handle errors reading the config file 58 fmt.Printf("Fatal error when reading %s config file: %s\n", cmdRoot, err) 59 os.Exit(2) 60 } 61 } 62 63 //initialize MSP from -m <mspconfigdir>. Defaults to ../../msp/sampleconfig 64 func initMSP(mainFlags *pflag.FlagSet) { 65 mspMgrConfigDir := "" 66 mspID := "" 67 mainFlags.StringVarP(&mspMgrConfigDir, "mspcfgdir", "m", "../../msp/sampleconfig/", "Path to MSP dir") 68 mainFlags.StringVarP(&mspID, "mspid", "i", "DEFAULT", "MSP ID") 69 70 err := common.InitCrypto(mspMgrConfigDir, mspID) 71 if err != nil { 72 panic(err.Error()) 73 } 74 } 75 76 //InitCCCheckerEnv initialize the CCChecker environment 77 func InitCCCheckerEnv(mainFlags *pflag.FlagSet) { 78 initCCCheckerParams(mainFlags) 79 initYaml(mainFlags) 80 initMSP(mainFlags) 81 }