-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
110 lines (96 loc) · 2.52 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
package fin
import (
"log"
"os"
"gopkg.in/yaml.v3"
)
const defaultConfig = "config.yaml"
type Config struct {
Input string `yaml:"input"`
Output string `yaml:"output"`
Format string `yaml:"format"`
SkipPies bool `yaml:"skip-pies"`
PieOnly string `yaml:"pie-only"`
Splits []Splits `yaml:"splits"`
Symbols map[string]string `yaml:"symbols"`
Renames map[string]string `yaml:"renames"`
Pies []struct {
Name string `yaml:"name"`
Symbols []string `yaml:"symbols"`
} `yaml:"pies"`
}
type Splits struct {
Symbol string `yaml:"symbol"`
Date string `yaml:"date"`
Ratio float64 `yaml:"ratio"`
}
// GetOrCreateConfig checks for a config file and otherwise creates one.
func GetOrCreateConfig() Config {
cfg, err := GetConfig(defaultConfig)
switch {
case os.IsNotExist(err):
createConfig()
cfg, _ = GetConfig(defaultConfig)
case err != nil:
log.Fatalf("Failed to read config file: %v", err)
}
return cfg
}
// GetConfig reads a config file from disk.
func GetConfig(file string) (Config, error) {
b, err := os.ReadFile(file)
if err != nil {
return Config{}, err
}
var cfg Config
if err := yaml.Unmarshal(b, &cfg); err != nil {
return Config{}, err
}
return cfg, nil
}
// createConfig creates a default config file.
func createConfig() {
data := `---
# Required config
input: data # folder where your Trading212 CSVs are stored
output: aggregated_quotes # name of output file (prefix)
format: aggregate # aggregate or yahoo
# Optional config
skip-pies: true # skip splitting by pies (default: false)
pie-only: "" # only generate this pie (default: "")
# Splits is a list of split events relevant to your portfolio
# this is needed to calculate the total stock count
splits:
- symbol: ABEC
date: 2022-07-16
ratio: 20 # for reverse splits, use a decimal ratio
# Symbols is a list of conversions to take Trading212 symbols
# and convert them to the symbols used by Yahoo portfolios
symbols:
RIO: RIO.L
SAN: SAN.PA
# Pies allows you split your aggregation into multiple CSVs
# uncomment to use
#pies:
# - name: Growth
# symbols:
# - GOOG
# - AMZN
# - name: Dividend
# symbols:
# - PEP
# - JNJ
`
err := os.WriteFile(defaultConfig, []byte(data), 0644)
if err != nil {
log.Fatalf("Failed to create config file: %v", err)
}
}
// PieNames returns the available pie names.
func (c Config) PieNames() []string {
var names []string
for _, p := range c.Pies {
names = append(names, p.Name)
}
return names
}