(ctx context.Context, name string, stdin io.Reader, stdout, stderr io.Writer, arg ...string)
| 45 | } |
| 46 | |
| 47 | func RunContextWithExitCode(ctx context.Context, |
| 48 | name string, |
| 49 | stdin io.Reader, |
| 50 | stdout, stderr io.Writer, |
| 51 | arg ...string) (int, error) { |
| 52 | |
| 53 | logLevel := logrus.GetLevel() |
| 54 | if stderr == nil && logLevel >= logrus.DebugLevel { |
| 55 | stderr = os.Stderr |
| 56 | } |
| 57 | |
| 58 | cmd := exec.CommandContext(ctx, name, arg...) |
| 59 | cmd.Stdin = stdin |
| 60 | cmd.Stdout = stdout |
| 61 | cmd.Stderr = stderr |
| 62 | |
| 63 | if err := cmd.Run(); err != nil { |
| 64 | if errors.Is(err, exec.ErrNotFound) { |
| 65 | return 1, fmt.Errorf("%s(1) not found", name) |
| 66 | } |
| 67 | |
| 68 | if ctxErr := ctx.Err(); ctxErr != nil { |
| 69 | return 1, ctxErr |
| 70 | } |
| 71 | |
| 72 | var exitErr *exec.ExitError |
| 73 | if errors.As(err, &exitErr) { |
| 74 | exitCode := exitErr.ExitCode() |
| 75 | return exitCode, nil |
| 76 | } |
| 77 | |
| 78 | return 1, fmt.Errorf("failed to invoke %s(1)", name) |
| 79 | } |
| 80 | |
| 81 | return 0, nil |
| 82 | } |
| 83 | |
| 84 | func RunWithExitCode(name string, stdin io.Reader, stdout, stderr io.Writer, arg ...string) (int, error) { |
| 85 | ctx := context.Background() |
no test coverage detected
searching dependent graphs…