-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.go
More file actions
269 lines (235 loc) · 7.06 KB
/
Copy pathshell.go
File metadata and controls
269 lines (235 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cobrashell
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/chzyer/readline"
"github.com/google/shlex"
)
const (
defaultPrompt = "> "
defaultCompletionTimeout = 500 * time.Millisecond
)
// Shell wraps a Cobra binary in an interactive readline loop. Create one with
// [New] and start it with [Run].
type Shell struct {
cfg Config
binary string // resolved absolute path; empty when initErr is set
initErr error // deferred error from New, returned by Run
sessionEnv map[string]string // runtime env overrides; set via SetEnv/UnsetEnv
lastExitCode int // exit code of the most recently executed command
}
// New creates a Shell from cfg. BinaryPath is resolved to an absolute path
// immediately; if resolution fails the error is stored and returned by [Run].
// All zero-value Config fields are replaced with defaults before Run is called.
//
// New never returns nil.
func New(cfg Config) *Shell {
s := &Shell{}
binary, err := resolveBinary(cfg.BinaryPath)
if err != nil {
s.initErr = fmt.Errorf("cobra-shell: resolve binary %q: %w", cfg.BinaryPath, err)
s.cfg = cfg
return s
}
s.binary = binary
s.sessionEnv = make(map[string]string)
if cfg.Prompt == "" {
cfg.Prompt = defaultPrompt
}
if cfg.CompletionTimeout == 0 {
cfg.CompletionTimeout = defaultCompletionTimeout
}
if cfg.HistoryFile == "" {
cfg.HistoryFile = defaultHistoryFilePath(binary)
}
s.cfg = cfg
return s
}
// Run starts the interactive shell loop. It blocks until the user exits via
// Ctrl-D or the built-in "exit" command.
//
// Run returns a non-nil error if:
// - BinaryPath could not be resolved (error stored by [New])
// - readline fails to initialise (e.g. history file is unwritable)
//
// A clean exit (Ctrl-D, "exit") returns nil.
func (s *Shell) Run() error {
if s.initErr != nil {
return s.initErr
}
initialPrompt := s.cfg.Prompt
if s.cfg.DynamicPrompt != nil {
initialPrompt = s.cfg.DynamicPrompt(0)
}
rl, err := readline.NewEx(&readline.Config{
Prompt: initialPrompt,
HistoryFile: s.cfg.HistoryFile,
AutoComplete: &completer{shell: s},
InterruptPrompt: "",
EOFPrompt: "exit",
})
if err != nil {
return fmt.Errorf("cobra-shell: initialise readline: %w", err)
}
defer rl.Close()
if s.cfg.Hooks.OnStart != nil {
s.cfg.Hooks.OnStart(s)
}
for {
if s.cfg.PrePrompt != "" {
fmt.Print(s.cfg.PrePrompt)
}
line, err := rl.Readline()
if err == io.EOF {
break
}
if errors.Is(err, readline.ErrInterrupt) {
// Ctrl-C at the prompt clears the line; readline resets the
// display automatically — just loop back for a fresh prompt.
continue
}
if err != nil {
return fmt.Errorf("cobra-shell: readline: %w", err)
}
line = strings.TrimSpace(line)
if line == "" {
// Empty input is a no-op: no subprocess, no history entry.
continue
}
if line == "exit" {
break
}
s.execute(line)
if s.cfg.DynamicPrompt != nil {
rl.SetPrompt(s.cfg.DynamicPrompt(s.lastExitCode))
}
}
if s.cfg.Hooks.OnExit != nil {
s.cfg.Hooks.OnExit()
}
return nil
}
// execute tokenises line, runs BeforeExec, spawns the binary, and runs
// AfterExec. SIGINT is caught in the parent while the child runs so that
// Ctrl-C cancels the child but does not exit the shell.
func (s *Shell) execute(line string) {
tokens, err := shlex.Split(line)
if err != nil {
writeErr("cobra-shell: parse error: %v\n", err)
return
}
if len(tokens) == 0 {
return
}
// The env built-in is handled entirely in-process; it does not invoke the
// binary and does not trigger BeforeExec/AfterExec hooks.
if s.handleEnvBuiltin(tokens) {
return
}
if hasPipe(tokens) {
s.executePipeline(line, tokens)
return
}
if s.cfg.Hooks.BeforeExec != nil {
if err := s.cfg.Hooks.BeforeExec(tokens); err != nil {
writeErr("%v\n", err)
return
}
}
exitCode, err := spawnCommand(s.binary, tokens, s.buildEnv())
if err != nil {
writeErr("cobra-shell: %v\n", err)
}
s.lastExitCode = exitCode
if s.cfg.EnvBuiltin != "" && isRootHelp(tokens) {
fmt.Printf("\nShell built-ins:\n %-12s %s\n", s.cfg.EnvBuiltin,
"Manage session-scoped environment variables")
}
if s.cfg.Hooks.AfterExec != nil {
s.cfg.Hooks.AfterExec(tokens, exitCode)
}
}
// hasPipe reports whether any token is a standalone "|".
// shlex produces "|" as its own token only when surrounded by spaces,
// matching standard shell convention.
func hasPipe(tokens []string) bool {
for _, t := range tokens {
if t == "|" {
return true
}
}
return false
}
// isRootHelp reports whether tokens is a root-level help request:
// bare "help", "--help", or "-h" with no additional arguments.
// Used to decide whether to append the shell built-ins section to help output.
func isRootHelp(tokens []string) bool {
return len(tokens) == 1 && (tokens[0] == "help" || tokens[0] == "--help" || tokens[0] == "-h")
}
// leftOfFirstPipe returns the tokens before the first "|".
// Used to give BeforeExec/AfterExec hooks the cobra-command tokens.
func leftOfFirstPipe(tokens []string) []string {
for i, t := range tokens {
if t == "|" {
return tokens[:i]
}
}
return tokens
}
// executePipeline handles lines containing "|" by delegating to sh -c.
// The raw user line is passed verbatim; the binary path is single-quoted
// and prepended (s.binary is an absolute path and cannot contain a single-quote).
// BeforeExec and AfterExec receive only the left-side (cobra) tokens.
func (s *Shell) executePipeline(line string, tokens []string) {
leftTokens := leftOfFirstPipe(tokens)
if s.cfg.Hooks.BeforeExec != nil {
if err := s.cfg.Hooks.BeforeExec(leftTokens); err != nil {
writeErr("%v\n", err)
return
}
}
// Single-quote the binary path so sh treats it as a literal.
// s.binary is always an absolute path produced by filepath.Abs or
// exec.LookPath, which never yields a path containing a single-quote.
script := "'" + s.binary + "' " + line
cmd := exec.Command("sh", "-c", script)
cmd.Env = s.buildEnv()
exitCode, err := runPlain(cmd)
if err != nil {
writeErr("cobra-shell: %v\n", err)
}
s.lastExitCode = exitCode
if s.cfg.Hooks.AfterExec != nil {
s.cfg.Hooks.AfterExec(leftTokens, exitCode)
}
}
// resolveBinary resolves path to an absolute path. Bare names (no path
// separator) are looked up via exec.LookPath; paths with a separator are
// passed through filepath.Abs.
func resolveBinary(path string) (string, error) {
if path == "" {
return "", errors.New("BinaryPath must not be empty")
}
if strings.ContainsRune(path, filepath.Separator) {
return filepath.Abs(path)
}
return exec.LookPath(path)
}
// defaultHistoryFilePath returns ~/.{basename}_history for the given resolved
// binary path. Errors from os.UserHomeDir are silently ignored; readline
// handles an empty HistoryFile gracefully (no persistence).
func defaultHistoryFilePath(binary string) string {
home, err := os.UserHomeDir()
if err != nil {
return ""
}
base := filepath.Base(binary)
base = strings.TrimSuffix(base, filepath.Ext(base))
return filepath.Join(home, "."+base+"_history")
}