-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
119 lines (101 loc) · 2.29 KB
/
main.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
114
115
116
117
118
119
package main
import "os"
import "fmt"
import "github.com/bnclabs/golog"
import "github.com/tn47/goledger/dblentry"
import "github.com/tn47/goledger/reports"
import "github.com/tn47/goledger/api"
var _ = fmt.Sprintf("dummy")
func main() {
args := phase1()
reporter, db := phase2(args)
nreporter, ndb := phase3(args, reporter, db)
nreporter.Render(args, ndb)
}
func trycommand(args []string, phase string) bool {
if len(args) == 0 {
return false
}
switch phase {
case "phase1":
switch args[0] {
case "version", "ver":
log.Consolef("goledger version - goledger%v\n", api.LedgerVersion)
return true
}
case "phase2":
case "phase3":
}
return false
}
// 1. arguments are parsed.
// 2. log is initialized.
func phase1() (args []string) {
defer func() {
if trycommand(args, "phase1") {
os.Exit(0)
}
}()
var err error
if args, err = argparse(); err != nil {
os.Exit(1)
}
return args
}
// 1. create one or more reporter
// 2. create a datastore.
// 3. firstpass on all journal files on the datastore.
// 4. firstpass completed
func phase2(args []string) (api.Reporter, api.Datastorer) {
defer func() {
if trycommand(args, "phase2") {
os.Exit(0)
}
}()
reporter, err := reports.NewReporter(args)
if err != nil {
os.Exit(1)
}
db := dblentry.NewDatastore(api.Options.Dbname, reporter)
// apply command line arguments here.
if api.Options.Enddt != nil {
db.Applytill(*api.Options.Enddt)
}
for _, journal := range api.Options.Journals {
log.Debugf("processing journal %q\n", journal)
reporter.Startjournal(journal, false /*included*/)
if err := dofirstpass(reporter, db, journal); err != nil {
os.Exit(1)
}
}
db.Firstpassok()
db.PrintAccounts() // for debug
return reporter, db
}
// 1. clone reporter and datastore for secondpass.
// 2. do secondpass on datastore.
// 3. secondpass completed.
func phase3(
args []string,
reporter api.Reporter, db api.Datastorer) (api.Reporter, api.Datastorer) {
defer func() {
if trycommand(args, "phase3") {
os.Exit(0)
}
}()
if len(args) == 0 {
return reporter, nil
}
switch args[0] {
case "list", "ls":
return reporter, db
}
nreporter := reporter.Clone()
//nreporter.secondpass()
ndb := db.Clone(nreporter)
if err := secondpass(ndb); err != nil {
os.Exit(2)
}
ndb.Secondpassok()
return nreporter, ndb
}