Skip to content

Commit 091d905

Browse files
committed
Set default timeout to 30min and add WithoutTimeout option
1 parent e391838 commit 091d905

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Default option functions:
3434

3535
- `cmd.WithStandardStreams`
3636
- `cmd.WithTimeout(time.Duration)`
37+
- `cmd.WithoutTimeout`
3738

3839
#### Example
3940

command.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Command struct {
3030

3131
// NewCommand creates a new command
3232
// You can add option with variadic option argument
33+
// Default timeout is set to 30 minutes
3334
//
3435
// Example:
3536
// c := cmd.NewCommand("echo hello", function (c *Command) {
@@ -45,7 +46,7 @@ type Command struct {
4546
func NewCommand(cmd string, options ...func(*Command)) *Command {
4647
c := &Command{
4748
Command: cmd,
48-
Timeout: 1 * time.Minute,
49+
Timeout: 30 * time.Minute,
4950
executed: false,
5051
Env: []string{},
5152
}
@@ -84,6 +85,11 @@ func WithTimeout(t time.Duration) func(c *Command) {
8485
}
8586
}
8687

88+
// WithoutTimeout disables the timeout for the command
89+
func WithoutTimeout(c *Command) {
90+
c.Timeout = 0
91+
}
92+
8793
// AddEnv adds an environment variable to the command
8894
// If a variable gets passed like ${VAR_NAME} the env variable will be read out by the current shell
8995
func (c *Command) AddEnv(key string, value string) {
@@ -147,6 +153,12 @@ func (c *Command) Execute() error {
147153
cmd.Stderr = c.StderrWriter
148154
cmd.Dir = c.WorkingDir
149155

156+
// Create timer only if timeout was set > 0
157+
var timeoutChan = make(<-chan time.Time, 1)
158+
if c.Timeout != 0 {
159+
timeoutChan = time.After(c.Timeout)
160+
}
161+
150162
err := cmd.Start()
151163
if err != nil {
152164
return err
@@ -173,15 +185,14 @@ func (c *Command) Execute() error {
173185
break
174186
}
175187
c.exitCode = 0
176-
case <-time.After(c.Timeout):
188+
case <-timeoutChan:
177189
quit <- true
178190
if err := cmd.Process.Kill(); err != nil {
179191
return fmt.Errorf("Timeout occurred and can not kill process with pid %v", cmd.Process.Pid)
180192
}
181193
return fmt.Errorf("Command timed out after %v", c.Timeout)
182194
}
183195

184-
//Remove leading and trailing whitespaces
185196
c.executed = true
186197

187198
return nil

command_linux_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestCommand_ExecuteStderr(t *testing.T) {
1919
}
2020

2121
func TestCommand_WithTimeout(t *testing.T) {
22-
cmd := NewCommand("sleep 0.01;", WithTimeout(1*time.Millisecond))
22+
cmd := NewCommand("sleep 0.1;", WithTimeout(1*time.Millisecond))
2323

2424
err := cmd.Execute()
2525

@@ -65,3 +65,12 @@ func TestCommand_WithStandardStreams(t *testing.T) {
6565
assert.Nil(t, err)
6666
assert.Equal(t, "hey\n", string(r))
6767
}
68+
69+
func TestCommand_WithoutTimeout(t *testing.T) {
70+
cmd := NewCommand("sleep 0.001; echo hello", WithoutTimeout)
71+
72+
err := cmd.Execute()
73+
74+
assert.Nil(t, err)
75+
assert.Equal(t, "hello\n", cmd.Stdout())
76+
}

0 commit comments

Comments
 (0)