/
shell_cmd.go
76 lines (63 loc) · 1.56 KB
/
shell_cmd.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
67
68
69
70
71
72
73
74
75
76
package commands
import (
"errors"
"os/exec"
"github.com/bitwormhole/starter-cli/cli"
"github.com/bitwormhole/starter/markup"
)
type Shell struct {
markup.Component `class:"cli-handler"`
}
func (inst *Shell) _Impl() cli.Handler {
return inst
}
func (inst *Shell) GetHelpInfo() *cli.CommandHelpInfo {
info := &cli.CommandHelpInfo{}
info.Name = "shell"
info.Title = "执行操作系统中的命令"
info.Description = "shell(aka.'sh')将委托操作系统执行指定的命令。"
info.Content = "usage: command [arguments...]"
return info
}
func (inst *Shell) Init(service cli.Service) error {
err := service.RegisterHandler("sh", inst)
if err != nil {
return err
}
err = service.RegisterHandler("shell", inst)
if err != nil {
return err
}
return nil
}
func (inst *Shell) Handle(ctx *cli.TaskContext) error {
console := ctx.Console
name, args, err := inst.prepareArgs(ctx.CurrentTask.Arguments)
if err != nil {
return err
}
cmd := exec.Command(name, args...)
cmd.Stderr = console.Error()
cmd.Stdout = console.Output()
cmd.Stdin = console.Input()
cmd.Dir = console.GetWD()
cmd.Env = nil
err = cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if err != nil {
console.Error().Write([]byte(err.Error()))
}
return nil
}
func (inst *Shell) prepareArgs(args []string) (string, []string, error) {
const index = 1
if index < len(args) {
name := args[index]
a2 := args[index+1:]
return name, a2, nil
}
return "", nil, errors.New("bad arguments")
}