| 273 | } |
| 274 | |
| 275 | func RunRawCommand(command string, inputs []string) (int, string) { |
| 276 | logs := new(strings.Builder) |
| 277 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 278 | defer cancel() |
| 279 | |
| 280 | fmt.Print("Running command: ", command) |
| 281 | |
| 282 | c := exec.CommandContext(ctx, "bash", "-c", command) |
| 283 | |
| 284 | ptyF, err := pty.Start(c) |
| 285 | if err != nil { |
| 286 | log.Panic(err) |
| 287 | } |
| 288 | defer ptyF.Close() |
| 289 | |
| 290 | for _, input := range inputs { |
| 291 | fmt.Fprintf(ptyF, "%s\n", input) |
| 292 | } |
| 293 | |
| 294 | var cmdOutput string |
| 295 | if err := c.Wait(); err != nil { |
| 296 | if ctx.Err() == context.DeadlineExceeded { |
| 297 | cmdOutput = "Command timed out:\n" |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | io.Copy(logs, ptyF) |
| 302 | cmdOutput += logs.String() |
| 303 | cmdOutput = strings.TrimLeft(cmdOutput, strings.Join(inputs, "\r\n")) |
| 304 | |
| 305 | return c.ProcessState.ExitCode(), cmdOutput |
| 306 | } |