v0.5.0 c37833b

runner.go RawHistoryFolder

1package shellcmd
2
3import (
4	"bytes"
5	"context"
6	"errors"
7	"io"
8	"os/exec"
9	"syscall"
10
11	"github.com/bnema/vev/internal/ports"
12)
13
14// Runner executes bounded shell commands for bar scripts and similar usecases.
15type Runner struct{}
16
17// New returns a production shell command runner.
18func New() Runner { return Runner{} }
19
20// Run executes spec.Command through sh -c, capturing bounded stdout and stderr.
21func (Runner) Run(ctx context.Context, spec ports.CommandSpec) (ports.ShellCommandResult, error) {
22	cmd := exec.CommandContext(ctx, "sh", "-c", spec.Command)
23	cmd.Env = spec.Env
24	cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
25	cmd.WaitDelay = spec.Timeout
26	cmd.Cancel = func() error {
27		if cmd.Process == nil {
28			return nil
29		}
30		return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
31	}
32	var stdout, stderr boundedBuffer
33	stdout.limit = spec.StdoutLimit
34	stderr.limit = spec.StderrLimit
35	cmd.Stdout = &stdout
36	cmd.Stderr = &stderr
37
38	err := cmd.Run()
39	res := ports.ShellCommandResult{Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}
40	if err != nil {
41		var exitErr *exec.ExitError
42		if errors.As(err, &exitErr) {
43			res.ExitCode = exitErr.ExitCode()
44		} else {
45			res.ExitCode = -1
46		}
47		return res, err
48	}
49	return res, nil
50}
51
52type boundedBuffer struct {
53	buf   bytes.Buffer
54	limit int
55}
56
57func (b *boundedBuffer) Write(p []byte) (int, error) {
58	if b.limit <= 0 {
59		return len(p), nil
60	}
61	remaining := b.limit - b.buf.Len()
62	if remaining > 0 {
63		if len(p) < remaining {
64			remaining = len(p)
65		}
66		_, _ = b.buf.Write(p[:remaining])
67	}
68	return len(p), nil
69}
70
71func (b *boundedBuffer) Bytes() []byte { return b.buf.Bytes() }
72
73var _ ports.ShellCommandRunner = Runner{}
74var _ io.Writer = (*boundedBuffer)(nil)

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 12

Tags 11

↑↓ moveenter openesc close