diff --git a/process.go b/process.go index bcf8d08..4bfa36e 100644 --- a/process.go +++ b/process.go @@ -50,12 +50,7 @@ func (p *Process) Write(b []byte) (int, error) { func (p *Process) start() error { cmdArgs := strings.Split(p.service.Command, " ") - - if p.service.ExecTimeout > 0 { - p.createProcessCtx(cmdArgs) - } else { - p.createProcess(cmdArgs) - } + p.createProcess(cmdArgs) defer func() { if p.pid == 0 && p.cancel != nil { p.cancel() @@ -89,26 +84,13 @@ func (p *Process) start() error { return nil } -// create command for the process with ExecTimeout -func (p *Process) createProcessCtx(cmdArgs []string) { - if len(cmdArgs) < 2 { - var ctx context.Context - ctx, p.cancel = context.WithTimeout(context.Background(), p.service.ExecTimeout) - p.command = exec.CommandContext(ctx, p.service.Command) //nolint:gosec - } else { - var ctx context.Context - ctx, p.cancel = context.WithTimeout(context.Background(), p.service.ExecTimeout) - p.command = exec.CommandContext(ctx, cmdArgs[0], cmdArgs[1:]...) //nolint:gosec - } -} - // create command for the process func (p *Process) createProcess(cmdArgs []string) { - if len(cmdArgs) < 2 { - p.command = exec.CommandContext(context.Background(), p.service.Command) //nolint:gosec - } else { - p.command = exec.CommandContext(context.Background(), cmdArgs[0], cmdArgs[1:]...) //nolint:gosec + ctx := context.Background() + if p.service.ExecTimeout > 0 { + ctx, p.cancel = context.WithTimeout(ctx, p.service.ExecTimeout) } + p.command = exec.CommandContext(ctx, cmdArgs[0], cmdArgs[1:]...) //nolint:gosec } func (p *Process) configureUser() error { diff --git a/process_test.go b/process_test.go index 6bca5f6..6095623 100644 --- a/process_test.go +++ b/process_test.go @@ -37,11 +37,9 @@ func TestCreateProcess(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { p := &Process{service: &Service{Command: tt.command, ExecTimeout: tt.execTimeout}} + p.createProcess(strings.Split(tt.command, " ")) if tt.execTimeout > 0 { - p.createProcessCtx(strings.Split(tt.command, " ")) t.Cleanup(p.cancel) - } else { - p.createProcess(strings.Split(tt.command, " ")) } require.Equal(t, tt.args, p.command.Args)