-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows for versioned go get/install commands to bundle the module version in the binary, then the version flag can print it as-is. Prebuilt binaries will also include it via ldflags. Not for gofumports, since its days are counted. For #99.
- Loading branch information
Showing
3 changed files
with
52 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) 2020, Daniel Martí <mvdan@mvdan.cc> | ||
// See LICENSE for licensing information | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"runtime/debug" | ||
) | ||
|
||
var ( | ||
showVersion = flag.Bool("version", false, "show version and exit") | ||
|
||
version = "(devel)" // to match the default from runtime/debug | ||
) | ||
|
||
func printVersion() { | ||
// don't overwrite the version if it was set by -ldflags=-X | ||
if info, ok := debug.ReadBuildInfo(); ok && version == "(devel)" { | ||
mod := &info.Main | ||
if mod.Replace != nil { | ||
mod = mod.Replace | ||
} | ||
version = mod.Version | ||
} | ||
fmt.Println(version) | ||
} |