11 "github.com/zyedidia/micro/cmd/micro/shellwords"
14 // ExecCommand executes a command using exec
15 // It returns any output/errors
16 func ExecCommand(name string, arg ...string) (string, error) {
18 cmd := exec.Command(name, arg...)
19 outputBytes := &bytes.Buffer{}
20 cmd.Stdout = outputBytes
21 cmd.Stderr = outputBytes
26 err = cmd.Wait() // wait for command to finish
27 outstring := outputBytes.String()
31 // RunShellCommand executes a shell command and returns the output/error
32 func RunShellCommand(input string) (string, error) {
33 args, err := shellwords.Split(input)
39 return ExecCommand(inputCmd, args[1:]...)
42 func RunBackgroundShell(input string) {
43 args, err := shellwords.Split(input)
49 messenger.Message("Running...")
51 output, err := RunShellCommand(input)
52 totalLines := strings.Split(output, "\n")
54 if len(totalLines) < 3 {
56 messenger.Message(inputCmd, " exited without error")
58 messenger.Message(inputCmd, " exited with error: ", err, ": ", output)
61 messenger.Message(output)
63 // We have to make sure to redraw
68 func RunInteractiveShell(input string, wait bool, getOutput bool) (string, error) {
69 args, err := shellwords.Split(input)
75 // Shut down the screen because we're going to interact directly with the shell
81 // Set up everything for the command
82 outputBytes := &bytes.Buffer{}
83 cmd := exec.Command(inputCmd, args...)
86 cmd.Stdout = io.MultiWriter(os.Stdout, outputBytes)
88 cmd.Stdout = os.Stdout
90 cmd.Stderr = os.Stderr
92 // This is a trap for Ctrl-C so that it doesn't kill micro
93 // Instead we trap Ctrl-C to kill the program we're running
94 c := make(chan os.Signal, 1)
95 signal.Notify(c, os.Interrupt)
105 output := outputBytes.String()
108 // This is just so we don't return right away and let the user press enter to return
112 // Start the screen back up
118 // HandleShellCommand runs the shell command
119 // The openTerm argument specifies whether a terminal should be opened (for viewing output
120 // or interacting with stdin)
121 func HandleShellCommand(input string, openTerm bool, waitToFinish bool) string {
123 RunBackgroundShell(input)
126 return RunInteractiveShell(input, waitToFinish, false)