-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (38 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
package main
import (
"fmt"
"io"
"os"
"os/signal"
"github.com/kkrull/gohttp/main/cmd"
)
const maxConnections uint = 4
func main() {
factory := &cmd.InterruptFactory{
Interrupts: subscribeToSignals(os.Interrupt),
MaxConnections: maxConnections}
gohttp := &GoHTTP{
CommandParser: factory.CliCommandParser(),
Stderr: os.Stderr}
code, err := gohttp.Run(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "gohttp: %s\n", err.Error())
}
os.Exit(code)
}
type GoHTTP struct {
CommandParser CommandParser
Stderr io.Writer
}
func (gohttp *GoHTTP) Run(args []string) (exitCode int, runErr error) {
command := gohttp.CommandParser.Parse(args)
return command.Run(gohttp.Stderr)
}
func subscribeToSignals(sig os.Signal) <-chan os.Signal {
interrupts := make(chan os.Signal, 1)
signal.Notify(interrupts, sig)
return interrupts
}
type CommandParser interface {
Parse(args []string) cmd.CliCommand
}