-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
82 lines (66 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
package internal
import (
"encoding/json"
"os"
"strconv"
_ "github.com/joho/godotenv/autoload" // auto import variables from .env
log "github.com/sirupsen/logrus"
)
// Cfg is a global Config instance
var Config Configuration
// Configuration represents a configuration object
type Configuration struct {
SimulationDays uint32
InitialNetworkHashPower uint64 // Hashes per second
LimitNetworkHashPowerPctChange int // How much the network power can vary from the initial power
}
// InitConfig instantiates Cfg with defaults or environment variables
func InitConfig() {
Config.SimulationDays = getEnvAsUint32("SIMULATION_DAYS", 365)
Config.InitialNetworkHashPower = getEnvAsUint64("INITIAL_NETWORK_HASH_POWER", 1000000)
Config.LimitNetworkHashPowerPctChange = getEnvAsInt("LIMIT_NETWORK_HASH_POWER_PCT_CHANGE", 10)
}
// PrintConfig prints the current configuration in an easy to read format
func PrintConfig() {
s, _ := json.MarshalIndent(Config, "", "\t")
log.Info("Configuration: \n", string(s))
}
// getEnv is a simple helper function to read an environment or return a default value
func getEnv(key string, defaultVal string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultVal
}
// getEnvAsInt is a simple helper function to read an environment variable into integer or return a default value
func getEnvAsInt(name string, defaultVal int) int {
valueStr := getEnv(name, "")
if value, err := strconv.Atoi(valueStr); err == nil {
return value
}
return defaultVal
}
// getEnvAsUint64 is a simple helper function to read an environment variable into integer or return a default value
func getEnvAsUint64(name string, defaultVal uint64) uint64 {
valueStr := getEnv(name, "")
if value, err := strconv.ParseUint(valueStr, 10, 64); err == nil {
return value
}
return defaultVal
}
// getEnvAsUint32 is a simple helper function to read an environment variable into integer or return a default value
func getEnvAsUint32(name string, defaultVal uint32) uint32 {
valueStr := getEnv(name, "")
if value, err := strconv.ParseUint(valueStr, 10, 32); err == nil {
return uint32(value)
}
return defaultVal
}
// getEnvAsBool is a simple helper function to read an environment variable into a bool or return default value
func getEnvAsBool(name string, defaultVal bool) bool {
valStr := getEnv(name, "")
if val, err := strconv.ParseBool(valStr); err == nil {
return val
}
return defaultVal
}