refactor(app): typed per-command options, render in cmd (#693)

Validate now returns only what each command reads, with -b resolved to browsers or vaults, so no
stage compares against "all"; printing moves to cmd, which already printed the list tables.
This commit is contained in:
Roger authored and GitHub committed 2026-11-10 17:00:44 +08:00
1 parent a7741eaab2
commit b5eddf799b
21 files changed
+281 -266

No files matched your search

+2 -44
View File
@@ -1,15 +1,9 @@
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"
)
@@ -22,7 +16,7 @@ func newListCommand() *cobra.Command {
Example: ` hack-browser-data list
hack-browser-data list --detail`,
RunE: func(command *cobra.Command, args []string) error {
options, err := app.ListOptions()
options, err := app.ValidateList()
if err != nil {
return err
}
@@ -37,7 +31,7 @@ func newListCommand() *cobra.Command {
if !detail {
return printProfiles(command.OutOrStdout(), discovery)
}
summary, err := app.CountRecords(command.Context(), discovery, options.Categories, options.Keychain)
summary, err := app.CountRecords(command.Context(), discovery, options.Keychain)
if err != nil {
return err
}
@@ -48,39 +42,3 @@ func newListCommand() *cobra.Command {
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()
}