( ctx context.Context, command string, args []string, dir string, continueOnError bool, stdout io.Writer, stderr io.Writer, stdin io.Reader, environ expand.Environ, execHandler types.ExecHandler, )
| 31 | } |
| 32 | |
| 33 | func ExecutePipelineShellCommand( |
| 34 | ctx context.Context, |
| 35 | command string, |
| 36 | args []string, |
| 37 | dir string, |
| 38 | continueOnError bool, |
| 39 | stdout io.Writer, |
| 40 | stderr io.Writer, |
| 41 | stdin io.Reader, |
| 42 | environ expand.Environ, |
| 43 | execHandler types.ExecHandler, |
| 44 | ) (*interp.Runner, error) { |
| 45 | // Replace runtime environment variables with ., so a runtime.images.test => runtime_images_test |
| 46 | // which otherwise wouldn't be correct syntax |
| 47 | command = replaceVariablesRegEx.ReplaceAllStringFunc(command, func(s string) string { |
| 48 | if strings.Contains(s, ".") { |
| 49 | return strings.ReplaceAll(s, ".", types.DotReplacement) |
| 50 | } |
| 51 | |
| 52 | return s |
| 53 | }) |
| 54 | |
| 55 | // Let's parse the complete command |
| 56 | file, err := syntax.NewParser().Parse(strings.NewReader(command), "") |
| 57 | if err != nil { |
| 58 | return nil, errors.Wrap(err, "parse shell command") |
| 59 | } |
| 60 | |
| 61 | // Get current working directory |
| 62 | if dir == "" { |
| 63 | dir, err = os.Getwd() |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // create options |
| 70 | options := []interp.RunnerOption{ |
| 71 | interp.Dir(dir), |
| 72 | interp.StdIO(stdin, stdout, stderr), |
| 73 | interp.Env(environ), |
| 74 | interp.ExecHandler(execHandler.ExecHandler), |
| 75 | } |
| 76 | if !continueOnError { |
| 77 | options = append(options, interp.Params("-e")) |
| 78 | } |
| 79 | |
| 80 | // Create shell runner |
| 81 | r, err := interp.New(options...) |
| 82 | if err != nil { |
| 83 | return nil, errors.Wrap(err, "create shell runner") |
| 84 | } |
| 85 | r.Params = args |
| 86 | |
| 87 | // Run command |
| 88 | err = r.Run(ctx, file) |
| 89 | if err != nil { |
| 90 | if status, ok := interp.IsExitStatus(err); ok && status == 0 { |
no test coverage detected