Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON Output Option and Refactor Data Preparation #142

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ enp show enpass.com
$ # copy password of 'reddit.com' entry to clipboard
$ enp copy reddit.com

$ # print password of 'github.com' to stdout, useful for scripting
$ # print password of 'github.com' to stdout, useful for scripting
$ password=$(enp pass github.com)
```

Expand All @@ -47,6 +47,7 @@ Flags
| `-type=TYPE` | The type of your card (password, ...) |
| `-log=LEVEL` | The log level from debug (5) to error (1) |
| `-nonInteractive` | Disable prompts and fail instead |
| `-json` | Output as JSON to stdout |
| `-pin` | Enable Quick Unlock using a PIN |
| `-and` | Combines filters with AND instead of default OR |
| `-sort` | Sort the output by title and username of the `list` and `show` command |
Expand Down
88 changes: 57 additions & 31 deletions cmd/enpasscli/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -55,6 +56,7 @@ type Args struct {
cardType *string
keyFilePath *string
logLevelStr *string
jsonOutput *bool
nonInteractive *bool
pinEnable *bool
sort *bool
Expand All @@ -68,6 +70,7 @@ func (args *Args) parse() {
args.cardType = flag.String("type", "password", "The type of your card. (password, ...)")
args.keyFilePath = flag.String("keyfile", "", "Path to your Enpass vault keyfile.")
args.logLevelStr = flag.String("log", defaultLogLevel.String(), "The log level from debug (5) to error (1).")
args.jsonOutput = flag.Bool("json", false, "Output data in JSON format.")
args.nonInteractive = flag.Bool("nonInteractive", false, "Disable prompts and fail instead.")
args.pinEnable = flag.Bool("pin", false, "Enable PIN.")
args.and = flag.Bool("and", false, "Combines filters with AND instead of default OR.")
Expand Down Expand Up @@ -123,21 +126,13 @@ func listEntries(logger *logrus.Logger, vault *enpass.Vault, args *Args) {
if *args.sort {
sortEntries(cards)
}
for _, card := range cards {
if card.IsTrashed() && !*args.trashed {
continue
}
logger.Printf(
"> title: %s"+
" login: %s"+
" cat.: %s"+
" label: %s",
card.Title,
card.Subtitle,
card.Category,
card.Label,
)

data, err := prepareCardData(cards, false, args)
if err != nil {
logger.WithError(err).Fatal(err.Error())
}

outputDataOrLog(logger, data, args)
}

func showEntries(logger *logrus.Logger, vault *enpass.Vault, args *Args) {
Expand All @@ -148,29 +143,60 @@ func showEntries(logger *logrus.Logger, vault *enpass.Vault, args *Args) {
if *args.sort {
sortEntries(cards)
}

data, err := prepareCardData(cards, true, args)
if err != nil {
logger.WithError(err).Fatal(err.Error())
}

outputDataOrLog(logger, data, args)
}

func prepareCardData(cards []enpass.Card, includeDecrypted bool, args *Args) ([]map[string]string, error) {
data := make([]map[string]string, 0)
for _, card := range cards {
if card.IsTrashed() && !*args.trashed {
continue
}
decrypted, err := card.Decrypt()
if err != nil {
logger.WithError(err).Error("could not decrypt " + card.Title)
continue

cardMap := map[string]string{
"title": card.Title,
"login": card.Subtitle,
"category": card.Category,
"label": card.Label,
"type": card.Type,
}

logger.Printf(
"> title: %s"+
" login: %s"+
" cat.: %s"+
" label: %s"+
" %s: %s",
card.Title,
card.Subtitle,
card.Category,
card.Label,
card.Type,
decrypted,
)
if includeDecrypted {
decrypted, err := card.Decrypt()
if err != nil {
return nil, fmt.Errorf("could not decrypt %s: %w", card.Title, err)
}
cardMap["password"] = decrypted
}

data = append(data, cardMap)
}
return data, nil
}

func outputDataOrLog(logger *logrus.Logger, data []map[string]string, args *Args) {
if *args.jsonOutput {
jsonData, jsonErr := json.Marshal(data)
if jsonErr != nil {
logger.WithError(jsonErr).Fatal("could not marshal JSON data")
}
fmt.Println(string(jsonData))
} else {
for _, card := range data {
logger.Printf(
"> title: %s login: %s cat.: %s label: %s",
card["title"],
card["login"],
card["category"],
card["label"],
)
}
}
}

Expand Down
Loading