show version with branch and commit in help and -version
- add -version flag (besides --version and -V) - help output includes the version - commit and branch are set via ldflags by goreleaser; plain go build falls back to the VCS revision embedded by Go Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
03e791cbcd
commit
f51680f83c
4 files changed
+79
-7
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"os/signal"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
@@ -19,8 +20,51 @@ import (
|
||||
// ErrNoPath is returned when 'cd' was called without a second argument.
|
||||
var ErrNoPath = errors.New("path required")
|
||||
|
||||
// version is set at build time via -ldflags "-X main.version=..."
|
||||
var version = "dev"
|
||||
// Build information, set at build time via
|
||||
// -ldflags "-X main.version=... -X main.commit=... -X main.branch=...".
|
||||
var (
|
||||
version = "dev"
|
||||
commit = ""
|
||||
branch = ""
|
||||
)
|
||||
|
||||
// versionInfo returns the version including branch and commit, e.g.
|
||||
// "0.2.1 (branch: main, commit: 03e791c)". Without ldflags (plain go build)
|
||||
// the commit is taken from the VCS information that Go embeds in the binary.
|
||||
func versionInfo() string {
|
||||
c := commit
|
||||
if c == "" {
|
||||
if info, ok := debug.ReadBuildInfo(); ok {
|
||||
var dirty bool
|
||||
for _, s := range info.Settings {
|
||||
switch s.Key {
|
||||
case "vcs.revision":
|
||||
c = s.Value
|
||||
case "vcs.modified":
|
||||
dirty = s.Value == "true"
|
||||
}
|
||||
}
|
||||
if len(c) > 7 {
|
||||
c = c[:7]
|
||||
}
|
||||
if c != "" && dirty {
|
||||
c += "-dirty"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var details []string
|
||||
if branch != "" {
|
||||
details = append(details, "branch: "+branch)
|
||||
}
|
||||
if c != "" {
|
||||
details = append(details, "commit: "+c)
|
||||
}
|
||||
if len(details) == 0 {
|
||||
return version
|
||||
}
|
||||
return version + " (" + strings.Join(details, ", ") + ")"
|
||||
}
|
||||
|
||||
// previousDir tracks the previous working directory for 'cd -' support
|
||||
var previousDir string
|
||||
@@ -568,9 +612,12 @@ func completeFilePath(line, prefix string) []string {
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "-V") {
|
||||
fmt.Println("gosh", version)
|
||||
return
|
||||
if len(os.Args) > 1 {
|
||||
switch os.Args[1] {
|
||||
case "-version", "--version", "-V":
|
||||
fmt.Println("gosh", versionInfo())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Ctrl+C sends SIGINT to the whole foreground process group. Catch it so
|
||||
@@ -727,7 +774,7 @@ func execInput(input string) error {
|
||||
os.Setenv(parts[0], parts[1])
|
||||
return nil
|
||||
case "help":
|
||||
fmt.Println(`gosh - Go Shell
|
||||
fmt.Println("gosh - Go Shell\nVersion: " + versionInfo() + `
|
||||
|
||||
Built-in commands:
|
||||
cd [dir] - Change directory (cd with no args goes to home)
|
||||
|
||||
Reference in new issue
Block a user