-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathargs.go
93 lines (78 loc) · 2.62 KB
/
args.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
package kece
import (
"errors"
"flag"
"fmt"
)
// Arguments struct will hold flag and arguments from stdin
type Arguments struct {
Auth string
Network string
Port string
DataStorageType string
ShowVersion bool
Help func()
}
// ParseArgs function, this function will parse flag and arguments from stdin to Arguments struct
func ParseArgs() (*Arguments, error) {
var (
auth string
network string
port string
dataStorageType string
showVersion bool
)
flag.StringVar(&auth, "auth", "", "set server auth eg: -auth my-secret")
flag.StringVar(&network, "net", "tcp", "network type eg: -net tcp")
flag.StringVar(&port, "port", "9000", "port to listen eg: -port 9000")
flag.StringVar(&dataStorageType, "ds", HashMap, "data storage type (hashmap or binary tree)")
flag.BoolVar(&showVersion, "version", false, "show version")
flag.BoolVar(&showVersion, "v", false, "show version")
flag.Usage = func() {
printGreenColor(Banner)
fmt.Println()
printGreenColor(" **-----------------------------------------------** ")
fmt.Println()
printGreenColor(" Kece (an Experimental Distributed Key Value Store) ")
fmt.Println()
printGreenColor(" -net | --net network type eg: -net tcp")
printGreenColor(" -port | --port port to listen eg: -port 9000")
printGreenColor(" -auth | --auth if you want to client send auth before exchange data")
printGreenColor(" -ds | --ds acronym from (data storage), ")
printGreenColor(" you can choose either type (hashmap or binary tree)")
printGreenColor(" -v | --version show kece version")
printGreenColor(" -h | --help show help and usage")
fmt.Println()
printGreenColor(" **-----------------------------------------------** ")
printGreenColor(" Running: ")
printGreenColor(" kece -port 8000 -net tcp -ds (bt/hashmap)")
fmt.Println()
}
flag.Parse()
if len(network) <= 0 {
return &Arguments{Help: flag.Usage}, errors.New(" (-net) arg required")
}
if len(port) <= 0 {
return &Arguments{Help: flag.Usage}, errors.New(" (-port) arg required")
}
return &Arguments{
Auth: auth,
Network: network,
Port: port,
DataStorageType: dataStorageType,
ShowVersion: showVersion,
Help: flag.Usage,
}, nil
}
func printRedColor(s string) {
fmt.Printf("\033[31m%s\033[0m%s", s, "\n")
}
func printGreenColor(s string) {
fmt.Printf("\033[32m%s\033[0m%s", s, "\n")
}
func printYellowColor(s string) {
fmt.Printf("\033[33m%s\033[0m%s", s, "\n")
}
func printCyanColor(s string) {
fmt.Printf("\033[36m%s\033[0m%s", s, "\n")
}