Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 5 additions & 23 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading