From cd4ca2bdf41e55290a3630783328165ab5570676 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 8 Jan 2023 10:06:03 +0100 Subject: [PATCH] feat(shell): add a CLI timeout of 4 seconds resolves 3332 --- src/platform/cmd/run.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/platform/cmd/run.go b/src/platform/cmd/run.go index d817087b..0359e802 100644 --- a/src/platform/cmd/run.go +++ b/src/platform/cmd/run.go @@ -2,12 +2,18 @@ package cmd import ( "bytes" + "context" "os/exec" "strings" + "time" ) +// Run is used to correctly run a command with a timeout. 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 err bytes.Buffer cmd.Stdout = &out