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