-
Notifications
You must be signed in to change notification settings - Fork 0
/
genid.go
107 lines (99 loc) · 3.08 KB
/
genid.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
package main
import (
"fmt"
ap "github.com/akamensky/argparse"
core "github.com/kooixh/genid/pkg/app"
c "github.com/kooixh/genid/pkg/constants"
"github.com/kooixh/genid/utils/redis"
"os"
)
func main() {
fmt.Println("Genid Id Generation System")
parser := ap.NewParser("main", "Configuration provided for core genid")
calibrate := parser.Flag("c", "calibrate", &ap.Options{Required: false, Help: "Initiate calibration"})
refill := parser.Flag("r", "refill", &ap.Options{Required: false, Help: "Refill ids"})
info := parser.Flag("i", "info", &ap.Options{Required: false, Help: "Show info of app"})
initial := parser.Int("", "initial", &ap.Options{Required: false, Help: "Initial starting number for id",
Default: 100})
total := parser.Int("", "total", &ap.Options{Required: false, Help: "Total number stored each refill",
Default: 100})
idType := parser.String("t", "type", &ap.Options{Required: false, Help: "Type of id to generate (alphanum, num). Default alphanum",
Default: c.GeneratorTypeAlphaNum})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
os.Exit(1)
}
res := redis.Ping()
if res.Err() != nil {
fmt.Println("error connecting to Redis database")
os.Exit(1)
}
refillChannel := make(chan int)
if *calibrate {
os.Exit(initCalibration(*initial, *total, *idType, refillChannel))
} else if *refill {
os.Exit(initRefill(refillChannel))
} else if *info {
os.Exit(initPrintInfo())
}
if !core.CheckIsCalibrated() {
fmt.Println("Genid is not calibrated, calibrate before using -c")
os.Exit(1)
}
id := core.GenerateNewId(refillChannel)
if id == nil {
fmt.Println("Genid was unable to generate an Id because of an unknown error")
os.Exit(1)
}
fmt.Println("id generated by Genid is", *id)
exitCode := <- refillChannel
os.Exit(exitCode)
}
func initCalibration(initial int, total int, idType string, refillChannel chan int) int {
settings := core.CalibrationSettings{
Initial: initial,
Offset: c.InitialOffset,
Total: total,
IdType: idType,
}
fmt.Println("Starting Genid calibration with settings", settings)
validate, msg := settings.Validate()
if !validate {
fmt.Println(msg)
return 1
}
core.Calibrate(settings, refillChannel)
exit := <-refillChannel
fmt.Println("Completed Genid Calibration")
return exit
}
func initRefill(refillChannel chan int) int {
fmt.Println("Refilling the lists of Ids")
if !core.CheckIsCalibrated() {
fmt.Println("Genid is not calibrated, calibrate before using -c")
return 1
}
go core.Refill(refillChannel)
exit := <-refillChannel
fmt.Println("Successfully refilled the lists of Ids")
return exit
}
func initPrintInfo() int {
appInfo, err := core.ReturnAppInfo()
if err != nil {
fmt.Println(err)
return 1
}
fmt.Println("Status:", appInfo.Status)
fmt.Println("Version:", appInfo.Version)
if appInfo.Status == c.Calibrated {
fmt.Println("Next Id:", appInfo.NextId)
settings := appInfo.Settings
fmt.Println("Initial Id:", settings.Initial)
fmt.Println("Id Type:", settings.IdType)
fmt.Println("Offset:", settings.Offset)
fmt.Println("Refill Amount:", settings.Total)
}
return 0
}