Files
HackBrowserData/cmd/hack-browser-data/list.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

87 lines
2.5 KiB
Go

package main
import (
"fmt"
"io"
"path/filepath"
"text/tabwriter"
"github.com/spf13/cobra"
"github.com/moond4rk/hackbrowserdata/internal/app"
"github.com/moond4rk/hackbrowserdata/internal/category"
"github.com/moond4rk/hackbrowserdata/internal/log"
)
func newListCommand() *cobra.Command {
var detail bool
command := &cobra.Command{
Use: "list",
Short: "List detected browsers and profiles",
Example: ` hack-browser-data list
hack-browser-data list --detail`,
RunE: func(command *cobra.Command, args []string) error {
options, err := app.ListOptions()
if err != nil {
return err
}
discovery := app.Discover(options.Target)
for _, failure := range discovery.DirFailures {
log.Warnf("%s: %v", failure.UserDataDirID, failure.Err)
}
if len(discovery.Dirs) == 0 {
command.Println("No browsers found.")
return nil
}
if !detail {
return printProfiles(command.OutOrStdout(), discovery)
}
summary, err := app.CountRecords(command.Context(), discovery, options.Categories, options.Keychain)
if err != nil {
return err
}
return printRecordCounts(command.OutOrStdout(), discovery, summary)
},
}
command.Flags().BoolVar(&detail, "detail", false, "show per-category entry counts")
return command
}
func printProfiles(out io.Writer, discovery app.Discovery) error {
table := tabwriter.NewWriter(out, 0, 0, 3, ' ', 0)
fmt.Fprintln(table, "Browser\tProfile\tPath")
for _, dir := range discovery.Dirs {
for _, profile := range dir.Profiles {
profilePath := filepath.Join(dir.Path, filepath.FromSlash(profile.RelativePath))
fmt.Fprintf(table, "%s\t%s\t%s\n", dir.Browser.DisplayName, profile.Name, profilePath)
}
}
return table.Flush()
}
func printRecordCounts(out io.Writer, discovery app.Discovery, summary app.Summary) error {
counts := make(map[app.Extraction]int, len(summary.Results))
for _, result := range summary.Results {
counts[result.Extraction] = result.Records
}
table := tabwriter.NewWriter(out, 0, 0, 3, ' ', 0)
fmt.Fprint(table, "Browser\tProfile")
for _, dataCategory := range category.All() {
fmt.Fprintf(table, "\t%s", dataCategory.String())
}
fmt.Fprintln(table)
for _, dir := range discovery.Dirs {
for _, profile := range dir.Profiles {
fmt.Fprintf(table, "%s\t%s", dir.Browser.DisplayName, profile.Name)
for _, dataCategory := range category.All() {
fmt.Fprintf(table, "\t%d", counts[app.Extraction{UserDataDirID: dir.ID, ProfileName: profile.Name, Category: dataCategory}])
}
fmt.Fprintln(table)
}
}
return table.Flush()
}