-
Notifications
You must be signed in to change notification settings - Fork 35
/
cmd_shell.go
66 lines (57 loc) · 1.22 KB
/
cmd_shell.go
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
// Copyright 2016 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/google/shlex"
)
// cmdShell runs a shell command.
type cmdShell struct {
ctx *Cmd
}
func (cmd *cmdShell) Name() string {
return "/!"
}
func (cmd *cmdShell) Run(args []string) error {
sh := exec.Command(args[0], args[1:]...)
sh.Stdin = os.Stdin
sh.Stdout = os.Stdout
sh.Stderr = os.Stderr
return sh.Run()
}
func (cmd *cmdShell) Help(w io.Writer) {
fmt.Fprintf(w, "%s \t-- run a shell command\n", cmd.Name())
}
func (cmd *cmdShell) Complete(line string) []string {
var o []string
args, err := shlex.Split(line)
if err != nil {
cmd.ctx.msg.Printf("error splitting line: %v\n", err)
return o
}
if len(args) < 2 {
return o
}
i := len(args) - 1
if args[i] != "" {
matches, err := filepath.Glob(args[i] + "*")
//fmt.Printf(">>> matches: %v\nerr=%v\n", matches, err)
if err != nil {
return o
}
for _, m := range matches {
mm := strings.Trim(m, "\t\n\r ")
if mm != "" {
args[i] = mm
o = append(o, strings.Join(args, " "))
}
}
}
return o
}