13 shellquote "github.com/kballard/go-shellquote"
14 "github.com/zyedidia/micro/v2/internal/screen"
17 // ExecCommand executes a command using exec
18 // It returns any output/errors
19 func ExecCommand(name string, arg ...string) (string, error) {
21 cmd := exec.Command(name, arg...)
22 outputBytes := &bytes.Buffer{}
23 cmd.Stdout = outputBytes
24 cmd.Stderr = outputBytes
29 err = cmd.Wait() // wait for command to finish
30 outstring := outputBytes.String()
34 // RunCommand executes a shell command and returns the output/error
35 func RunCommand(input string) (string, error) {
36 args, err := shellquote.Split(input)
41 return "", errors.New("No arguments")
45 return ExecCommand(inputCmd, args[1:]...)
48 // RunBackgroundShell runs a shell command in the background
49 // It returns a function which will run the command and returns a string
51 func RunBackgroundShell(input string) (func() string, error) {
52 args, err := shellquote.Split(input)
57 return nil, errors.New("No arguments")
60 return func() string {
61 output, err := RunCommand(input)
62 totalLines := strings.Split(output, "\n")
65 if len(totalLines) < 3 {
67 str = fmt.Sprint(inputCmd, " exited without error")
69 str = fmt.Sprint(inputCmd, " exited with error: ", err, ": ", output)
76 // RunInteractiveShell runs a shellcommand interactively
77 func RunInteractiveShell(input string, wait bool, getOutput bool) (string, error) {
78 args, err := shellquote.Split(input)
83 return "", errors.New("No arguments")
87 // Shut down the screen because we're going to interact directly with the shell
88 screenb := screen.TempFini()
92 // Set up everything for the command
93 outputBytes := &bytes.Buffer{}
94 cmd := exec.Command(inputCmd, args...)
97 cmd.Stdout = io.MultiWriter(os.Stdout, outputBytes)
99 cmd.Stdout = os.Stdout
101 cmd.Stderr = os.Stderr
103 // This is a trap for Ctrl-C so that it doesn't kill micro
104 // Instead we trap Ctrl-C to kill the program we're running
105 c := make(chan os.Signal, 1)
106 signal.Notify(c, os.Interrupt)
116 output := outputBytes.String()
119 // This is just so we don't return right away and let the user press enter to return
120 screen.TermMessage("")
123 // Start the screen back up
124 screen.TempStart(screenb)
129 // UserCommand runs the shell command
130 // The openTerm argument specifies whether a terminal should be opened (for viewing output
131 // or interacting with stdin)
132 // func UserCommand(input string, openTerm bool, waitToFinish bool) string {
134 // RunBackgroundShell(input)
137 // output, _ := RunInteractiveShell(input, waitToFinish, false)