Debian Codegraph @

internal/shell/shell.go

raw
1 package shell
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "io"
8 "os"
9 "os/exec"
10 "os/signal"
11
12 shellquote "github.com/kballard/go-shellquote"
13 "github.com/zyedidia/micro/v2/internal/screen"
14 "github.com/zyedidia/micro/v2/internal/util"
15 )
16
17 // ExecCommand executes a command using exec
18 // It returns any output/errors
19 func ExecCommand(name string, arg ...string) (string, error) {
20 var err error
21 cmd := exec.Command(name, arg...)
22 outputBytes := &bytes.Buffer{}
23 cmd.Stdout = outputBytes
24 cmd.Stderr = outputBytes
25 err = cmd.Start()
26 if err != nil {
27 return "", err
28 }
29 err = cmd.Wait() // wait for command to finish
30 outstring := outputBytes.String()
31 return outstring, err
32 }
33
34 // RunCommand executes a shell command and returns the output/error
35 func RunCommand(input string) (string, error) {
36 args, err := shellquote.Split(input)
37 if err != nil {
38 return "", err
39 }
40 if len(args) == 0 {
41 return "", errors.New("No arguments")
42 }
43 inputCmd := args[0]
44
45 return ExecCommand(inputCmd, args[1:]...)
46 }
47
48 // RunBackgroundShell runs a shell command in the background
49 // It returns a function which will run the command and returns a string
50 // message result
51 func RunBackgroundShell(input string) (func() string, error) {
52 args, err := shellquote.Split(input)
53 if err != nil {
54 return nil, err
55 }
56 if len(args) == 0 {
57 return nil, errors.New("No arguments")
58 }
59 inputCmd := args[0]
60 return func() string {
61 output, err := RunCommand(input)
62
63 str := output
64 if err != nil {
65 str = fmt.Sprint(inputCmd, " exited with error: ", err, ": ", output)
66 }
67 return str
68 }, nil
69 }
70
71 // RunInteractiveShell runs a shellcommand interactively
72 func RunInteractiveShell(input string, wait bool, getOutput bool) (string, error) {
73 args, err := shellquote.Split(input)
74 if err != nil {
75 return "", err
76 }
77 if len(args) == 0 {
78 return "", errors.New("No arguments")
79 }
80 inputCmd := args[0]
81
82 // Shut down the screen because we're going to interact directly with the shell
83 screenb := screen.TempFini()
84
85 args = args[1:]
86
87 // Set up everything for the command
88 outputBytes := &bytes.Buffer{}
89 cmd := exec.Command(inputCmd, args...)
90 cmd.Stdin = os.Stdin
91 if getOutput {
92 cmd.Stdout = io.MultiWriter(os.Stdout, outputBytes)
93 } else {
94 cmd.Stdout = os.Stdout
95 }
96 cmd.Stderr = os.Stderr
97
98 // This is a trap for Ctrl-C so that it doesn't kill micro
99 // micro is killed if the signal is ignored only on Windows, so it is
100 // received
101 c := make(chan os.Signal, 1)
102 signal.Reset(os.Interrupt)
103 signal.Notify(c, os.Interrupt)
104 err = cmd.Start()
105 if err == nil {
106 err = cmd.Wait()
107 if wait {
108 // This is just so we don't return right away and let the user press enter to return
109 screen.TermMessage("")
110 }
111 } else {
112 screen.TermMessage(err)
113 }
114
115 output := outputBytes.String()
116
117 // Start the screen back up
118 screen.TempStart(screenb)
119
120 signal.Notify(util.Sigterm, os.Interrupt)
121 signal.Stop(c)
122
123 return output, err
124 }
125
126 // UserCommand runs the shell command
127 // The openTerm argument specifies whether a terminal should be opened (for viewing output
128 // or interacting with stdin)
129 // func UserCommand(input string, openTerm bool, waitToFinish bool) string {
130 // if !openTerm {
131 // RunBackgroundShell(input)
132 // return ""
133 // } else {
134 // output, _ := RunInteractiveShell(input, waitToFinish, false)
135 // return output
136 // }
137 // }