-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
61 lines (54 loc) · 1.12 KB
/
main.go
File metadata and controls
61 lines (54 loc) · 1.12 KB
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
package main
import (
"fmt"
"os"
"strings"
"github.com/ev-the-dev/redis-go-clone/config"
"github.com/ev-the-dev/redis-go-clone/server"
)
const ErrMainArg = "main: arg:"
func main() {
args := os.Args[1:]
cfg, err := parseArgs(args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
s := server.New(cfg)
s.Start()
}
// TODO: validate that the args have present and valid values (directory and filename)
func parseArgs(args []string) (*config.Config, error) {
cfg := config.New()
for i := 0; i < len(args); i++ {
var a, v string
argPair := strings.Split(args[i], "=")
a = argPair[0]
if len(argPair) == 2 {
v = argPair[1]
}
switch strings.ToLower(a) {
case "--dir":
if v != "" {
cfg.Dir = v
} else {
if i+1 >= len(args) {
return nil, fmt.Errorf("%s parse: --dir requires argument", ErrMainArg)
}
cfg.Dir = args[i+1]
i++
}
case "--dbfilename":
if v != "" {
cfg.DBFilename = v
} else {
if i+1 >= len(args) {
return nil, fmt.Errorf("%s parse: --dbfilename requires argument", ErrMainArg)
}
cfg.DBFilename = args[i+1]
i++
}
}
}
return cfg, nil
}