From f915299fa4106c75f46bad12dd28ddb3b1d0a1f9 Mon Sep 17 00:00:00 2001 From: Petr Date: Mon, 29 Jun 2026 11:56:09 +0200 Subject: [PATCH] feat: add --backup-converted flag to keep .orig backups --- cmd/goget/setup.go | 3 +++ internal/mirror/mirror.go | 11 +++++++++++ internal/protocol/http/client.go | 1 + internal/protocol/http/config_types.go | 9 +++++---- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/cmd/goget/setup.go b/cmd/goget/setup.go index 60ce23b..15ff907 100644 --- a/cmd/goget/setup.go +++ b/cmd/goget/setup.go @@ -236,6 +236,9 @@ func ApplyFlags(c *http.Config, f *Flags, cfg *config.Config, cookieJar *cookie. if *f.AdjustExt { c.Recursive.AdjustExt = true } + if *f.BackupConverted { + c.Recursive.BackupConverted = true + } // ── Cookies from CLI flags ── if len(f.Cookies) > 0 { diff --git a/internal/mirror/mirror.go b/internal/mirror/mirror.go index 836a46b..a3052ef 100644 --- a/internal/mirror/mirror.go +++ b/internal/mirror/mirror.go @@ -44,6 +44,7 @@ type MirrorConfig struct { ConvertLinks bool // Convert links for local viewing DownloadAssets bool // Download CSS, JS, images PruneEmpty bool // Remove empty directories after download + BackupConverted bool // Keep .orig backup before link conversion RestrictFiles bool // Only download files from original host Timeout time.Duration RateLimiter *transport.TokenBucket @@ -642,6 +643,16 @@ func (m *Mirror) convertLinksInFile(filePath string) { converted := m.rewriter.RewriteLinks(doc, filePath) if converted > 0 { + // Backup original file before link conversion. + if m.config.BackupConverted { + backupPath := filePath + ".orig" + if err := os.WriteFile(backupPath, data, 0644); err != nil { + if m.config.Verbose { + fmt.Fprintf(os.Stderr, "[error] failed to backup %s: %v\n", filePath, err) + } + } + } + // Write modified HTML var buf bytes.Buffer if err := html.Render(&buf, doc); err != nil { diff --git a/internal/protocol/http/client.go b/internal/protocol/http/client.go index e32fc14..f34d859 100644 --- a/internal/protocol/http/client.go +++ b/internal/protocol/http/client.go @@ -403,6 +403,7 @@ func (c *Client) downloadMirror(ctx context.Context, req *core.DownloadRequest) ConvertLinks: c.config.Recursive.ConvertLinks, DownloadAssets: c.config.Recursive.MirrorAssets, PruneEmpty: true, + BackupConverted: c.config.Recursive.BackupConverted, RestrictFiles: true, Timeout: c.config.Transport.Timeout, RespectRobots: c.config.Recursive.RespectRobots, diff --git a/internal/protocol/http/config_types.go b/internal/protocol/http/config_types.go index 87f5829..621ad95 100644 --- a/internal/protocol/http/config_types.go +++ b/internal/protocol/http/config_types.go @@ -84,10 +84,11 @@ type RecursiveConfig struct { DryRun bool // Mirror mode - Mirror bool - MirrorAssets bool - ConvertLinks bool - RespectRobots bool + Mirror bool + MirrorAssets bool + ConvertLinks bool + BackupConverted bool + RespectRobots bool } // OutputConfig groups output, progress, and display settings.