MCPcopy
hub / github.com/containers/toolbox / RunContextWithExitCode

Function RunContextWithExitCode

src/pkg/shell/shell.go:47–82  ·  view source on GitHub ↗
(ctx context.Context,
	name string,
	stdin io.Reader,
	stdout, stderr io.Writer,
	arg ...string)

Source from the content-addressed store, hash-verified

45}
46
47func 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
84func RunWithExitCode(name string, stdin io.Reader, stdout, stderr io.Writer, arg ...string) (int, error) {
85 ctx := context.Background()

Callers 2

RunContextFunction · 0.85
RunWithExitCodeFunction · 0.85

Calls 1

IsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…