main: calculate default output path if -o is not specified

This matches the Go command and is generally convenient to have.
This commit is contained in:
Ayke van Laethem
2022-03-12 20:19:07 +01:00
committed by Ron Evans
parent 7d4bf09b1a
commit e49e93f22c
2 changed files with 34 additions and 5 deletions
+11 -5
View File
@@ -149,6 +149,17 @@ func Build(pkgName, outpath string, options *compileopts.Options) error {
}
return builder.Build(pkgName, outpath, config, func(result builder.BuildResult) error {
if outpath == "" {
if strings.HasSuffix(pkgName, ".go") {
// A Go file was specified directly on the command line.
// Base the binary name off of it.
outpath = filepath.Base(pkgName[:len(pkgName)-3]) + config.DefaultBinaryExtension()
} else {
// Pick a default output path based on the main directory.
outpath = filepath.Base(result.MainDir) + config.DefaultBinaryExtension()
}
}
if err := os.Rename(result.Binary, outpath); err != nil {
// Moving failed. Do a file copy.
inf, err := os.Open(result.Binary)
@@ -1319,11 +1330,6 @@ func main() {
switch command {
case "build":
if outpath == "" {
fmt.Fprintln(os.Stderr, "No output filename supplied (-o).")
usage(command)
os.Exit(1)
}
pkgName := "."
if flag.NArg() == 1 {
pkgName = filepath.ToSlash(flag.Arg(0))