Repository navigation
Expand file tree
/
Copy pathconfig.go
More file actions
68 lines (58 loc) · 1.62 KB
/
Copy pathconfig.go
File metadata and controls
68 lines (58 loc) · 1.62 KB
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
package main
import (
"path/filepath"
"strings"
"github.com/spf13/viper"
)
const (
ConfigName = "fabric.toml"
)
type Config struct {
Fabric Fabric `toml:"fabric" json:"fabric"`
Services []Service `mapstructure:"services" json:"services"`
}
type Fabric struct {
Name string `toml:"name" json:"name"`
Username string `toml:"username" json:"username"`
CCID string `toml:"ccid" json:"ccid"`
ChannelId string `mapstructure:"channel_id" toml:"channel_id" json:"channel_id"`
Org string `toml:"org" json:"org"`
ServerPort string `toml:"server_port" json:"server_port"`
TimeoutHeight int64 `mapstructure:"timeout_height" json:"timeout_height"`
TimeoutPeriod uint64 `mapstructure:"timeout_period" json:"timeout_period"`
}
type Service struct {
ID string `toml:"id" json:"id"`
Name string `toml:"name" json:"name"`
Type string `toml:"type" json:"type"`
}
func DefaultConfig() *Config {
return &Config{
Fabric: Fabric{
Name: "fabric",
Username: "Admin",
CCID: "broker",
ChannelId: "mychannel",
Org: "org2",
TimeoutHeight: 30,
TimeoutPeriod: 60,
},
Services: nil,
}
}
func UnmarshalConfig(configPath string) (*Config, error) {
viper.SetConfigFile(filepath.Join(configPath, ConfigName))
viper.SetConfigType("toml")
viper.AutomaticEnv()
viper.SetEnvPrefix("FABRIC")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
if err := viper.ReadInConfig(); err != nil {
return nil, err
}
config := DefaultConfig()
if err := viper.Unmarshal(config); err != nil {
return nil, err
}
return config, nil
}