Skip to content

Commit 6076cb1

Browse files
author
Jari Turkia
committed
feat: Introduced -detailed to display all fields of a record instead of hard-coded list of fields
1 parent 57855c5 commit 6076cb1

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

cmd/enpasscli/main.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type Args struct {
6868
pinEnable *bool
6969
sort *bool
7070
trashed *bool
71+
detailed *bool
7172
and *bool
7273
clipboardPrimary *bool
7374
// write command flags
@@ -91,6 +92,7 @@ func (args *Args) parse() {
9192
args.and = flag.Bool("and", false, "Combines filters with AND instead of default OR.")
9293
args.sort = flag.Bool("sort", false, "Sort the output by title and username of the 'list' and 'show' command.")
9394
args.trashed = flag.Bool("trashed", false, "Show trashed items in the 'list' and 'show' command.")
95+
args.detailed = flag.Bool("detailed", false, "Show every field of each entry in 'list' and 'show'. Without this flag, only the original summary fields (title, login, category, label, type) are displayed.")
9496
args.clipboardPrimary = flag.Bool("clipboardPrimary", false, "Use primary X selection instead of clipboard for the 'copy' command.")
9597
// write command flags
9698
args.title = flag.String("title", "", "Entry title (for create/edit).")
@@ -259,6 +261,66 @@ func collectEntries(vault *enpass.Vault, args *Args, includeSensitive bool) ([]e
259261
}
260262

261263
func outputEntriesOrLog(logger *logrus.Logger, entries []entryView, args *Args) {
264+
if *args.detailed {
265+
outputDetailed(logger, entries, args)
266+
return
267+
}
268+
outputCompact(logger, entries, args)
269+
}
270+
271+
// outputCompact reproduces the original list/show output: one row per entry
272+
// with the summary fields title, login, category, label, type — plus password
273+
// when present (show mode).
274+
func outputCompact(logger *logrus.Logger, entries []entryView, args *Args) {
275+
type compactRow struct {
276+
Title string `json:"title"`
277+
Login string `json:"login"`
278+
Category string `json:"category"`
279+
Label string `json:"label"`
280+
Type string `json:"type"`
281+
Password string `json:"password,omitempty"`
282+
}
283+
284+
rows := make([]compactRow, 0, len(entries))
285+
for _, e := range entries {
286+
anchor := anchorField(e.Fields)
287+
row := compactRow{
288+
Title: e.Title,
289+
Login: e.Subtitle,
290+
Category: e.Category,
291+
}
292+
if anchor != nil {
293+
row.Label = anchor.Label
294+
row.Type = anchor.Type
295+
if anchor.Sensitive {
296+
row.Password = anchor.Value
297+
}
298+
}
299+
rows = append(rows, row)
300+
}
301+
302+
if *args.jsonOutput {
303+
jsonData, err := json.Marshal(rows)
304+
if err != nil {
305+
logger.WithError(err).Fatal("could not marshal JSON data")
306+
}
307+
fmt.Println(string(jsonData))
308+
return
309+
}
310+
for _, r := range rows {
311+
format := "> title: %s login: %s cat.: %s label: %s type: %s"
312+
vals := []any{r.Title, r.Login, r.Category, r.Label, r.Type}
313+
if r.Password != "" {
314+
format += " password: %s"
315+
vals = append(vals, r.Password)
316+
}
317+
logger.Printf(format, vals...)
318+
}
319+
}
320+
321+
// outputDetailed emits the grouped per-field view: one header line per entry
322+
// followed by an indented line per field.
323+
func outputDetailed(logger *logrus.Logger, entries []entryView, args *Args) {
262324
if *args.jsonOutput {
263325
jsonData, err := json.Marshal(entries)
264326
if err != nil {
@@ -296,6 +358,21 @@ func outputEntriesOrLog(logger *logrus.Logger, entries []entryView, args *Args)
296358
}
297359
}
298360

361+
// anchorField picks the field that represents the entry in compact mode.
362+
// Mirrors the original GetEntries dedup: prefer the sensitive (password)
363+
// field, fall back to the first field.
364+
func anchorField(fields []fieldView) *fieldView {
365+
for i := range fields {
366+
if fields[i].Sensitive {
367+
return &fields[i]
368+
}
369+
}
370+
if len(fields) > 0 {
371+
return &fields[0]
372+
}
373+
return nil
374+
}
375+
299376
func copyEntry(logger *logrus.Logger, vault *enpass.Vault, args *Args) {
300377
card, err := vault.GetEntry(*args.cardType, args.filters, true)
301378
if err != nil {

0 commit comments

Comments
 (0)