mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
Names say what each thing is: no package stutter, one word per concept, spelled out, none that differ only in case. The browser table puts one field per line. Wire names (flags, output fields, keys.json) are unchanged.
56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package export
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/moond4rk/hackbrowserdata/internal/category"
|
|
"github.com/moond4rk/hackbrowserdata/internal/record"
|
|
)
|
|
|
|
// Format defaults to JSON at its zero value, as -f does.
|
|
type Format int
|
|
|
|
const (
|
|
JSON Format = iota
|
|
CSV
|
|
CookieEditor
|
|
)
|
|
|
|
func ParseFormat(value string) (Format, error) {
|
|
switch value {
|
|
case "json":
|
|
return JSON, nil
|
|
case "csv":
|
|
return CSV, nil
|
|
case "cookie-editor":
|
|
return CookieEditor, nil
|
|
default:
|
|
return 0, fmt.Errorf("unsupported format: %s", value)
|
|
}
|
|
}
|
|
|
|
func (f Format) ext() string {
|
|
if f == CSV {
|
|
return "csv"
|
|
}
|
|
return "json"
|
|
}
|
|
|
|
// encoder writes one category's file; it frames the document itself so that every Write can append to it.
|
|
type encoder interface {
|
|
encode(row record.Record) error
|
|
end() error
|
|
}
|
|
|
|
func newEncoder(writer io.Writer, format Format, dataCategory category.Category) encoder {
|
|
switch {
|
|
case format == CSV:
|
|
return newCSVEncoder(writer)
|
|
case format == CookieEditor && dataCategory == category.Cookie:
|
|
return &jsonEncoder{writer: writer, convert: toCookieEditor}
|
|
default:
|
|
return &jsonEncoder{writer: writer, escapeHTML: true}
|
|
}
|
|
}
|