Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 1 | // Package cmd contains helpers that abstract away the chore of starting new |
| 2 | // processes, tracking their lifetime, inspecting their output, etc. |
| 3 | package cmd |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "strings" |
| 12 | |
| 13 | "source.monogon.dev/metropolis/pkg/logbuffer" |
| 14 | ) |
| 15 | |
| 16 | // RunCommand starts a new process and waits until either its completion, or |
Mateusz Zalega | 2f7790d | 2022-08-06 16:10:42 +0200 | [diff] [blame] | 17 | // until the supplied predicate function pf returns true. The function is called |
Mateusz Zalega | 6cdc976 | 2022-08-03 17:15:01 +0200 | [diff] [blame] | 18 | // for each line produced by the new process. |
Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 19 | // |
Mateusz Zalega | 2f7790d | 2022-08-06 16:10:42 +0200 | [diff] [blame] | 20 | // The returned boolean value equals the last value returned by pf. |
| 21 | // |
Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 22 | // The process will be killed both in the event the context is cancelled, and |
| 23 | // when expectedOutput is found. |
Mateusz Zalega | 6cdc976 | 2022-08-03 17:15:01 +0200 | [diff] [blame] | 24 | func RunCommand(ctx context.Context, path string, args []string, pf func(string) bool) (bool, error) { |
Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 25 | // Make a sub-context to ensure the process exits when this function is done. |
| 26 | ctx, ctxC := context.WithCancel(ctx) |
| 27 | defer ctxC() |
| 28 | |
| 29 | // Copy the stdout and stderr output to a single channel of lines so that they |
| 30 | // can then be matched against expectedOutput. |
| 31 | |
| 32 | // Since LineBuffer can write its buffered contents on a deferred Close, |
| 33 | // after the reader loop is broken, avoid deadlocks by making lineC a |
| 34 | // buffered channel. |
| 35 | lineC := make(chan string, 2) |
Serge Bazanski | e012b72 | 2023-03-29 17:49:04 +0200 | [diff] [blame^] | 36 | lineCB := func(l *logbuffer.Line) { |
| 37 | // If the context is canceled, no-one is listening on lineC anymore, so we would |
| 38 | // block. |
| 39 | select { |
| 40 | case <-ctx.Done(): |
| 41 | return |
| 42 | case lineC <- l.Data: |
| 43 | } |
| 44 | } |
| 45 | outBuffer := logbuffer.NewLineBuffer(1024, lineCB) |
Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 46 | defer outBuffer.Close() |
Serge Bazanski | e012b72 | 2023-03-29 17:49:04 +0200 | [diff] [blame^] | 47 | errBuffer := logbuffer.NewLineBuffer(1024, lineCB) |
Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 48 | defer errBuffer.Close() |
| 49 | |
| 50 | // Prepare the command context, and start the process. |
| 51 | cmd := exec.CommandContext(ctx, path, args...) |
| 52 | // Tee std{out,err} into the linebuffers above and the process' std{out,err}, to |
| 53 | // allow easier debugging. |
| 54 | cmd.Stdout = io.MultiWriter(os.Stdout, outBuffer) |
| 55 | cmd.Stderr = io.MultiWriter(os.Stderr, errBuffer) |
| 56 | if err := cmd.Start(); err != nil { |
| 57 | return false, fmt.Errorf("couldn't start the process: %w", err) |
| 58 | } |
| 59 | |
Mateusz Zalega | 2f7790d | 2022-08-06 16:10:42 +0200 | [diff] [blame] | 60 | // Handle the case in which the process finishes before pf takes the chance to |
| 61 | // kill it. |
| 62 | complC := make(chan error, 1) |
| 63 | go func() { |
| 64 | complC <- cmd.Wait() |
| 65 | }() |
| 66 | |
Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 67 | // Try matching against expectedOutput and return the result. |
| 68 | for { |
| 69 | select { |
| 70 | case <-ctx.Done(): |
| 71 | return false, ctx.Err() |
| 72 | case line := <-lineC: |
Mateusz Zalega | 6cdc976 | 2022-08-03 17:15:01 +0200 | [diff] [blame] | 73 | if pf(line) { |
Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 74 | cmd.Process.Kill() |
| 75 | cmd.Wait() |
| 76 | return true, nil |
| 77 | } |
Mateusz Zalega | 2f7790d | 2022-08-06 16:10:42 +0200 | [diff] [blame] | 78 | case err := <-complC: |
| 79 | return false, err |
Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
Mateusz Zalega | 6cdc976 | 2022-08-03 17:15:01 +0200 | [diff] [blame] | 83 | |
| 84 | // TerminateIfFound creates RunCommand predicates that instantly terminate |
| 85 | // program execution in the event the given string is found in any line |
| 86 | // produced. RunCommand will return true, if the string searched for was found, |
Mateusz Zalega | b838e05 | 2022-08-12 18:08:10 +0200 | [diff] [blame] | 87 | // and false otherwise. If logf isn't nil, it will be called whenever a new |
| 88 | // line is received. |
| 89 | func TerminateIfFound(needle string, logf func(string)) func(string) bool { |
Mateusz Zalega | 6cdc976 | 2022-08-03 17:15:01 +0200 | [diff] [blame] | 90 | return func(haystack string) bool { |
Mateusz Zalega | b838e05 | 2022-08-12 18:08:10 +0200 | [diff] [blame] | 91 | if logf != nil { |
| 92 | logf(haystack) |
| 93 | } |
Mateusz Zalega | 6cdc976 | 2022-08-03 17:15:01 +0200 | [diff] [blame] | 94 | return strings.Contains(haystack, needle) |
| 95 | } |
| 96 | } |
Mateusz Zalega | b838e05 | 2022-08-12 18:08:10 +0200 | [diff] [blame] | 97 | |
| 98 | // WaitUntilCompletion creates a RunCommand predicate that will make it wait |
| 99 | // for the process to exit on its own. If logf isn't nil, it will be called |
| 100 | // whenever a new line is received. |
| 101 | func WaitUntilCompletion(logf func(string)) func(string) bool { |
| 102 | return func(line string) bool { |
| 103 | if logf != nil { |
| 104 | logf(line) |
| 105 | } |
| 106 | return false |
| 107 | } |
| 108 | } |