-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstate.go
65 lines (57 loc) · 1.5 KB
/
state.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
)
func stateFile() string {
homeDir, _ := os.UserHomeDir()
return filepath.Join(homeDir, fmt.Sprintf("algo-csv-state.json"))
}
// ExportState is the root type we use to persist state for the formats/accounts
// we exported.
// The state is tracking by format, then by account, and storing a 'state' instance.
type ExportState map[string]AccountExportState
type AccountExportState map[string]*state
type state struct {
LastRound uint64
}
// LoadConfig hande
func LoadConfig() ExportState {
var retState = ExportState{}
configFile := stateFile()
if !fileExist(configFile) {
return retState
}
configBytes, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatalln("reading config file:", configFile, "error:", err)
}
err = json.Unmarshal(configBytes, &retState)
if err != nil {
log.Fatalln("parsing config file:", configFile, "error:", err)
}
return retState
}
func (s ExportState) ForAccount(format string, account string) *state {
if exportState, found := s[format]; found {
if retState, found := exportState[account]; found {
return retState
}
s[format][account] = &state{}
return s[format][account]
}
s[format] = AccountExportState{account: &state{}}
return s[format][account]
}
func (s ExportState) SaveConfig() {
data, err := json.MarshalIndent(s, "", " ")
if err != nil {
log.Println("error mashalling address data:", err)
os.Exit(1)
}
_ = ioutil.WriteFile(stateFile(), data, 0644)
}