github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/core/config/config.go (about) 1 /* 2 Copyright Greg Haskins <gregory.haskins@gmail.com> 2017, All Rights Reserved. 3 Copyright IBM Corp. All Rights Reserved. 4 5 SPDX-License-Identifier: Apache-2.0 6 */ 7 8 package config 9 10 import ( 11 "fmt" 12 "os" 13 "path/filepath" 14 15 "github.com/spf13/viper" 16 ) 17 18 func dirExists(path string) bool { 19 fi, err := os.Stat(path) 20 if err != nil { 21 return false 22 } 23 return fi.IsDir() 24 } 25 26 func AddConfigPath(v *viper.Viper, p string) { 27 if v != nil { 28 v.AddConfigPath(p) 29 } else { 30 viper.AddConfigPath(p) 31 } 32 } 33 34 //---------------------------------------------------------------------------------- 35 // TranslatePath() 36 //---------------------------------------------------------------------------------- 37 // Translates a relative path into a fully qualified path relative to the config 38 // file that specified it. Absolute paths are passed unscathed. 39 //---------------------------------------------------------------------------------- 40 func TranslatePath(base, p string) string { 41 if filepath.IsAbs(p) { 42 return p 43 } 44 45 return filepath.Join(base, p) 46 } 47 48 //---------------------------------------------------------------------------------- 49 // TranslatePathInPlace() 50 //---------------------------------------------------------------------------------- 51 // Translates a relative path into a fully qualified path in-place (updating the 52 // pointer) relative to the config file that specified it. Absolute paths are 53 // passed unscathed. 54 //---------------------------------------------------------------------------------- 55 func TranslatePathInPlace(base string, p *string) { 56 *p = TranslatePath(base, *p) 57 } 58 59 //---------------------------------------------------------------------------------- 60 // GetPath() 61 //---------------------------------------------------------------------------------- 62 // GetPath allows configuration strings that specify a (config-file) relative path 63 // 64 // For example: Assume our config is located in /etc/hyperledger/fabric/core.yaml with 65 // a key "msp.configPath" = "msp/config.yaml". 66 // 67 // This function will return: 68 // GetPath("msp.configPath") -> /etc/hyperledger/fabric/msp/config.yaml 69 // 70 //---------------------------------------------------------------------------------- 71 func GetPath(key string) string { 72 p := viper.GetString(key) 73 if p == "" { 74 return "" 75 } 76 77 return TranslatePath(filepath.Dir(viper.ConfigFileUsed()), p) 78 } 79 80 const OfficialPath = "/etc/hyperledger/fabric" 81 82 //---------------------------------------------------------------------------------- 83 // InitViper() 84 //---------------------------------------------------------------------------------- 85 // Performs basic initialization of our viper-based configuration layer. 86 // Primary thrust is to establish the paths that should be consulted to find 87 // the configuration we need. If v == nil, we will initialize the global 88 // Viper instance 89 //---------------------------------------------------------------------------------- 90 func InitViper(v *viper.Viper, configName string) error { 91 var altPath = os.Getenv("FABRIC_CFG_PATH") 92 if altPath != "" { 93 // If the user has overridden the path with an envvar, its the only path 94 // we will consider 95 96 if !dirExists(altPath) { 97 return fmt.Errorf("FABRIC_CFG_PATH %s does not exist", altPath) 98 } 99 100 AddConfigPath(v, altPath) 101 } else { 102 // If we get here, we should use the default paths in priority order: 103 // 104 // *) CWD 105 // *) /etc/hyperledger/fabric 106 107 // CWD 108 AddConfigPath(v, "./") 109 110 // And finally, the official path 111 if dirExists(OfficialPath) { 112 AddConfigPath(v, OfficialPath) 113 } 114 } 115 116 // Now set the configuration file. 117 if v != nil { 118 v.SetConfigName(configName) 119 } else { 120 viper.SetConfigName(configName) 121 } 122 123 return nil 124 }