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