github.com/ewagmig/fabric@v2.1.1+incompatible/cmd/common/config.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package common 8 9 import ( 10 "io/ioutil" 11 12 "github.com/hyperledger/fabric/cmd/common/comm" 13 "github.com/hyperledger/fabric/cmd/common/signer" 14 "github.com/pkg/errors" 15 yaml "gopkg.in/yaml.v2" 16 ) 17 18 // Config aggregates configuration of TLS and signing 19 type Config struct { 20 Version int 21 TLSConfig comm.Config 22 SignerConfig signer.Config 23 } 24 25 // ConfigFromFile loads the given file and converts it to a Config 26 func ConfigFromFile(file string) (Config, error) { 27 configData, err := ioutil.ReadFile(file) 28 if err != nil { 29 return Config{}, errors.WithStack(err) 30 } 31 config := Config{} 32 33 if err := yaml.Unmarshal([]byte(configData), &config); err != nil { 34 return Config{}, errors.Errorf("error unmarshaling YAML file %s: %s", file, err) 35 } 36 37 return config, validateConfig(config) 38 } 39 40 // ToFile writes the config into a file 41 func (c Config) ToFile(file string) error { 42 if err := validateConfig(c); err != nil { 43 return errors.Wrap(err, "config isn't valid") 44 } 45 b, _ := yaml.Marshal(c) 46 if err := ioutil.WriteFile(file, b, 0600); err != nil { 47 return errors.Errorf("failed writing file %s: %v", file, err) 48 } 49 return nil 50 } 51 52 func validateConfig(conf Config) error { 53 nonEmptyStrings := []string{ 54 conf.SignerConfig.MSPID, 55 conf.SignerConfig.IdentityPath, 56 conf.SignerConfig.KeyPath, 57 } 58 59 for _, s := range nonEmptyStrings { 60 if s == "" { 61 return errors.New("empty string that is mandatory") 62 } 63 } 64 return nil 65 }