This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
helpers.go
113 lines (103 loc) · 2.91 KB
/
helpers.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package commands
import (
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/hyperledger/burrow/config"
"github.com/hyperledger/burrow/config/source"
"github.com/hyperledger/burrow/genesis"
logging_config "github.com/hyperledger/burrow/logging/logconfig"
)
type Output interface {
Printf(format string, args ...interface{})
Logf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
}
func obtainBurrowConfig(configFile, genesisDocFile string) (*config.BurrowConfig, error) {
// We need to reflect on whether this obscures where values are coming from
conf := config.DefaultBurrowConfig()
// We treat logging a little differently in that if anything is set for logging we will not
// set default outputs
conf.Logging = nil
err := source.EachOf(
burrowConfigProvider(configFile),
source.FirstOf(
genesisDocProvider(genesisDocFile, false),
// Try working directory
genesisDocProvider(config.DefaultGenesisDocJSONFileName, true)),
).Apply(conf)
if err != nil {
return nil, err
}
// If no logging config was provided use the default
if conf.Logging == nil {
conf.Logging = logging_config.DefaultNodeLoggingConfig()
}
return conf, nil
}
func burrowConfigProvider(configFile string) source.ConfigProvider {
return source.FirstOf(
// Will fail if file doesn't exist, but still skipped it configFile == ""
source.File(configFile, false),
source.Environment(config.DefaultBurrowConfigEnvironmentVariable),
// Try working directory
source.File(config.DefaultBurrowConfigTOMLFileName, true),
source.Default(config.DefaultBurrowConfig()))
}
func genesisDocProvider(genesisFile string, skipNonExistent bool) source.ConfigProvider {
return source.NewConfigProvider(fmt.Sprintf("genesis file at %s", genesisFile),
source.ShouldSkipFile(genesisFile, skipNonExistent),
func(baseConfig interface{}) error {
conf, ok := baseConfig.(*config.BurrowConfig)
if !ok {
return fmt.Errorf("config passed was not BurrowConfig")
}
if conf.GenesisDoc != nil {
return nil
}
genesisDoc := new(genesis.GenesisDoc)
err := source.FromFile(genesisFile, genesisDoc)
if err != nil {
return err
}
conf.GenesisDoc = genesisDoc
return nil
})
}
func parseRange(rangeString string) (start int64, end int64, err error) {
start = 0
end = -1
if rangeString == "" {
return
}
bounds := strings.Split(rangeString, ":")
if len(bounds) == 1 {
startString := bounds[0]
start, err = strconv.ParseInt(startString, 10, 64)
return
}
if len(bounds) == 2 {
if bounds[0] != "" {
start, err = strconv.ParseInt(bounds[0], 10, 64)
if err != nil {
return
}
}
if bounds[1] != "" {
end, err = strconv.ParseInt(bounds[1], 10, 64)
}
return
}
return 0, 0, fmt.Errorf("could not parse range from %s", rangeString)
}
func handleTerm() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
os.Exit(1)
}()
}