-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
51 lines (44 loc) · 937 Bytes
/
main.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
package main
import (
"errors"
"fmt"
"io"
"os"
"github.com/practicalgo/code/chap2/sub-cmd-arch/cmd"
)
var errInvalidSubCommand = errors.New("Invalid sub-command specified")
func printUsage(w io.Writer) {
fmt.Fprintf(w, "Usage: mync [http|grpc] -h\n")
cmd.HandleHttp(w, []string{"-h"})
cmd.HandleGrpc(w, []string{"-h"})
}
func handleCommand(w io.Writer, args []string) error {
var err error
if len(args) < 1 {
err = errInvalidSubCommand
} else {
switch args[0] {
case "http":
err = cmd.HandleHttp(w, args[1:])
case "grpc":
err = cmd.HandleGrpc(w, args[1:])
case "-h":
printUsage(w)
case "-help":
printUsage(w)
default:
err = errInvalidSubCommand
}
}
if errors.Is(err, cmd.ErrNoServerSpecified) || errors.Is(err, errInvalidSubCommand) {
fmt.Fprintln(w, err)
printUsage(w)
}
return err
}
func main() {
err := handleCommand(os.Stdout, os.Args[1:])
if err != nil {
os.Exit(1)
}
}