-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.sgo
140 lines (116 loc) · 3.07 KB
/
main.sgo
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
138
139
140
package main
import (
"fmt"
"io"
"os"
"os/exec"
"github.com/tcard/sgo/sgo"
"github.com/tcard/sgo/sgo/scanner"
)
func main() {
if len(os.Args) == 1 {
fmt.Print(helpMsg)
return
}
var buildFlags []string
var extraArgs []string
for i, arg := range os.Args[2:] {
if arg[0] == '-' {
buildFlags = append(buildFlags, arg)
} else {
extraArgs = os.Args[i+2:]
break
}
}
switch os.Args[1] {
case "version":
fmt.Println("sgo version 0.7 (compatible with go1.7)")
return
case "run":
if len(extraArgs) == 0 {
fmt.Fprintln(os.Stderr, "sgo run: no files listed")
os.Exit(1)
}
created, errs := sgo.TranslateFilePaths(extraArgs...)
reportErrs(errs...)
if len(errs) > 0 {
os.Exit(1)
}
runGoCommand("run", buildFlags, created...)
return
case "help":
if len(extraArgs) == 0 {
fmt.Print(helpMsg)
} else {
switch extraArgs[0] {
case "translate":
fmt.Print(translateHelpMsg)
return
case "version":
fmt.Print(versionHelpMsg)
return
}
runGoCommand("help", buildFlags, extraArgs...)
}
return
case "translate":
errs := sgo.TranslateFile(func() (io.Writer \ error) { return os.Stdout \ }, os.Stdin, "stdin.sgo")
if len(errs) > 0 {
reportErrs(errs...)
os.Exit(1)
}
return
}
if len(extraArgs) == 0 {
extraArgs = append(extraArgs, ".")
}
_, warnings, errs := sgo.TranslatePaths(extraArgs)
reportErrs(warnings...)
reportErrs(errs...)
if len(errs) > 0 {
os.Exit(1)
}
runGoCommand(os.Args[1], buildFlags, extraArgs...)
}
func reportErrs(errs ...error) {
for _, err := range errs {
if errs \ ok := err.(scanner.ErrorList); ok {
for _, err := range errs {
fmt.Fprintln(os.Stderr, err)
}
} else {
fmt.Fprintln(os.Stderr, err)
}
}
}
func runGoCommand(cmd string, buildFlags []string, extraArgs ...string) {
c := exec.Command("go", append(append([]string{cmd}, buildFlags...), extraArgs...)...)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Run()
}
const helpMsg = `sgo is a tool for managing SGo source code.
Usage:
sgo command [arguments]
All commands that the go tool supports are wrapped by the sgo tool. sgo will
translate all Go files to SGo affected before running them.
To see a list of those commands:
go help
Additionally, SGo supports or overrides the following commands:
translate read SGo code, print the resulting Go code
version print SGo version, and the Go version it works with
Use "sgo help [command]" for more information about a command.
Use "go help" to see a complete list of help topics.
`
const translateHelpMsg = `usage: sgo translate
Translate reads SGo code from the standard input, and prints the resulting Go
code to the standard output.
If there are errors in the provided SGo code, they will be reported to the
standard error and the command will exit with a non-zero exit code.
`
const versionHelpMsg = `usage: sgo version
Version prints the SGo version. It also reports the Go version it is compatible
with. "Compatible" means that SGo compiles to this Go version, and is able to
import all the packages that this Go version is able to.
`