From 38124c70c676945587db80eb5abc4e2f25f51ed3 Mon Sep 17 00:00:00 2001 From: phm07 <22707808+phm07@users.noreply.github.com> Date: Wed, 3 Jul 2024 10:29:32 +0200 Subject: [PATCH] also write command debug line to stderr --- internal/state/state.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/internal/state/state.go b/internal/state/state.go index fd91fa9c..96089182 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -3,6 +3,7 @@ package state import ( "context" "fmt" + "io" "os" "strings" @@ -87,25 +88,27 @@ func (c *state) newClient() (hcapi2.Client, error) { return nil, err } + var debugWriter io.Writer if filePath == "" { - opts = append(opts, hcloud.WithDebugWriter(os.Stderr)) + debugWriter = os.Stderr } else { f, err := os.OpenFile(filePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) if err != nil { return nil, err } + debugWriter = f + } - quotedArgs := make([]string, 0, len(os.Args)) - for _, arg := range os.Args { - quotedArgs = append(quotedArgs, fmt.Sprintf("%q", arg)) - } - _, err = f.WriteString("--- Command:\n" + strings.Join(quotedArgs, " ") + "\n\n\n\n") - if err != nil { - return nil, err - } - - opts = append(opts, hcloud.WithDebugWriter(f)) + quotedArgs := make([]string, 0, len(os.Args)) + for _, arg := range os.Args { + quotedArgs = append(quotedArgs, fmt.Sprintf("%q", arg)) } + _, err = debugWriter.Write([]byte("--- Command:\n" + strings.Join(quotedArgs, " ") + "\n\n\n\n")) + if err != nil { + return nil, err + } + + opts = append(opts, hcloud.WithDebugWriter(debugWriter)) } pollInterval, err := config.OptionPollInterval.Get(c.config)