Skip to content

Commit

Permalink
Issue-70: Add timeout to zfs/zpool commands
Browse files Browse the repository at this point in the history
  • Loading branch information
avnish30jn committed Jul 27, 2018
1 parent d5b1632 commit c30c995
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package zfs

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"time"

"github.com/pborman/uuid"
)
Expand All @@ -21,8 +24,16 @@ type command struct {
}

func (c *command) Run(arg ...string) ([][]string, error) {
var cmd *exec.Cmd

cmd := exec.Command(c.Command, arg...)
value := os.Getenv("COMMAND_TIMEOUT")
if timeout, err := time.ParseDuration(value); value != "" && err != nil {
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()
cmd = exec.CommandContext(ctx, c.Command, arg...)
} else {
cmd = exec.Command(c.Command, arg...)
}

var stdout, stderr bytes.Buffer

Expand Down

0 comments on commit c30c995

Please sign in to comment.