Exec's `journalctl` with the provided arguments and hooks it up to the given stdout/stderr streams.
(stdout, stderr io.Writer, journalctlArgs []string, stopChannel chan os.Signal)
| 161 | // Exec's `journalctl` with the provided arguments and hooks it up |
| 162 | // to the given stdout/stderr streams. |
| 163 | func FetchLogs(stdout, stderr io.Writer, journalctlArgs []string, stopChannel chan os.Signal) error { |
| 164 | journalctl, err := exec.LookPath("journalctl") |
| 165 | if err != nil { |
| 166 | return fmt.Errorf("could not find `journalctl` executable in PATH: %w", err) |
| 167 | } |
| 168 | |
| 169 | cmd := exec.Command(journalctl, journalctlArgs...) |
| 170 | cmd.Stdout = stdout |
| 171 | cmd.Stderr = stderr |
| 172 | |
| 173 | if err := cmd.Start(); err != nil { |
| 174 | return fmt.Errorf("failed to start journalctl command with args %#v: %w", journalctlArgs, err) |
| 175 | } |
| 176 | |
| 177 | // Setup killing goroutine: |
| 178 | killed := false |
| 179 | go func() { |
| 180 | <-stopChannel |
| 181 | killed = true |
| 182 | log.L.Debugf("killing journalctl logs process with PID: %#v", cmd.Process.Pid) |
| 183 | cmd.Process.Kill() |
| 184 | }() |
| 185 | |
| 186 | err = cmd.Wait() |
| 187 | if exitError, ok := err.(*exec.ExitError); ok { |
| 188 | if !killed && exitError.ExitCode() != 0 { |
| 189 | return fmt.Errorf("journalctl command exited with non-zero exit code (%d): %w", exitError.ExitCode(), exitError) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return nil |
| 194 | } |
| 195 | |
| 196 | // Formats command line arguments for `journalctl` with the provided log viewing options and |
| 197 | // exec's and redirects `journalctl`s outputs to the provided io.Writers. |