From 42aa57e2b98e07a1a72dcf27848c053783b99d8f Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Sun, 21 Apr 2024 18:01:40 +0200 Subject: [PATCH] exec: stream command output (#2166) --- providers/dns/exec/exec.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/providers/dns/exec/exec.go b/providers/dns/exec/exec.go index a07cba0a..23fdaf38 100644 --- a/providers/dns/exec/exec.go +++ b/providers/dns/exec/exec.go @@ -2,6 +2,7 @@ package exec import ( + "bufio" "context" "errors" "fmt" @@ -117,10 +118,27 @@ func (d *DNSProvider) run(ctx context.Context, command, domain, token, keyAuth s cmd := exec.CommandContext(ctx, d.config.Program, args...) - output, err := cmd.CombinedOutput() - if len(output) > 0 { - log.Println(string(output)) + stdout, err := cmd.StdoutPipe() + if err != nil { + return fmt.Errorf("create pipe: %w", err) } - return err + cmd.Stderr = cmd.Stdout + + err = cmd.Start() + if err != nil { + return fmt.Errorf("start command: %w", err) + } + + scanner := bufio.NewScanner(stdout) + for scanner.Scan() { + log.Println(scanner.Text()) + } + + err = cmd.Wait() + if err != nil { + return fmt.Errorf("wait command: %w", err) + } + + return nil }