commit c0b256674bcc78486e2c18417e37c4d1675977cd
parent 072659c0fb185191796e7d60d5fd941e84a9fff2
Author: Sean Enck <sean@ttypty.com>
Date: Thu, 30 Mar 2023 19:03:05 -0400
begin to support json output
Diffstat:
4 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/cmd/vers.txt b/cmd/vers.txt
@@ -1 +1 @@
-6bf2506-1
-\ No newline at end of file
+76d0142-1
+\ No newline at end of file
diff --git a/internal/backend/query.go b/internal/backend/query.go
@@ -3,6 +3,7 @@ package backend
import (
"crypto/sha512"
+ "encoding/json"
"errors"
"fmt"
"sort"
@@ -129,6 +130,14 @@ func (t *Transaction) QueryCallback(args QueryOptions) ([]QueryEntity, error) {
val = e.backing.GetPassword()
}
switch args.Values {
+ case JSONValue:
+ t := getValue(e.backing, modTimeKey)
+ s := JSON{Path: k, ModTime: t}
+ m, err := json.MarshalIndent(s, "", " ")
+ if err != nil {
+ return nil, err
+ }
+ entity.Value = string(m)
case SecretValue:
entity.Value = val
case HashedValue, StatsValue:
diff --git a/internal/backend/query_test.go b/internal/backend/query_test.go
@@ -1,6 +1,7 @@
package backend_test
import (
+ "encoding/json"
"fmt"
"os"
"strings"
@@ -109,6 +110,17 @@ func TestValueModes(t *testing.T) {
if err != nil || !strings.HasPrefix(q.Value, "modtime: ") || len(strings.Split(q.Value, "\n")) != 1 {
t.Errorf("invalid stats: %s", q.Value)
}
+ q, err = fullSetup(t, true).Get("test/test/abc", backend.JSONValue)
+ if err != nil {
+ t.Errorf("no error: %v", err)
+ }
+ m := backend.JSON{}
+ if err := json.Unmarshal([]byte(q.Value), &m); err != nil {
+ t.Errorf("no error: %v", err)
+ }
+ if len(m.ModTime) != 25 || m.Path != "test/test/abc" {
+ t.Errorf("invalid json: %v", m)
+ }
}
func TestQueryCallback(t *testing.T) {
diff --git a/internal/backend/types.go b/internal/backend/types.go
@@ -53,6 +53,11 @@ type (
title string
hook Hook
}
+ // JSON is an entry as a JSON string
+ JSON struct {
+ ModTime string `json:"modtime"`
+ Path string `json:"path"`
+ }
)
const (
@@ -91,6 +96,8 @@ const (
SecretValue
// StatsValue will show the last modification time
StatsValue
+ // JSONValue will show entries as a JSON payload
+ JSONValue
)
const (