github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/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/core/config" 28 "github.com/hyperledger/fabric/peer/common" 29 ) 30 31 //This is where all initializations take place. These closely follow CLI 32 //initializations. 33 34 //read CC checker configuration from -s <jsonfile>. Defaults to ccchecker.json 35 func initCCCheckerParams(mainFlags *pflag.FlagSet) { 36 configFile := "" 37 mainFlags.StringVarP(&configFile, "config", "s", "ccchecker.json", "CC Checker config file ") 38 39 err := LoadCCCheckerParams(configFile) 40 if err != nil { 41 fmt.Printf("error unmarshalling ccchecker: %s\n", err) 42 os.Exit(1) 43 } 44 } 45 46 //read yaml file from -y <dir_to_core.yaml>. Defaults to ../../peer 47 func initYaml(mainFlags *pflag.FlagSet) { 48 defaultConfigPath, err := config.GetDevConfigDir() 49 if err != nil { 50 fmt.Printf("Fatal error when getting DevConfigDir: %s\n", err) 51 os.Exit(2) 52 } 53 54 // For environment variables. 55 viper.SetConfigName(cmdRoot) 56 viper.SetEnvPrefix(cmdRoot) 57 viper.AutomaticEnv() 58 replacer := strings.NewReplacer(".", "_") 59 viper.SetEnvKeyReplacer(replacer) 60 61 pathToYaml := "" 62 mainFlags.StringVarP(&pathToYaml, "yamlfile", "y", defaultConfigPath, "Path to core.yaml defined for peer") 63 64 viper.AddConfigPath(pathToYaml) 65 66 err = viper.ReadInConfig() // Find and read the config file 67 if err != nil { // Handle errors reading the config file 68 fmt.Printf("Fatal error when reading %s config file: %s\n", cmdRoot, err) 69 os.Exit(2) 70 } 71 } 72 73 //initialize MSP from -m <mspconfigdir>. Defaults to ../../sampleconfig/msp 74 func initMSP(mainFlags *pflag.FlagSet) { 75 defaultMspDir, err := config.GetDevMspDir() 76 if err != nil { 77 panic(err.Error()) 78 } 79 80 mspMgrConfigDir := "" 81 mspID := "" 82 mainFlags.StringVarP(&mspMgrConfigDir, "mspcfgdir", "m", defaultMspDir, "Path to MSP dir") 83 mainFlags.StringVarP(&mspID, "mspid", "i", "DEFAULT", "MSP ID") 84 85 err = common.InitCrypto(mspMgrConfigDir, mspID) 86 if err != nil { 87 panic(err.Error()) 88 } 89 } 90 91 //InitCCCheckerEnv initialize the CCChecker environment 92 func InitCCCheckerEnv(mainFlags *pflag.FlagSet) { 93 initCCCheckerParams(mainFlags) 94 initYaml(mainFlags) 95 initMSP(mainFlags) 96 }