-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig.go
95 lines (87 loc) · 2.44 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
//common use fot ontology-test
package common
import (
"encoding/json"
"fmt"
log4 "github.com/alecthomas/log4go"
"io/ioutil"
"os"
)
//Default config instance
var DefConfig = NewTestConfig()
//Config object used by ontology-instance
type TestConfig struct {
//JsonRpcAddress of ontology
JsonRpcAddress string
//RestfulAddress of ontology
RestfulAddress string
//WebSocketAddress of ontology
WebSocketAddress string
//WalletFile of test
WalletFile string
//The Password of wallet
Password string
//Gas Price of transaction
GasPrice uint64
//Gas Limit of invoke transaction
GasLimit uint64
//Gas Limit of deploy transaction
GasDeployLimit uint64
}
//NewTestConfig retuen a TestConfig instance
func NewTestConfig() *TestConfig {
return &TestConfig{}
}
//Init TestConfig with a config file
func (this *TestConfig) Init(fileName string) error {
err := this.loadConfig(fileName)
if err != nil {
return fmt.Errorf("loadConfig error:%s", err)
}
return nil
}
func (this *TestConfig) loadConfig(fileName string) error {
data, err := this.readFile(fileName)
if err != nil {
return err
}
err = json.Unmarshal(data, this)
if err != nil {
return fmt.Errorf("json.Unmarshal TestConfig:%s error:%s", data, err)
}
return nil
}
func (this *TestConfig) readFile(fileName string) ([]byte, error) {
file, err := os.OpenFile(fileName, os.O_RDONLY, 0666)
if err != nil {
return nil, fmt.Errorf("OpenFile %s error %s", fileName, err)
}
defer func() {
err := file.Close()
if err != nil {
log4.Error("File %s close error %s", fileName, err)
}
}()
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("ioutil.ReadAll %s error %s", fileName, err)
}
return data, nil
}