This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
/
parser.go
147 lines (120 loc) · 3.15 KB
/
parser.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package configuration
import (
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"os"
"runtime"
"github.com/rebel-l/sessionservice/src/response"
)
const Version = "0.1.0"
var instance *Parser
type Parser struct {
filename string
servicePort int
serviceLogLevel int
serviceSessionLifetime int
version bool
redisAddr string
redisPassword string
redisDb int
printVersion func()
}
func GetParser() *Parser {
if instance == nil {
instance = new(Parser)
instance.printVersion = printVersion
instance.parseCliArgs()
}
return instance
}
func (p *Parser) Parse() *Config {
if p.version {
p.printVersion()
}
c := newConfig(p.filename)
if p.servicePort > 0 {
c.Service.Port = p.servicePort
}
if p.serviceLogLevel > 0 {
c.Service.LogLevel = log.Level(p.serviceLogLevel)
}
if p.serviceSessionLifetime > 0 {
c.Service.SessionLifetime = p.serviceSessionLifetime
}
if p.redisAddr != "" {
c.Redis.Addr = p.redisAddr
}
if p.redisPassword != "" {
c.Redis.Password = p.redisPassword
}
if p.redisDb > 0 {
c.Redis.DB = p.redisDb
}
return c
}
func (p *Parser) parseCliArgs() {
var filename *string
var servicePort *int
var serviceLogLevel *int
var serviceSessionLifetime *int
var version *bool
var redisAddr *string
var redisPassword *string
var redisDb *int
if flag.Lookup("config") == nil {
filename = flag.String(
"config",
"",
"A filename from where to load a config from. The file must be in JSON format. This parameter is optional.",
)
}
if flag.Lookup("Service.Port") == nil {
servicePort = flag.Int(
"Service.Port",
0,
"The port on which this service should serve. This parameter is optional.",
)
}
if flag.Lookup("Service.LogLevel") == nil {
serviceLogLevel = flag.Int(
"Service.LogLevel",
0,
"The level this service logging on. 0 = highest level, 5 = lowest level. This parameter is optional.",
)
}
if flag.Lookup("Service.SessionLifetime") == nil {
serviceSessionLifetime = flag.Int(
"Service.SessionLifetime",
0,
fmt.Sprintf("The lifetime of the session in seconds. Default is %d", response.LIFETIME),
)
}
if flag.Lookup("v") == nil {
version = flag.Bool("v", false, "Shows Version information.")
}
if flag.Lookup("Redis.Addr") == nil {
redisAddr = flag.String("Redis.Addr", "", "The Hostname of the Redis server. Format: hostname:port. This parameter is optional.")
}
if flag.Lookup("Redis.Password") == nil {
redisPassword = flag.String("Redis.Password", "", "The password of the Redis server. This parameter is optional.")
}
if flag.Lookup("Redis.DB") == nil {
redisDb = flag.Int("Redis.DB", 0, "The DB of the Redis server. This parameter is optional.")
}
flag.Parse()
p.filename = *filename
p.servicePort = *servicePort
p.serviceLogLevel = *serviceLogLevel
p.serviceSessionLifetime = *serviceSessionLifetime
p.redisAddr = *redisAddr
p.redisDb = *redisDb
p.redisPassword = *redisPassword
p.version = *version
return
}
func printVersion() {
fmt.Printf("SessionService Version: %s\n", Version)
fmt.Printf("Runtime Version: %s\n\n", runtime.Version())
os.Exit(0)
}