Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f51680f83c | ||
|
|
03e791cbcd |
No files matched your search
+1
-1
@@ -22,7 +22,7 @@ builds:
|
||||
flags:
|
||||
- -trimpath
|
||||
ldflags:
|
||||
- -s -w -X main.version={{ .Version }}
|
||||
- -s -w -X main.version={{ .Version }} -X main.commit={{ .ShortCommit }} -X main.branch={{ .Branch }}
|
||||
goos:
|
||||
- linux
|
||||
- darwin
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
```
|
||||
gosh - Go Shell
|
||||
Version: 0.2.1 (branch: main, commit: 03e791c)
|
||||
|
||||
Built-in commands:
|
||||
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 ./
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
Binaries sowie `.deb`- und `.rpm`-Pakete werden mit [GoReleaser](https://goreleaser.com) gebaut (Konfiguration: `.goreleaser.yaml`).
|
||||
|
||||
@@ -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,10 +612,13 @@ 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)
|
||||
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
|
||||
// that it only stops the running command and not the shell. (signal.Ignore
|
||||
@@ -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)
|
||||
@@ -760,9 +807,13 @@ Features:
|
||||
// Prepare the command to execute.
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
|
||||
// Set the correct output device.
|
||||
cmd.Stderr = os.Stderr
|
||||
// Connect the command to the terminal. Without Stdin the command would
|
||||
// read from /dev/null, so interactive programs (su, vim, ssh, ...) would
|
||||
// get EOF right away. readline only reads from stdin while a line is
|
||||
// being edited, so it does not steal input from the command.
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
// Execute the command return the error.
|
||||
return cmd.Run()
|
||||
|
||||
@@ -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) {
|
||||
// Just verify help doesn't return an error
|
||||
// We can't easily capture stdout in a unit test without a pipe
|
||||
@@ -1221,3 +1236,34 @@ func TestShell_SIGINTKillsOnlyChild(t *testing.T) {
|
||||
t.Errorf("child was not interrupted (took %v)", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecInput_PassesStdin(t *testing.T) {
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
origStdin := os.Stdin
|
||||
os.Stdin = r
|
||||
defer func() {
|
||||
os.Stdin = origStdin
|
||||
r.Close()
|
||||
}()
|
||||
|
||||
if _, err := w.WriteString("hello\n"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w.Close()
|
||||
|
||||
out := filepath.Join(t.TempDir(), "out")
|
||||
t.Setenv("GOSH_TEST_OUT", out)
|
||||
if err := execInput(`sh -c 'read x; printf %s "$x" > "$GOSH_TEST_OUT"'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := os.ReadFile(out)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(got) != "hello" {
|
||||
t.Errorf("command read %q from stdin, want %q", got, "hello")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user