| 235 | } |
| 236 | |
| 237 | func RunCommand(command string, inputs []string) (int, string) { |
| 238 | logs := new(strings.Builder) |
| 239 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 240 | defer cancel() |
| 241 | |
| 242 | // build the command |
| 243 | cmdArgs := strings.Split(command, " ") |
| 244 | c := exec.CommandContext(ctx, config.Get("bin"), cmdArgs...) |
| 245 | |
| 246 | // Start the command with a pty (pseudo terminal) |
| 247 | // This is required to interact with the command |
| 248 | ptyF, err := pty.Start(c) |
| 249 | if err != nil { |
| 250 | log.Panic(err) |
| 251 | } |
| 252 | defer ptyF.Close() |
| 253 | |
| 254 | for _, input := range inputs { |
| 255 | fmt.Fprintf(ptyF, "%s\n", input) |
| 256 | } |
| 257 | |
| 258 | var cmdOutput string |
| 259 | if err := c.Wait(); err != nil { |
| 260 | if ctx.Err() == context.DeadlineExceeded { |
| 261 | cmdOutput = "Command timed out:\n" |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Copy the logs from the pty |
| 266 | io.Copy(logs, ptyF) |
| 267 | cmdOutput += logs.String() |
| 268 | |
| 269 | // TODO: find if there is a better way to remove stdins from the output |
| 270 | cmdOutput = strings.TrimLeft(cmdOutput, strings.Join(inputs, "\r\n")) |
| 271 | |
| 272 | return c.ProcessState.ExitCode(), cmdOutput |
| 273 | } |
| 274 | |
| 275 | func RunRawCommand(command string, inputs []string) (int, string) { |
| 276 | logs := new(strings.Builder) |