feat(shell): add a CLI timeout of 4 seconds

resolves 3332
This commit is contained in:
Jan De Dobbeleer 2023-01-08 10:06:03 +01:00 committed by Jan De Dobbeleer
parent 5eb6e99ea3
commit cd4ca2bdf4

View file

@ -2,12 +2,18 @@ package cmd
import ( import (
"bytes" "bytes"
"context"
"os/exec" "os/exec"
"strings" "strings"
"time"
) )
// Run is used to correctly run a command with a timeout.
func Run(command string, args ...string) (string, error) { func Run(command string, args ...string) (string, error) {
cmd := exec.Command(command, args...) // set a timeout of 4 seconds
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
defer cancel()
cmd := exec.CommandContext(ctx, command, args...)
var out bytes.Buffer var out bytes.Buffer
var err bytes.Buffer var err bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out