Files
HackBrowserData/internal/app/options.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

121 lines
3.3 KiB
Go

package app
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/category"
"github.com/moond4rk/hackbrowserdata/internal/export"
)
// Options are a command's validated flags, grouped by the stage that reads them; a command leaves the rest empty.
type Options struct {
Target Target
Categories []category.Category
Output Output
Keychain KeychainConfig
}
// Target is what Discover looks for.
type Target struct {
BrowserName string // "all" or one Browser.Name
ProfilePath string // a custom profile dir, used only with one browser
HomeDir string
}
// Output is where and how Execute writes records.
type Output struct {
Dir string
Format export.Format
Zip bool
}
// KeychainConfig locates the macOS login keychain and says how to get its password; other systems ignore it.
type KeychainConfig struct {
HomeDir string
// Password runs when the keychain first opens; "" leaves it locked, and nil, as list has it, silently.
Password func(ctx context.Context) (string, error)
}
func homeDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("home directory: %w", err)
}
return home, nil
}
const allBrowsers = "all"
// parseBrowser accepts all or one of this OS's browser names; a name from another OS's table has nothing to find here.
func parseBrowser(value string) (string, error) {
name := strings.ToLower(strings.TrimSpace(value))
if name == "" || name == allBrowsers {
return allBrowsers, nil
}
for _, supportedBrowser := range browser.Supported() {
if supportedBrowser.Name == name {
return name, nil
}
}
return "", fmt.Errorf("unknown browser %q, available: %s|%s", value, allBrowsers, strings.Join(browser.Names(), "|"))
}
// parseCategories keeps a repeated category once, or its records would be exported twice.
func parseCategories(value string) ([]category.Category, error) {
value = strings.TrimSpace(value)
if strings.EqualFold(value, "all") {
return category.All(), nil
}
var categories []category.Category
seen := make(map[category.Category]bool)
for _, name := range strings.Split(value, ",") {
name = strings.TrimSpace(strings.ToLower(name))
if name == "" {
continue
}
dataCategory, err := category.Parse(name)
if err != nil {
return nil, fmt.Errorf("%w, available: all|%s", err, strings.Join(category.Names(), ","))
}
if !seen[dataCategory] {
seen[dataCategory] = true
categories = append(categories, dataCategory)
}
}
if len(categories) == 0 {
return nil, fmt.Errorf("no categories specified")
}
return categories, nil
}
// checkOutputDir creates nothing: -d, or its nearest existing ancestor, must be a directory we can create in.
func checkOutputDir(dir string) error {
path := filepath.Clean(dir)
for {
info, err := os.Stat(path)
switch {
case err == nil && info.IsDir():
if err := canCreateIn(path); err != nil {
return fmt.Errorf("output dir %s: %w", dir, err)
}
return nil
case err == nil:
return fmt.Errorf("output dir %s: %s is not a directory", dir, path)
case !errors.Is(err, fs.ErrNotExist):
return fmt.Errorf("output dir %s: %w", dir, err)
}
parent := filepath.Dir(path)
if parent == path {
return fmt.Errorf("output dir %s: no existing parent directory", dir)
}
path = parent
}
}