/
config.go
59 lines (50 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
package fabclient
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
// Config holds the client configuration.
type Config struct {
Chaincodes []Chaincode `json:"chaincodes" yaml:"chaincodes"`
Channels []Channel `json:"channels" yaml:"channels"`
ConnectionProfile string `json:"connectionProfile" yaml:"connectionProfile"`
Identities struct {
Admin Identity `json:"admin" yaml:"admin"`
Users []Identity `json:"users" yaml:"users"`
} `json:"identities" yaml:"identities"`
Organization string `json:"organization" yaml:"organization"`
}
// NewConfigFromFile returns a new client config.
func NewConfigFromFile(configPath string) (*Config, error) {
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
fileAsBytes, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
var (
config = &Config{}
extension = filepath.Ext(configPath)
)
switch extension {
case ".json":
if err := json.Unmarshal(fileAsBytes, config); err != nil {
return nil, fmt.Errorf("failed to create client configuration from file %s: %w", configPath, err)
}
case ".yml", ".yaml":
if err := yaml.Unmarshal(fileAsBytes, config); err != nil {
return nil, fmt.Errorf("failed to create client configuration from file %s: %w", configPath, err)
}
default:
return nil, errors.New("invalid client configuration file extension, supported: .json|.yml|.yaml")
}
return config, nil
}