This repository was archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
81 lines (70 loc) · 1.59 KB
/
cli.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
package main
import (
"github.com/kimuraz/golang-json-db/client"
"github.com/kimuraz/golang-json-db/server"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"os"
)
func startServer(cCtx *cli.Context) error {
config := NewConfig("config.json")
server := server.NewServer(config.ServerPort)
go server.StartServer()
log.Info().Msg("Server started")
for {
message := <-server.MessageChan
if message != "" {
log.Info().Msg(message)
}
}
return nil
}
func connClient(cCtx *cli.Context) error {
port := cCtx.Args().First()
cl := client.NewClient()
cl.Connect(port)
return nil
}
func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
commands := []*cli.Command{
{
Name: "server",
Aliases: []string{"svr"},
Usage: "Server commands, it uses config.json by default",
Subcommands: []*cli.Command{
{
Name: "start",
Category: "server",
Usage: "Starts the server",
Action: startServer,
},
},
},
{
Name: "client",
Aliases: []string{"cl"},
Usage: "Client commands",
Subcommands: []*cli.Command{
{
Name: "connect",
Category: "client",
Usage: "Connects to a server",
Action: connClient,
},
},
},
}
app := &cli.App{
Name: "gjdb",
Usage: "Golang JSON DB is a fun simple project implementing a JSON-based db from scratch",
Commands: commands,
}
if err := app.Run(os.Args); err != nil {
log.Fatal().Err(err)
os.Exit(1)
}
}