Files
HackBrowserData/internal/app/dump.go
T
Roger 7e6f325508 refactor(app): per-stage inputs and per-dir planning (#689)
* refactor(app): per-command flags and per-stage inputs

One Options bag fed every stage and cmd patched restore's keys into it after validation; each stage now takes what it
reads, dir-level failures are their own type, and cmd prints what a run exported.

* refactor(app): group the plan by dir and profile

Execute now walks the plan's own shape instead of filtering a flat list and looking profiles up by name.

* refactor(app): file code by command and stage

Pure moves: each command's flags and validation sit beside its stage, and execute.go keeps only the runner.

* refactor: read the keychain password in cmd

Prompting and reading stdin are terminal work, so app now receives the password func; cobra already rejects both flags.

* refactor(app): plan each user data dir right before it runs

A run-wide Plan had to drop its extractors and rebuild them with the keychain; per-dir planning keeps one extractor.
2026-11-09 17:25:36 +08:00

73 lines
1.9 KiB
Go

package app
import (
"context"
"fmt"
"os"
"github.com/moond4rk/hackbrowserdata/internal/export"
"github.com/moond4rk/hackbrowserdata/internal/log"
)
// DumpFlags are dump's command-line values as cobra parsed them.
type DumpFlags struct {
Browser string
Categories string
Format string
OutputDir string
Zip bool
ProfilePath string
}
// ValidateDump checks every flag before any browser or secret is touched, so a bad flag fails fast.
func ValidateDump(flags DumpFlags, keychainPassword func(context.Context) (string, error)) (Options, error) {
browserName, err := parseBrowser(flags.Browser)
if err != nil {
return Options{}, err
}
categories, err := parseCategories(flags.Categories)
if err != nil {
return Options{}, err
}
format, err := export.ParseFormat(flags.Format)
if err != nil {
return Options{}, err
}
if err := checkOutputDir(flags.OutputDir); err != nil {
return Options{}, err
}
profilePath, err := checkProfilePath(browserName, flags.ProfilePath)
if err != nil {
return Options{}, err
}
home, err := homeDir()
if err != nil {
return Options{}, err
}
return Options{
Target: Target{BrowserName: browserName, ProfilePath: profilePath, HomeDir: home},
Categories: categories,
Output: Output{Dir: flags.OutputDir, Format: format, Zip: flags.Zip},
Keychain: KeychainConfig{HomeDir: home, Password: keychainPassword},
}, nil
}
// checkProfilePath keeps -p only when one browser is named; with -b all it is ignored, as it always was, but now said.
func checkProfilePath(browserName, path string) (string, error) {
if path == "" {
return "", nil
}
if browserName == allBrowsers {
log.Warnf("-p needs a specific -b; ignoring %s", path)
return "", nil
}
info, err := os.Stat(path)
if err != nil {
return "", fmt.Errorf("profile path: %w", err)
}
if !info.IsDir() {
return "", fmt.Errorf("profile path %s: not a directory", path)
}
return path, nil
}