ExecuteCommand executes a command from the config
(ctx context.Context, cmd *latest.CommandConfig, variables map[string]interface{}, args []string, dir string, stdout io.Writer, stderr io.Writer, stdin io.Reader)
| 296 | |
| 297 | // ExecuteCommand executes a command from the config |
| 298 | func ExecuteCommand(ctx context.Context, cmd *latest.CommandConfig, variables map[string]interface{}, args []string, dir string, stdout io.Writer, stderr io.Writer, stdin io.Reader) error { |
| 299 | shellCommand := strings.TrimSpace(cmd.Command) |
| 300 | shellArgs := cmd.Args |
| 301 | appendArgs := cmd.AppendArgs |
| 302 | |
| 303 | extraEnv := map[string]string{} |
| 304 | for k, v := range variables { |
| 305 | extraEnv[k] = fmt.Sprintf("%v", v) |
| 306 | } |
| 307 | if shellArgs == nil { |
| 308 | if appendArgs { |
| 309 | // Append args to shell command |
| 310 | for _, arg := range args { |
| 311 | arg = strings.ReplaceAll(arg, "'", "'\"'\"'") |
| 312 | |
| 313 | shellCommand += " '" + arg + "'" |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // execute the command in a shell |
| 318 | err := engine.ExecuteSimpleShellCommand(ctx, dir, env.NewVariableEnvProvider(expand.ListEnviron(os.Environ()...), extraEnv), stdout, stderr, stdin, shellCommand, args...) |
| 319 | if err != nil { |
| 320 | if status, ok := interp.IsExitStatus(err); ok { |
| 321 | return &exit.ReturnCodeError{ |
| 322 | ExitCode: int(status), |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return errors.Wrap(err, "execute command") |
| 327 | } |
| 328 | |
| 329 | return nil |
| 330 | } |
| 331 | |
| 332 | shellArgs = append(shellArgs, args...) |
| 333 | return command.Command(ctx, dir, env.NewVariableEnvProvider(expand.ListEnviron(os.Environ()...), extraEnv), stdout, stderr, stdin, shellCommand, shellArgs...) |
| 334 | } |
| 335 | |
| 336 | // RunCommandCmd holds the cmd flags of a run command |
| 337 | type RunCommandCmd struct { |
no test coverage detected