forked from ahmetb/govvv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (144 loc) · 3.93 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
)
func init() {
log.SetFlags(0)
}
const (
defaultPackage = "main"
flDryRun = "-print"
flDryRunPrintLdFlags = "-flags"
flPackage = "-pkg"
flVersion = "-version"
)
var (
// govvvDirectives is mapping of govvv directives, which must be elided
// when constructing the final go tool command, to a boolean which
// indicates whether the directive takes an argument or not.
govvvDirectives = map[string]bool{
flDryRun: false,
flDryRunPrintLdFlags: false,
flPackage: true,
flVersion: true}
)
func main() {
args := os.Args
if len(args) < 2 {
log.Println(`govvv: not enough arguments (try "govvv build .")`)
log.Printf("version: %s", versionString())
os.Exit(1)
} else if args[1] != "build" && args[1] != "install" && args[1] != "list" && !isGovvvDirective(args[1]) {
// do not wrap the entire 'go tool'
// "list" is wrapped to be compatible with mitchellh/gox.
log.Fatalf(`govvv: only works with "build", "install" and "list". try "go %s" instead`, args[1])
}
wd, err := os.Getwd()
if err != nil {
log.Fatalf("govvv: cannot get working directory: %v", err)
}
versionValues, err := GetFlags(wd, args)
if err != nil {
log.Fatalf("failed to collect values: %v", err)
}
ldflags, err := mkLdFlags(versionValues)
if err != nil {
log.Fatalf("failed to compile values: %v", err)
}
if _, ok := collectGovvvDirective(args, flDryRunPrintLdFlags); ok {
fmt.Print(ldflags)
return
}
args = args[1:] // rm executable name
if args[0] == "build" || args[0] == "install" {
args, err = addLdFlags(args, ldflags)
if err != nil {
log.Fatalf("failed to add ldflags to args: %v", err)
}
}
if _, ok := collectGovvvDirective(args, flDryRun); ok {
fmt.Println(goToolDryRunCmd(args))
return
}
args = scrubGovvvDirectives(args)
if err := execGoTool(args); err != nil {
log.Fatalf("go tool: %v", err)
}
}
// execGoTool invokes "go" with given arguments and passes the current
// process' standard streams.
func execGoTool(args []string) error {
cmd := exec.Command("go", args...)
cmd.Stdout, cmd.Stderr, cmd.Stdin = os.Stdout, os.Stderr, os.Stdin
return cmd.Run()
}
// goToolDryRunCmd returns a POSIX shell-compatible command that would normally
// get executed. Not guaranteed to quote and escape the args very well.
func goToolDryRunCmd(args []string) string {
var b bytes.Buffer
b.WriteString("go")
b.WriteRune(' ')
printed := false
for _, v := range scrubGovvvDirectives(args) {
if printed {
b.WriteString(" \\\n")
b.WriteString("\t")
}
if strings.ContainsAny(v, " \"'\n\t") {
v = strconv.QuoteToASCII(v)
}
b.WriteString(v)
printed = true
}
return b.String()
}
// isGovvvDirective returns true if the arg is a govvv directive, and false
// otherwise.
func isGovvvDirective(arg string) bool {
_, ok := govvvDirectives[arg]
return ok
}
// scrubGovvvDirectives filters out govvv directs to return a clean set of args
// that can be passed to the go command.
func scrubGovvvDirectives(args []string) (filtered []string) {
filtered = []string{}
skipping := 0
for _, arg := range args {
if skipping > 0 {
skipping--
continue
}
if hasArgument, ok := govvvDirectives[arg]; ok {
if hasArgument {
skipping = 1
}
} else {
filtered = append(filtered, arg)
}
}
return filtered
}
// collectGovvvDirective searches the args array for a directive and a possible
// argument for that directive. It returns that argument, or "", if the
// directive takes none, and an indication if the directive was actually found.
func collectGovvvDirective(args []string, directive string) (argument string, ok bool) {
for i, arg := range args {
if directive == arg {
hasArgument, _ := govvvDirectives[arg]
if !hasArgument {
return "", true
} else if i+1 < len(args) {
return args[i+1], true
} else {
break
}
}
}
return "", false
}