-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
137 lines (123 loc) · 3.18 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
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
package main
import (
"log"
"os"
"sort"
"strings"
"github.com/urfave/cli"
)
func main() {
app := &cli.App{
Name: "minidfs",
Usage: "Example: cli [COMMAND]",
Commands: []cli.Command{
{
Name: "master",
Usage: "start the master server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "port",
Usage: "Port to run the server on",
},
&cli.IntFlag{
Name: "chunkSize",
Usage: "Chunk size",
},
},
Action: func(c *cli.Context) error {
port := c.String("port")
if port == "" {
log.Fatal("please specify a port using '--port 8000'")
}
chunkSize := c.Uint64("chunkSize")
if chunkSize == 0 {
log.Fatal("please specify a chunk size using '--chunkSize 1024'")
}
master := NewMasterServer(port, chunkSize)
return master.run()
},
},
{
Name: "chunkserver",
Usage: "start a instance of the chunkserver",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "port",
Usage: "Port to run the server on",
},
&cli.StringFlag{
Name: "master",
Usage: "Master server url",
},
&cli.StringFlag{
Name: "dir",
Usage: "Directory to store the files in",
},
},
Action: func(c *cli.Context) error {
port := c.String("port")
if port == "" {
log.Fatal("please specify a port using '--port 8001'")
}
master := c.String("master")
if master == "" {
log.Fatal("please specify the master url using '--master http://localhost:8000'")
}
dir := c.String("dir")
if dir == "" {
log.Fatal("please specify a valid directory using '--dir ./data'")
}
chunkserver := NewChunkserver(master, port, dir)
return chunkserver.run()
},
},
{
Name: "client",
Usage: "interact with the dfs",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "master",
Usage: "Master server url",
},
&cli.StringFlag{
Name: "action",
Usage: "action to perform (read/write)",
},
&cli.StringFlag{
Name: "filename",
Usage: "input file name",
},
&cli.StringFlag{
Name: "output-filename",
Usage: "output file name",
},
},
Action: func(c *cli.Context) error {
master := c.String("master")
if master == "" {
log.Fatal("please specify the master url using '--master http://localhost:8000'")
}
action := strings.ToLower(c.String("action"))
if action != "read" && action != "write" {
log.Fatal("action needs to specified as read or write using '--action read'")
}
filename := c.String("filename")
if filename == "" {
log.Fatal("please specify a filename using '--filename input.txt'")
}
outputFilename := c.String("output-filename")
if action == "read" && outputFilename == "" {
log.Fatal("please specify a output filename using '--output-filename output.txt' for the read action")
}
client := NewClient(master, action, filename, outputFilename)
return client.run()
},
},
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}