/
main.go
executable file
·130 lines (104 loc) · 2.46 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
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"github.com/codegangsta/cli"
"github.com/gi4nks/quant"
"os"
)
var parrot = quant.NewParrot("decanter")
var settings = Settings{}
var repository = Repository{}
func initDB() {
repository.InitDB(settings.RepositoryUrl())
}
func readSettings() {
settings.LoadSettings()
if settings.DebugMode() {
parrot = quant.NewVerboseParrot("decanter")
}
parrot.Debug("Parrot is set to talk so much!")
}
func main() {
readSettings()
initDB()
// -------------------
app := cli.NewApp()
app.Name = "decanter"
app.Usage = "Voxxed Zurich Votes translator"
app.Version = "0.0.1"
app.Copyright = "gi4nks - 2015"
app.Commands = []cli.Command{
{
Name: "run",
Aliases: []string{"ru"},
Usage: "run a command, remember to run the command with -- before. (./ambros r -- ls -la)",
Action: CmdRun,
},
{
Name: "reset",
Aliases: []string{"re"},
Usage: "reset votes",
Action: CmdReset,
},
{
Name: "mock",
Aliases: []string{"mo"},
Usage: "mock server",
Action: CmdMock,
},
}
app.Run(os.Args)
//defer closeDB()
}
// List of functions
func CmdReset(ctx *cli.Context) {
commandWrapper(ctx, func() {
parrot.Info("==> Recovery of all booked items not shipped.")
err := repository.ResetVotes()
if err != nil {
parrot.Error("Something went wrong. Please check the error.", err)
panic(err)
}
})
}
func CmdRun(ctx *cli.Context) {
commandWrapper(ctx, func() {
parrot.Info("Running decanter...")
terminated := false
repository.ResetVotes()
var rc = RestClient{}
for !terminated {
vote := repository.GetNextVote()
if vote.IsEmpty() {
terminated = true
} else {
// Now I have a vote to send
err := rc.Send(vote)
if err != nil {
parrot.Error("Something went wrong. Please check the error.", err)
panic(err)
}
err = repository.ArchiveVote(vote.ID)
if err != nil {
parrot.Error("Something went wrong. Please check the error.", err)
panic(err)
}
}
}
})
}
func CmdMock(ctx *cli.Context) {
commandWrapper(ctx, func() {
parrot.Info("==> Mocking a server on port 8080")
// url -i -X POST -H "Content-Type: application/json" -d "{ \"talkid\": \"Talk1\", \"rating\": \"2\" }" http://localhost:8080/api/v1/votes
serve()
})
}
func CmdWrapper(ctx *cli.Context) {
}
// -------------------------------
// Cli command wrapper
// -------------------------------
func commandWrapper(ctx *cli.Context, cmd quant.Action0) {
CmdWrapper(ctx)
cmd()
}