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
5669360e59
4 files changed
+79
-7
No files matched your search
+1
-1
@@ -22,7 +22,7 @@ builds:
|
|||||||
flags:
|
flags:
|
||||||
- -trimpath
|
- -trimpath
|
||||||
ldflags:
|
ldflags:
|
||||||
- -s -w -X main.version={{ .Version }}
|
- -s -w -X main.version={{ .Version }} -X main.commit={{ .ShortCommit }} -X main.branch={{ .Branch }}
|
||||||
goos:
|
goos:
|
||||||
- linux
|
- linux
|
||||||
- darwin
|
- darwin
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
```
|
```
|
||||||
gosh - Go Shell
|
gosh - Go Shell
|
||||||
|
Version: 0.2.1 (branch: main, commit: 03e791c)
|
||||||
|
|
||||||
Built-in commands:
|
Built-in commands:
|
||||||
cd [dir] - Change directory (cd with no args goes to home)
|
cd [dir] - Change directory (cd with no args goes to home)
|
||||||
@@ -102,6 +103,15 @@ _gosh is not meant for production use, it is just a proof of concept._
|
|||||||
go build ./
|
go build ./
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Die Version lässt sich mit `gosh -version` (oder `--version`, `-V`) abfragen und steht auch in der Ausgabe von `help`:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ gosh -version
|
||||||
|
gosh 0.2.1 (branch: main, commit: 03e791c)
|
||||||
|
```
|
||||||
|
|
||||||
|
Version und Branch werden beim Bauen mit GoReleaser per `-ldflags` gesetzt. Bei einem einfachen `go build` steht dort `dev`, der Commit wird dann aus den von Go eingebetteten Git-Informationen gelesen (mit `-dirty`, falls es uncommittete Änderungen gab).
|
||||||
|
|
||||||
## Release
|
## Release
|
||||||
|
|
||||||
Binaries sowie `.deb`- und `.rpm`-Pakete werden mit [GoReleaser](https://goreleaser.com) gebaut (Konfiguration: `.goreleaser.yaml`).
|
Binaries sowie `.deb`- und `.rpm`-Pakete werden mit [GoReleaser](https://goreleaser.com) gebaut (Konfiguration: `.goreleaser.yaml`).
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -19,8 +20,51 @@ import (
|
|||||||
// ErrNoPath is returned when 'cd' was called without a second argument.
|
// ErrNoPath is returned when 'cd' was called without a second argument.
|
||||||
var ErrNoPath = errors.New("path required")
|
var ErrNoPath = errors.New("path required")
|
||||||
|
|
||||||
// version is set at build time via -ldflags "-X main.version=..."
|
// Build information, set at build time via
|
||||||
var version = "dev"
|
// -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
|
// previousDir tracks the previous working directory for 'cd -' support
|
||||||
var previousDir string
|
var previousDir string
|
||||||
@@ -568,9 +612,12 @@ func completeFilePath(line, prefix string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "-V") {
|
if len(os.Args) > 1 {
|
||||||
fmt.Println("gosh", version)
|
switch os.Args[1] {
|
||||||
return
|
case "-version", "--version", "-V":
|
||||||
|
fmt.Println("gosh", versionInfo())
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ctrl+C sends SIGINT to the whole foreground process group. Catch it so
|
// 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])
|
os.Setenv(parts[0], parts[1])
|
||||||
return nil
|
return nil
|
||||||
case "help":
|
case "help":
|
||||||
fmt.Println(`gosh - Go Shell
|
fmt.Println("gosh - Go Shell\nVersion: " + versionInfo() + `
|
||||||
|
|
||||||
Built-in commands:
|
Built-in commands:
|
||||||
cd [dir] - Change directory (cd with no args goes to home)
|
cd [dir] - Change directory (cd with no args goes to home)
|
||||||
|
|||||||
@@ -648,6 +648,21 @@ func TestExecInput_ExportNoArgs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestVersionInfo(t *testing.T) {
|
||||||
|
origVersion, origCommit, origBranch := version, commit, branch
|
||||||
|
defer func() { version, commit, branch = origVersion, origCommit, origBranch }()
|
||||||
|
|
||||||
|
version, commit, branch = "1.2.3", "abc1234", "main"
|
||||||
|
if got := versionInfo(); got != "1.2.3 (branch: main, commit: abc1234)" {
|
||||||
|
t.Errorf("versionInfo() = %q", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
branch = ""
|
||||||
|
if got := versionInfo(); got != "1.2.3 (commit: abc1234)" {
|
||||||
|
t.Errorf("versionInfo() without branch = %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestExecInput_Help(t *testing.T) {
|
func TestExecInput_Help(t *testing.T) {
|
||||||
// Just verify help doesn't return an error
|
// Just verify help doesn't return an error
|
||||||
// We can't easily capture stdout in a unit test without a pipe
|
// We can't easily capture stdout in a unit test without a pipe
|
||||||
|
|||||||
Reference in new issue
Block a user