-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
113 lines (95 loc) · 3.46 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/pborman/getopt/v2"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var (
me = filepath.Base(os.Args[0])
showVersion = getopt.BoolLong("version", 0, "Show version ("+Version+")")
yamlFile = fmt.Sprintf("%s.yaml", me)
envPrefix = "SCID_UTIL"
configSearchPaths = []string{".", "./etc", "$HOME/.sc-data-util/", "$HOME/etc", "/etc"}
stdIn = getopt.BoolLong("stdin", 'i', "Read data from STDIN, Dump to STDOUT. Disables most other options.")
genConfig = getopt.BoolLong("genconfig", 'x', "Write example config to \"./"+yamlFile+"\"")
dailyDetail = getopt.BoolLong("dailyDetail", 0x00, "Print daily data with added row detail")
detailProfile = getopt.BoolLong("detailProfile", 0x00, "Print daily data with added row detail and bar profile")
slim = getopt.BoolLong("slim", 0x00, "Slim/Minimal CSV data")
symbol = getopt.StringLong("symbol", 's', "", "Symbol to operate on (required, unless `-i`)")
barSize = getopt.StringLong("barSize", 'b', "", "Export as bars of size: [10s, 2m, 4h, 3200t, 5000v]")
tickSize = getopt.StringLong("tickSize", 0x00, "0.001", "Round to tick size")
bundle = getopt.BoolLong("bundle", 'm', "", "Bundle trades")
//csvBars = getopt.BoolLong("csv", 0, "Bars in CSV")
//jsonBars = getopt.BoolLong("json", 0, "Bars as JSON")
startUnixTime = getopt.Int64Long("startUnixTime", 0, 0, "Export Starting at unix time")
endUnixTime = getopt.Int64Long("endUnixTime", 0, time.Now().Unix(), "End export at unix time")
)
func init() {
viper.SetConfigName(yamlFile)
viper.SetConfigType("yaml")
viper.SetEnvPrefix(envPrefix)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
for _, p := range configSearchPaths {
viper.AddConfigPath(p)
}
viper.SetDefault("data.dir", "data")
viper.SetDefault("log.level", "TRACE")
viper.SetDefault("log.file", "")
getopt.SetUsage(func() { usage() })
getopt.Parse()
if *genConfig {
configWrite()
os.Exit(0)
return
}
if *showVersion {
ShowVersion()
os.Exit(0)
return
}
if *tickSize != "" {
_, err := strconv.ParseFloat(strings.TrimSpace(*tickSize), 64)
if err != nil {
usage(fmt.Sprintf("\nFailed to parse tickSize: %v\n", err))
os.Exit(1)
}
}
if *stdIn && (*symbol != "" || *startUnixTime != 0 || *endUnixTime != 0) {
usage(fmt.Sprintf("\nMost options do not work with --stdin input!!\n\n"+
"Try: %s --genconfig for data directory configuration options.\n", me))
os.Exit(1)
}
if *symbol == "" && (!*stdIn || !*dailyDetail) {
usage(fmt.Sprintf("\nNo input or symbol provided\n\nTry: %s --genconfig\n", me))
os.Exit(1)
}
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Printf("%v\n", err)
//usage( fmt.Sprintf("\nTry: %s --genconfig\n", me) )
//os.Exit(1)
} else {
log.Fatalf("Failed to parse config: %v\n", err)
os.Exit(1)
}
}
initLogger(viper.GetString("log.level"), viper.GetString("log.file"))
}
func configWrite() {
viper.SafeWriteConfigAs(fmt.Sprintf("./%s", yamlFile))
log.Printf("Wrote example config to: \"./%s\", feel free to move to: %v", yamlFile, configSearchPaths[1:])
log.Println(`
Alternatively, set the following environment variables:
export ` + envPrefix + `_DATA_DIR='data'
export ` + envPrefix + `_LOG_LEVEL='TRACE'
export ` + envPrefix + `_LOG_FILE='sc-data-util.log'
`)
os.Exit(0)
}