Skip to content

Commit

Permalink
improve dmsgcurl get logic, so never use file for stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpalide committed Dec 9, 2023
1 parent d79b931 commit daf6c52
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions cmd/dmsgcurl/commands/dmsgcurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ var RootCmd = &cobra.Command{
}
fmt.Println(string(respBody))
} else {

file, err := parseOutputFile(dmsgcurlOutput, u.URL.Path, fullpath)
file := os.Stdout
if !stdout {
file, err = parseOutputFile(dmsgcurlOutput, u.URL.Path, fullpath)
}
if err != nil {
return fmt.Errorf("failed to prepare output file: %w", err)
}
Expand All @@ -170,21 +172,9 @@ var RootCmd = &cobra.Command{
for i := 0; i < dmsgcurlTries; i++ {
if !stdout {
dmsgcurlLog.Debugf("Download attempt %d/%d ...", i, dmsgcurlTries)
}

if _, err := file.Seek(0, 0); err != nil {
return fmt.Errorf("failed to reset file: %w", err)
}
if stdout {
if fErr := file.Close(); fErr != nil {
dmsgcurlLog.WithError(fErr).Warn("Failed to close output file.")
}
if err != nil {
if rErr := os.RemoveAll(file.Name()); rErr != nil {
dmsgcurlLog.WithError(rErr).Warn("Failed to remove output file.")
}
if _, err := file.Seek(0, 0); err != nil {
return fmt.Errorf("failed to reset file: %w", err)
}
file = os.Stdout
}
if err := Download(ctx, dmsgcurlLog, &httpC, file, u.URL.String(), 0); err != nil {
dmsgcurlLog.WithError(err).Error()
Expand Down

0 comments on commit daf6c52

Please sign in to comment.