refactor: run every command on the pipeline (#665)

dump, dumpkeys, archive, restore, and list now run on the app stages, so cmd's old path goes away.
archive writes manifest.json and restore reads it, falling back to today's layout for older archives.
Windows and macOS smoke matched today's output apart from the planned changes.
This commit is contained in:
Roger authored and GitHub committed 2026-10-29 14:15:59 +08:00
1 parent cebfa97ed6
commit bbd353f314
45 files changed
+1516 -664

No files matched your search

+33 -62
View File
@@ -1,29 +1,18 @@
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/spf13/cobra"
"github.com/moond4rk/hackbrowserdata/browser"
"github.com/moond4rk/hackbrowserdata/internal/app"
"github.com/moond4rk/hackbrowserdata/internal/log"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
"github.com/moond4rk/hackbrowserdata/internal/snapshot"
)
func restoreCmd() *cobra.Command {
var (
keysPath string
dataDir string
dataZip string
browserName string
category string
outputFormat string
outputDir string
compress bool
)
var f app.RestoreFlags
cmd := &cobra.Command{
Use: "restore",
@@ -33,36 +22,18 @@ func restoreCmd() *cobra.Command {
hack-browser-data restore --keys keys.json --data-dir ./chrome-userdata -b chrome
ssh origin "hack-browser-data dumpkeys" | hack-browser-data restore --keys - --data-zip data.zip`,
RunE: func(cmd *cobra.Command, args []string) error {
data, err := openRestoreData(dataDir, dataZip)
if err != nil {
return err
}
defer closeRestoreData(data)
browsers, err := loadRestoreBrowsers(keysPath, data.Path(), browserName)
if err != nil {
return err
}
if len(browsers) == 0 {
log.Warnf("no browsers to restore from the supplied keys and data")
return nil
}
categories, err := parseCategories(category)
if err != nil {
return err
}
return extractAndWrite(browsers, categories, outputDir, outputFormat, compress)
return runRestore(cmd.Context(), f)
},
}
cmd.Flags().StringVar(&keysPath, "keys", "", "keys file from dumpkeys (use - for stdin)")
cmd.Flags().StringVar(&dataDir, "data-dir", "", "copied profile data dir (archive layout, or one browser's User Data with -b)")
cmd.Flags().StringVar(&dataZip, "data-zip", "", "zip produced by the archive command (alternative to --data-dir)")
cmd.Flags().StringVarP(&browserName, "browser", "b", "", "restore only this browser (optional; must match a vault in --keys)")
cmd.Flags().StringVarP(&category, "category", "c", "all", "data categories (comma-separated): all|"+categoryNames())
cmd.Flags().StringVarP(&outputFormat, "format", "f", "json", "output format: csv|json|cookie-editor")
cmd.Flags().StringVarP(&outputDir, "dir", "d", "results", "output directory")
cmd.Flags().BoolVar(&compress, "zip", false, "compress output to zip")
cmd.Flags().StringVar(&f.Keys, "keys", "", "keys file from dumpkeys (use - for stdin)")
cmd.Flags().StringVar(&f.DataDir, "data-dir", "", "copied profile data dir (archive layout, or one browser's User Data with -b)")
cmd.Flags().StringVar(&f.DataZip, "data-zip", "", "zip produced by the archive command (alternative to --data-dir)")
cmd.Flags().StringVarP(&f.Browser, "browser", "b", "", "restore only this browser (optional; must match a vault in --keys)")
cmd.Flags().StringVarP(&f.Categories, "category", "c", "all", "data categories (comma-separated): all|"+categoryNames())
cmd.Flags().StringVarP(&f.Format, "format", "f", "json", "output format: csv|json|cookie-editor")
cmd.Flags().StringVarP(&f.Dir, "dir", "d", "results", "output directory")
cmd.Flags().BoolVar(&f.Zip, "zip", false, "compress output to zip")
_ = cmd.MarkFlagRequired("keys")
cmd.MarkFlagsMutuallyExclusive("data-dir", "data-zip")
@@ -70,34 +41,34 @@ func restoreCmd() *cobra.Command {
return cmd
}
func loadRestoreBrowsers(keysPath, dataDir, browserName string) ([]browser.Browser, error) {
if keysPath == "" {
return nil, fmt.Errorf("requires --keys <file> (or - for stdin)")
}
var r io.Reader = os.Stdin
if keysPath != "-" {
f, err := os.Open(keysPath)
if err != nil {
return nil, fmt.Errorf("open keys file %q: %w", keysPath, err)
}
defer f.Close()
r = f
}
dump, err := masterkey.ReadJSON(r)
func runRestore(ctx context.Context, f app.RestoreFlags) error {
req, dump, err := app.ValidateRestore(f)
if err != nil {
return nil, fmt.Errorf("read keys file %q: %w", keysPath, err)
return err
}
data, err := openRestoreData(f.DataDir, f.DataZip)
if err != nil {
return err
}
defer closeRestoreData(data)
return browser.BuildFromDump(dump, dataDir, browserName)
d, keys, err := app.RestoreDiscovery(data, dump, req)
if err != nil {
return err
}
if len(d.Dirs) == 0 {
log.Warnf("no browsers to restore from the supplied keys and data")
return nil
}
req.Keys = keys
summary, err := app.Execute(ctx, app.NewPlan(d, req), req)
report(summary)
return err
}
// openRestoreData returns the snapshot directory restore reads from: --data-dir in place, or --data-zip extracted
// into a temporary one. Exactly one of the two must be set.
// into a temporary one.
func openRestoreData(dataDir, dataZip string) (*snapshot.Dir, error) {
if (dataDir == "") == (dataZip == "") {
return nil, fmt.Errorf("exactly one of --data-dir or --data-zip is required")
}
if dataDir != "" {
return snapshot.Open(dataDir), nil
}