Skip to content

Commit 4007aea

Browse files
committed
build: add output name flag -o.
1 parent 2435c07 commit 4007aea

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

cmd/build/main.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424

2525
func main() {
2626
log.SetFlags(log.Lshortfile)
27-
buildMode, tags := parseFlags()
27+
buildMode, tags, output := parseFlags()
2828
cwd, err := os.Getwd()
2929
if err != nil {
3030
log.Fatal("could not get working directory:", err)
@@ -43,7 +43,7 @@ func main() {
4343
const (
4444
goBin = "go"
4545
goBuild = "build"
46-
maxArgs = 4
46+
maxArgs = 5
4747
)
4848
goArgs := make([]string, 1, maxArgs)
4949
goArgs[0] = goBuild
@@ -61,6 +61,9 @@ func main() {
6161
if tags != "" {
6262
goArgs = append(goArgs, "-tags="+tags)
6363
}
64+
if output != "" {
65+
goArgs = append(goArgs, "-o="+output)
66+
}
6467
goArgs = append(goArgs, pkgFSPath)
6568
cmd := exec.Command(goBin, goArgs...)
6669
cmd.Stdin = os.Stdin
@@ -76,7 +79,7 @@ func main() {
7679
}
7780
}
7881

79-
func parseFlags() (buildMode, string) {
82+
func parseFlags() (mode buildMode, tags string, output string) {
8083
const (
8184
regularUsage = "standard go build with no compiler or linker flags when building"
8285
releaseUsage = "remove extra debugging data when building"
@@ -86,7 +89,6 @@ func parseFlags() (buildMode, string) {
8689
var (
8790
cmdName = commandName()
8891
flagSet = flag.NewFlagSet(cmdName, flag.ExitOnError)
89-
mode = release
9092
modeUsage = fmt.Sprintf(
9193
"%s\t- %s"+
9294
"\n%s\t- %s"+
@@ -97,12 +99,12 @@ func parseFlags() (buildMode, string) {
9799
debug.String(), debugUsage,
98100
)
99101
)
102+
mode = release
100103
flagSet.Func(modeName, modeUsage, func(arg string) (err error) {
101104
mode, err = generic.ParseEnum(regular, debug, arg)
102105
return
103106
})
104107
flagSet.Lookup(modeName).DefValue = mode.String()
105-
var tags string
106108
const (
107109
tagName = "tags"
108110
tagsUsage = "a comma-separated list of build tags" +
@@ -111,6 +113,12 @@ func parseFlags() (buildMode, string) {
111113
"\nnoipfs - build without IPFS guest support"
112114
)
113115
flagSet.StringVar(&tags, tagName, "", tagsUsage)
116+
const (
117+
outputName = "o"
118+
outputUsage = "write the resulting executable" +
119+
" to the named output file or directory"
120+
)
121+
flagSet.StringVar(&output, outputName, "", outputUsage)
114122
if err := flagSet.Parse(os.Args[1:]); err != nil {
115123
log.Fatal(err)
116124
}
@@ -123,7 +131,7 @@ func parseFlags() (buildMode, string) {
123131
output.String(),
124132
)
125133
}
126-
return mode, tags
134+
return
127135
}
128136

129137
func commandName() string {

0 commit comments

Comments
 (0)