-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
82 lines (66 loc) · 1.96 KB
/
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
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
package main
import (
"flag"
"fmt"
"os"
)
type protoImplT func(bool, bool, string, string, []string) error
const VERSION = "1.1.1"
const usage = "Usage: goon -proto <version> [options] -- <program> [<arg>...]"
var protoFlag = flag.String("proto", "", "protocol version (one of: 1.0)")
var ackFlag = flag.String("ack", "", "arbitrary data used during handshake")
var inFlag = flag.Bool("in", false, "enable reading from stdin")
var outFlag = flag.Bool("out", false, "output program's stdout")
var errFlag = flag.String("err", "nil", "output or redirect stderr")
var dirFlag = flag.String("dir", ".", "working directory for the spawned process")
var logFlag = flag.String("log", "", "enable logging")
var versionFlag = flag.Bool("v", false, "print version and exit")
func main() {
flag.Parse()
if *versionFlag {
fmt.Printf("%s\n", VERSION)
os.Exit(0)
}
if *protoFlag == "" {
die_usage("Please specify the protocol version.")
}
if *ackFlag != "" {
os.Exit(performInitialHandshake(*protoFlag, *ackFlag))
}
initLogger(*logFlag)
args := flag.Args()
validateArgs(args)
/* Run external program and block until it terminates */
protoImpl := findProtocolImpl(*protoFlag)
if protoImpl == nil {
reason := fmt.Sprintf("Unsupported protocol version: %v", *protoFlag)
die(reason)
}
err := findProtocolImpl(*protoFlag)(*inFlag, *outFlag, *errFlag, *dirFlag, args)
if err != nil {
os.Exit(getExitStatus(err))
}
}
func performInitialHandshake(protoFlag, ackstr string) int {
if findProtocolImpl(protoFlag) == nil {
return 1
}
os.Stdout.WriteString(ackstr)
return 0
}
func validateArgs(args []string) {
if len(args) < 1 {
die_usage("Not enough arguments.")
}
logger.Printf("Flag values:\n proto: %v\n in: %v\n out: %v\n err: %v\n dir: %v\nArgs: %v\n",
*protoFlag, *inFlag, *outFlag, *errFlag, *dirFlag, args)
}
func findProtocolImpl(flag string) (impl protoImplT) {
switch flag {
case "1.0":
impl = proto_1_0
case "2.0":
impl = proto_2_0
}
return
}