-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·118 lines (97 loc) · 2.37 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
package main
import (
"errors"
"flag"
"fmt"
"math/rand"
"net/http"
"os"
"strconv"
"time"
"github.com/fluxynet/sequitur"
"github.com/mscraftsman/devcon-feedback/config"
"github.com/mscraftsman/devcon-feedback/sessionize"
"github.com/mscraftsman/devcon-feedback/store"
log "github.com/sirupsen/logrus"
)
// Version info
var (
appVersion = "n/a"
appCommit = "n/a"
appBuilt = "n/a"
)
func main() {
v := flag.Bool("v", false, "prints current app version")
r := flag.Bool("r", false, "triggers database resetdb process")
envfile := flag.String("c", ".env", "-c /path/to/my/env.file [defaults to .env]")
flag.Parse()
switch true {
default:
serve(*envfile)
case *v:
version()
case *r:
resetdb(*envfile)
}
}
func serve(envfile string) {
config.Load(envfile)
sessionize.LoadSessions()
if err := store.Init(); err != nil {
log.WithField("error", err).Fatalln("serve:init:store")
}
router := initRouter()
log.WithField("port", config.HTTPPort).Infoln("serve")
_ = http.ListenAndServe(":"+config.HTTPPort, router)
}
func version() {
fmt.Printf("Version: %s \nCommit: %s \nBuilt: %s", appVersion, appCommit, appBuilt)
os.Exit(0)
}
func resetdb(envfile string) {
var (
response string
sequence sequitur.Sequence
)
defer sequence.Catch(func(name string, err error) {
log.WithField("error", err).Error("error when: " + name)
})
sequence.Do("asking for confirmation", func() error {
fmt.Println("Are you sure you want to reset the database? [YES/...]")
if _, err := fmt.Scanln(&response); err != nil {
return err
} else if response != "YES" {
return errors.New("process cancelled")
}
return nil
})
sequence.Do("checking arithmetic skills", func() error {
x := rand.Intn(1000)
y := rand.Intn(1000)
fmt.Printf("%d + %d = ", x, y)
if _, err := fmt.Scanln(&response); err != nil {
return err
} else if response != strconv.Itoa(x+y) {
return errors.New("arithmetic skills bad")
}
return nil
})
sequence.Do("engaging countdown", func() error {
fmt.Println("engaging countdown...")
for x := 15; x > 0; x-- {
fmt.Printf("...%d", x)
time.Sleep(time.Second)
}
log.Println()
log.Warn("Resetting database")
return nil
})
sequence.Do("clearing db", func() error {
config.Load(envfile)
sessionize.LoadSessions()
return store.ClearDB()
})
sequence.Then(func() {
log.Println("feedback cleared from database")
})
}