Skip to content

Commit

Permalink
fix: debug log is truncated if it already exists (#793)
Browse files Browse the repository at this point in the history
This fixes #763. It also writes the used command to the debug logs for
easier debugging.
  • Loading branch information
phm07 authored Jul 4, 2024
1 parent 8c6fec9 commit c3d3a9f
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package state

import (
"context"
"fmt"
"io"
"os"
"strings"

"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state/config"
Expand Down Expand Up @@ -85,12 +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 {
writer, _ := os.Create(filePath)
opts = append(opts, hcloud.WithDebugWriter(writer))
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 = 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)
Expand Down

0 comments on commit c3d3a9f

Please sign in to comment.