feat(cli): --json field-select, --jq, auth token, doc --from-url
Output ergonomics:
- `--json` accepts a comma-separated field list (gh-parity); selects
named keys from the per-command payload. Bare `--json` keeps the
full shape.
- `--jq <expr>` evaluates a gojq expression over the JSON; pairs with
`--json field-list` so projection runs before jq.
- `--version` is a global cobra flag in addition to the `version`
subcommand; both render the same line.
- Per-command `--help` now renders the available JSON field list under
"JSON fields available via `--json id,name,...`" (field-discovery
parity with gh / kubectl `-o jsonpath`).
New commands:
- `auth token` — print the active context's credential to stdout for
shell command substitution (`WEKNORA_TOKEN=$(weknora auth token)`).
Default: raw secret, no trailing newline. `--json` emits
`{token, mode, context}`.
- `doc upload --from-url <URL>` — ingest a remote URL via the SDK
`CreateKnowledgeFromURL`. `--name` forwarded as `FileName` so the
server's known-extension heuristic upgrades crawl-mode to
file-download-mode where appropriate.
Includes the simplify post-review polish pass (field-filter unit
tests, --json/--jq compose check, agent_help copy fixes).
This commit is contained in:
@@ -46,7 +46,7 @@ const (
|
||||
CodeNetworkError ErrorCode = "network.error"
|
||||
// CodeSessionCreateFailed marks a chat invocation where the auto-created
|
||||
// session POST failed. Surfaced as a typed code distinct from generic
|
||||
// server.error so agents can retry with their own --session-id.
|
||||
// server.error so agents can retry with their own --session.
|
||||
CodeSessionCreateFailed ErrorCode = "server.session_create_failed"
|
||||
|
||||
// local.* — config / file / keychain on the user's machine
|
||||
|
||||
@@ -195,7 +195,7 @@ func defaultHint(code ErrorCode) string {
|
||||
case CodeSSEStreamAborted:
|
||||
return "the streaming answer was cut off mid-flight; retry, or pass --no-stream to buffer the full response"
|
||||
case CodeSessionCreateFailed:
|
||||
return "could not create a chat session; pass --session-id to reuse an existing session"
|
||||
return "could not create a chat session; pass --session to reuse an existing session"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ func buildClient(f *Factory) (*sdk.Client, error) {
|
||||
// authenticated invocation.
|
||||
var accessToken string
|
||||
if ctx.TokenRef != "" {
|
||||
if access, err := loadSecret(store, ctxName, "access"); err != nil {
|
||||
if access, err := LoadSecret(store, ctxName, "access"); err != nil {
|
||||
return nil, err
|
||||
} else if access != "" {
|
||||
accessToken = access
|
||||
@@ -147,7 +147,7 @@ func buildClient(f *Factory) (*sdk.Client, error) {
|
||||
}
|
||||
}
|
||||
if ctx.APIKeyRef != "" {
|
||||
if apiKey, err := loadSecret(store, ctxName, "api_key"); err != nil {
|
||||
if apiKey, err := LoadSecret(store, ctxName, "api_key"); err != nil {
|
||||
return nil, err
|
||||
} else if apiKey != "" {
|
||||
opts = append(opts, sdk.WithAPIKey(apiKey))
|
||||
@@ -213,9 +213,12 @@ func (f *Factory) ResolveKB(cmd *cobra.Command) (string, error) {
|
||||
return "", NewError(CodeKBIDRequired, "kb is required")
|
||||
}
|
||||
|
||||
// loadSecret returns the stored value for (context, key); ErrNotFound becomes
|
||||
// ("", nil) so callers can treat "not configured" as success.
|
||||
func loadSecret(store secrets.Store, context, key string) (string, error) {
|
||||
// LoadSecret fetches a named secret for the given context from the keyring.
|
||||
// Returns ("", nil) when the secret is absent (ErrNotFound); a real keyring
|
||||
// access failure surfaces as CodeLocalKeychainDenied. Used by buildClient
|
||||
// to assemble SDK auth options and by `auth token` to expose the raw
|
||||
// credential for shell scripting.
|
||||
func LoadSecret(store secrets.Store, context, key string) (string, error) {
|
||||
v, err := store.Get(context, key)
|
||||
if errors.Is(err, secrets.ErrNotFound) {
|
||||
return "", nil
|
||||
|
||||
@@ -21,19 +21,31 @@ func NormalizeHost(host string) (string, error) {
|
||||
if host == "" {
|
||||
return "", NewError(CodeInputInvalidArgument, "--host must not be empty")
|
||||
}
|
||||
u, err := url.Parse(host)
|
||||
if err := ValidateHTTPURL("--host", host); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return host, nil
|
||||
}
|
||||
|
||||
// ValidateHTTPURL applies the same http/https URL rules NormalizeHost uses,
|
||||
// but parameterized by flagLabel so error messages name the caller's flag
|
||||
// (e.g. "--host", "--from-url"). Returns CodeInputInvalidArgument on any
|
||||
// failure. Empty `value` is not pre-checked here — callers that distinguish
|
||||
// "missing" from "malformed" should test for empty before calling.
|
||||
func ValidateHTTPURL(flagLabel, value string) error {
|
||||
u, err := url.Parse(value)
|
||||
if err != nil {
|
||||
return "", NewError(CodeInputInvalidArgument, fmt.Sprintf("--host %q is not a valid URL: %v", host, err))
|
||||
return NewError(CodeInputInvalidArgument, fmt.Sprintf("%s %q is not a valid URL: %v", flagLabel, value, err))
|
||||
}
|
||||
if u.Scheme != "http" && u.Scheme != "https" {
|
||||
return "", &Error{
|
||||
return &Error{
|
||||
Code: CodeInputInvalidArgument,
|
||||
Message: fmt.Sprintf("--host scheme must be http or https, got %q", u.Scheme),
|
||||
Message: fmt.Sprintf("%s scheme must be http or https, got %q", flagLabel, u.Scheme),
|
||||
Hint: "example: https://kb.example.com",
|
||||
}
|
||||
}
|
||||
if u.Host == "" {
|
||||
return "", NewError(CodeInputInvalidArgument, fmt.Sprintf("--host %q is missing the host portion", host))
|
||||
return NewError(CodeInputInvalidArgument, fmt.Sprintf("%s %q is missing the host portion", flagLabel, value))
|
||||
}
|
||||
return host, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package cmdutil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
// JSONOptions captures the resolved --json + --jq state after CheckJSONFlags.
|
||||
// A non-nil value means the user requested JSON output; Fields restricts
|
||||
// data.items[*] when len(Fields) > 0; JQ is a jq expression applied to the
|
||||
// final envelope JSON.
|
||||
type JSONOptions struct {
|
||||
Fields []string
|
||||
JQ string
|
||||
}
|
||||
|
||||
// Enabled reports whether the caller asked for JSON output. Convenience
|
||||
// shorthand for `opts != nil`.
|
||||
func (o *JSONOptions) Enabled() bool { return o != nil }
|
||||
|
||||
// jsonNoOptSentinel marks a bare `--json` (no comma-separated values
|
||||
// after it). pflag's NoOptDefVal mechanism stores this sentinel into the
|
||||
// slice; CheckJSONFlags then maps it to "no field filter" (full envelope).
|
||||
//
|
||||
// This is a documented divergence from gh CLI, where bare `--json` errors
|
||||
// with a field-list discovery prompt. weknora's envelope is itself the
|
||||
// machine-readable contract (carries typed error.code / _meta / risk),
|
||||
// so `--json` bare always producing the full envelope keeps v0.3 caller
|
||||
// scripts and the typical agent pipe-to-jq pattern working. Field
|
||||
// discovery moves to per-command `--help` "JSON fields:" sections.
|
||||
const jsonNoOptSentinel = "\x00json-no-value"
|
||||
|
||||
// AddJSONFlags registers --json and --jq on cmd.
|
||||
//
|
||||
// - `--json` → full envelope (no field filter)
|
||||
// - `--json id,name` → envelope with data.items[*] / data restricted
|
||||
// to listed fields
|
||||
// - `--jq <expr>` → applies a jq expression after marshaling;
|
||||
// requires --json to be set explicitly
|
||||
//
|
||||
// `fields` is the set of available fields the user may pass; rendered in
|
||||
// the command's help. Pass nil to skip the help annotation (uncommon).
|
||||
func AddJSONFlags(cmd *cobra.Command, fields []string) {
|
||||
f := cmd.Flags()
|
||||
f.StringSlice("json", nil, "Output JSON envelope; bare `--json` for full, `--json=id,name` for filtered fields")
|
||||
f.Lookup("json").NoOptDefVal = jsonNoOptSentinel
|
||||
f.StringP("jq", "q", "", "Filter JSON output using a jq `expression` (requires --json)")
|
||||
|
||||
if len(fields) > 0 {
|
||||
sorted := append([]string(nil), fields...)
|
||||
sort.Strings(sorted)
|
||||
// Append to Long without overwriting per-command prose.
|
||||
hdr := "\n\nJSON fields available via `--json id,name,...`:\n " +
|
||||
strings.Join(sorted, "\n ")
|
||||
if cmd.Long != "" {
|
||||
cmd.Long += hdr
|
||||
} else {
|
||||
cmd.Long = strings.TrimSpace(cmd.Short) + hdr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CheckJSONFlags resolves the --json + --jq state from cmd. Returns:
|
||||
// - (nil, nil) neither flag set (human output mode)
|
||||
// - (*JSONOptions, nil) --json set (possibly with --jq)
|
||||
// - (nil, error) --jq without --json (plain error, exit 1)
|
||||
//
|
||||
// Bare `--json` yields Fields == nil (full envelope). Explicit field list
|
||||
// yields Fields == []string{"id", "name", ...} (filter applied).
|
||||
//
|
||||
// gh divergence: gh errors on bare --json with a discovery prompt. weknora
|
||||
// treats bare --json as "full envelope, no filter" because the envelope is
|
||||
// the contract.
|
||||
func CheckJSONFlags(cmd *cobra.Command) (*JSONOptions, error) {
|
||||
f := cmd.Flags()
|
||||
jsonFlag := f.Lookup("json")
|
||||
jqFlag := f.Lookup("jq")
|
||||
if jsonFlag == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if jsonFlag.Changed {
|
||||
sv, ok := jsonFlag.Value.(pflag.SliceValue)
|
||||
if !ok {
|
||||
return nil, errors.New("internal: --json flag is not a StringSlice")
|
||||
}
|
||||
raw := sv.GetSlice()
|
||||
opts := &JSONOptions{}
|
||||
// Strip the bare-flag sentinel if present.
|
||||
for _, v := range raw {
|
||||
if v == jsonNoOptSentinel {
|
||||
continue
|
||||
}
|
||||
opts.Fields = append(opts.Fields, v)
|
||||
}
|
||||
if jqFlag != nil {
|
||||
opts.JQ = jqFlag.Value.String()
|
||||
}
|
||||
return opts, nil
|
||||
}
|
||||
if jqFlag != nil && jqFlag.Changed {
|
||||
// gh parity: plain error, exit 1 (not FlagError → exit 2).
|
||||
return nil, errors.New("cannot use `--jq` without specifying `--json`")
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package cmdutil_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
||||
)
|
||||
|
||||
var testFields = []string{"id", "name", "kb_id", "updated_at"}
|
||||
|
||||
func newTestCmd(t *testing.T, captured **cmdutil.JSONOptions) *cobra.Command {
|
||||
t.Helper()
|
||||
cmd := &cobra.Command{
|
||||
Use: "test",
|
||||
SilenceErrors: true,
|
||||
SilenceUsage: true,
|
||||
RunE: func(c *cobra.Command, args []string) error {
|
||||
opts, err := cmdutil.CheckJSONFlags(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*captured = opts
|
||||
return nil
|
||||
},
|
||||
}
|
||||
cmdutil.AddJSONFlags(cmd, testFields)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func TestAddJSONFlags_BareYieldsFullEnvelopeOpts(t *testing.T) {
|
||||
// `--json` bare → Enabled() with empty Fields → caller emits full envelope.
|
||||
var captured *cmdutil.JSONOptions
|
||||
cmd := newTestCmd(t, &captured)
|
||||
cmd.SetArgs([]string{"--json"})
|
||||
cmd.SetOut(&bytes.Buffer{})
|
||||
cmd.SetErr(&bytes.Buffer{})
|
||||
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatalf("Execute() err = %v", err)
|
||||
}
|
||||
if !captured.Enabled() {
|
||||
t.Fatalf("expected Enabled, got nil")
|
||||
}
|
||||
if len(captured.Fields) != 0 {
|
||||
t.Errorf("bare --json must produce empty Fields, got %v", captured.Fields)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddJSONFlags_FieldsFlagParsing(t *testing.T) {
|
||||
// NoOptDefVal sentinel means the `=` form is required for value passing.
|
||||
// Space form `--json id,name` parses as bare + positional, which is
|
||||
// documented divergence from gh CLI to keep bare-envelope semantics.
|
||||
cases := []struct {
|
||||
args []string
|
||||
want []string
|
||||
}{
|
||||
{[]string{"--json=id,name"}, []string{"id", "name"}},
|
||||
{[]string{"--json=id,name,kb_id"}, []string{"id", "name", "kb_id"}},
|
||||
{[]string{"--json=id"}, []string{"id"}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
|
||||
var captured *cmdutil.JSONOptions
|
||||
cmd := newTestCmd(t, &captured)
|
||||
cmd.SetArgs(tc.args)
|
||||
cmd.SetOut(&bytes.Buffer{})
|
||||
cmd.SetErr(&bytes.Buffer{})
|
||||
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatalf("Execute() err = %v", err)
|
||||
}
|
||||
if captured == nil {
|
||||
t.Fatalf("expected JSONOptions captured, got nil")
|
||||
}
|
||||
if !equalSlice(captured.Fields, tc.want) {
|
||||
t.Errorf("Fields = %v, want %v", captured.Fields, tc.want)
|
||||
}
|
||||
if captured.JQ != "" {
|
||||
t.Errorf("JQ should be empty, got %q", captured.JQ)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddJSONFlags_NoFlagsSet(t *testing.T) {
|
||||
var captured *cmdutil.JSONOptions
|
||||
cmd := newTestCmd(t, &captured)
|
||||
cmd.SetArgs([]string{})
|
||||
cmd.SetOut(&bytes.Buffer{})
|
||||
cmd.SetErr(&bytes.Buffer{})
|
||||
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatalf("Execute() err = %v", err)
|
||||
}
|
||||
if captured != nil {
|
||||
t.Errorf("expected nil JSONOptions for human mode, got %+v", captured)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddJSONFlags_JQWithoutJSON(t *testing.T) {
|
||||
var captured *cmdutil.JSONOptions
|
||||
cmd := newTestCmd(t, &captured)
|
||||
cmd.SetArgs([]string{"--jq", ".data.items[]"})
|
||||
cmd.SetOut(&bytes.Buffer{})
|
||||
cmd.SetErr(&bytes.Buffer{})
|
||||
|
||||
err := cmd.Execute()
|
||||
if err == nil {
|
||||
t.Fatalf("expected mutual-dep error, got nil")
|
||||
}
|
||||
want := "cannot use `--jq` without specifying `--json`"
|
||||
if err.Error() != want {
|
||||
t.Errorf("wrong message.\nwant: %q\ngot: %q", want, err.Error())
|
||||
}
|
||||
// Must NOT be a FlagError — gh emits this as a plain error so exit
|
||||
// code stays 1, not 2.
|
||||
var fe *cmdutil.FlagError
|
||||
if errors.As(err, &fe) {
|
||||
t.Errorf("error should not be FlagError; gh treats this as exit 1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddJSONFlags_JQWithJSON(t *testing.T) {
|
||||
var captured *cmdutil.JSONOptions
|
||||
cmd := newTestCmd(t, &captured)
|
||||
cmd.SetArgs([]string{"--json=id,name", "--jq", ".data.items[0].id"})
|
||||
cmd.SetOut(&bytes.Buffer{})
|
||||
cmd.SetErr(&bytes.Buffer{})
|
||||
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatalf("Execute() err = %v", err)
|
||||
}
|
||||
if captured == nil {
|
||||
t.Fatalf("expected JSONOptions, got nil")
|
||||
}
|
||||
if !equalSlice(captured.Fields, []string{"id", "name"}) {
|
||||
t.Errorf("Fields = %v, want [id name]", captured.Fields)
|
||||
}
|
||||
if captured.JQ != ".data.items[0].id" {
|
||||
t.Errorf("JQ = %q, want %q", captured.JQ, ".data.items[0].id")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddJSONFlags_JQShortFlag(t *testing.T) {
|
||||
// gh uses -q as shorthand for --jq.
|
||||
var captured *cmdutil.JSONOptions
|
||||
cmd := newTestCmd(t, &captured)
|
||||
cmd.SetArgs([]string{"--json=id", "-q", ".data"})
|
||||
cmd.SetOut(&bytes.Buffer{})
|
||||
cmd.SetErr(&bytes.Buffer{})
|
||||
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatalf("Execute() err = %v", err)
|
||||
}
|
||||
if captured == nil || captured.JQ != ".data" {
|
||||
t.Errorf("expected JQ=.data, got %+v", captured)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddJSONFlags_HelpListsFields(t *testing.T) {
|
||||
cmd := &cobra.Command{Use: "test", Short: "A test command"}
|
||||
cmdutil.AddJSONFlags(cmd, []string{"name", "id", "updated_at"})
|
||||
if !strings.Contains(cmd.Long, "JSON fields available") {
|
||||
t.Errorf("expected help to include 'JSON fields available'; got Long=%q", cmd.Long)
|
||||
}
|
||||
// Fields should appear alphabetically sorted.
|
||||
idAt := strings.Index(cmd.Long, "id")
|
||||
nameAt := strings.Index(cmd.Long, "name")
|
||||
updatedAt := strings.Index(cmd.Long, "updated_at")
|
||||
if !(idAt < nameAt && nameAt < updatedAt) {
|
||||
t.Errorf("fields not sorted in Long: idAt=%d nameAt=%d updatedAt=%d", idAt, nameAt, updatedAt)
|
||||
}
|
||||
}
|
||||
|
||||
func equalSlice(a, b []string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/itchyny/gojq"
|
||||
)
|
||||
|
||||
// WriteEnvelopeFiltered serializes env to w, optionally restricting
|
||||
// data.items[*] (for list envelopes) or data (for single-resource envelopes)
|
||||
// to the named fields, then applying a jq expression on the result.
|
||||
//
|
||||
// - len(fields) == 0 → no field filter (full envelope)
|
||||
// - jqExpr == "" → no jq filter (just write the envelope)
|
||||
//
|
||||
// The envelope structure (ok / data / error / _meta / risk / dry_run / _notice)
|
||||
// is preserved across field filtering — only Data is rewritten. jq operates
|
||||
// on the entire envelope JSON so users can `--jq '.data.items[].id'`.
|
||||
func WriteEnvelopeFiltered(w io.Writer, env Envelope, fields []string, jqExpr string) error {
|
||||
raw, err := marshalEnvelope(env)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(fields) > 0 {
|
||||
raw, err = applyFieldFilter(raw, fields)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if jqExpr != "" {
|
||||
return writeJQ(w, raw, jqExpr)
|
||||
}
|
||||
_, err = w.Write(raw)
|
||||
return err
|
||||
}
|
||||
|
||||
// marshalEnvelope returns the canonical newline-terminated envelope JSON used
|
||||
// by the non-filtered path. Identical to WriteEnvelope but as bytes.
|
||||
func marshalEnvelope(env Envelope) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
enc := json.NewEncoder(&buf)
|
||||
enc.SetEscapeHTML(false)
|
||||
if err := enc.Encode(env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// applyFieldFilter rewrites envelope.data so that nested objects keep only
|
||||
// the named keys. The shape rules:
|
||||
//
|
||||
// - data is an object with `items` (the standard list envelope shape):
|
||||
// filter each items[*] to the named fields.
|
||||
// - data is an object without `items` (single-resource envelope):
|
||||
// filter data itself.
|
||||
// - data is an array (uncommon — currently no command produces this, but
|
||||
// defensive): filter each [*].
|
||||
// - data is nil / scalar: unchanged.
|
||||
//
|
||||
// Unknown field names are silently ignored (consistent with gh — users may
|
||||
// pass an aspirational field set across heterogenous list outputs).
|
||||
func applyFieldFilter(envelopeJSON []byte, fields []string) ([]byte, error) {
|
||||
var raw map[string]json.RawMessage
|
||||
if err := json.Unmarshal(envelopeJSON, &raw); err != nil {
|
||||
return nil, fmt.Errorf("field filter: parse envelope: %w", err)
|
||||
}
|
||||
dataRaw, ok := raw["data"]
|
||||
if !ok || len(dataRaw) == 0 || string(dataRaw) == "null" {
|
||||
return envelopeJSON, nil
|
||||
}
|
||||
|
||||
filtered, err := filterDataPayload(dataRaw, fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
raw["data"] = filtered
|
||||
|
||||
var buf bytes.Buffer
|
||||
enc := json.NewEncoder(&buf)
|
||||
enc.SetEscapeHTML(false)
|
||||
if err := enc.Encode(raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// filterDataPayload dispatches on the shape of the data JSON value.
|
||||
func filterDataPayload(dataRaw json.RawMessage, fields []string) (json.RawMessage, error) {
|
||||
trimmed := bytes.TrimSpace(dataRaw)
|
||||
if len(trimmed) == 0 {
|
||||
return dataRaw, nil
|
||||
}
|
||||
switch trimmed[0] {
|
||||
case '{':
|
||||
return filterObjectData(dataRaw, fields)
|
||||
case '[':
|
||||
return filterArrayItems(dataRaw, fields)
|
||||
default:
|
||||
// scalar (number / string / bool / null) — nothing to filter
|
||||
return dataRaw, nil
|
||||
}
|
||||
}
|
||||
|
||||
// filterObjectData filters either data.items[*] (list shape) or data (single
|
||||
// resource).
|
||||
func filterObjectData(dataRaw json.RawMessage, fields []string) (json.RawMessage, error) {
|
||||
var obj map[string]json.RawMessage
|
||||
if err := json.Unmarshal(dataRaw, &obj); err != nil {
|
||||
return nil, fmt.Errorf("field filter: parse data object: %w", err)
|
||||
}
|
||||
if items, ok := obj["items"]; ok {
|
||||
filtered, err := filterArrayItems(items, fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
obj["items"] = filtered
|
||||
return json.Marshal(obj)
|
||||
}
|
||||
// Single-resource envelope: filter the data object itself.
|
||||
return filterObjectKeys(dataRaw, fields)
|
||||
}
|
||||
|
||||
// filterArrayItems applies filterObjectKeys to each element of an array.
|
||||
// Non-object elements (e.g. an array of strings) are passed through.
|
||||
func filterArrayItems(arrayRaw json.RawMessage, fields []string) (json.RawMessage, error) {
|
||||
var items []json.RawMessage
|
||||
if err := json.Unmarshal(arrayRaw, &items); err != nil {
|
||||
return nil, fmt.Errorf("field filter: parse data items: %w", err)
|
||||
}
|
||||
for i, item := range items {
|
||||
trimmed := bytes.TrimSpace(item)
|
||||
if len(trimmed) == 0 || trimmed[0] != '{' {
|
||||
continue
|
||||
}
|
||||
filtered, err := filterObjectKeys(item, fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items[i] = filtered
|
||||
}
|
||||
return json.Marshal(items)
|
||||
}
|
||||
|
||||
// filterObjectKeys produces a new object containing only the listed keys
|
||||
// that were present in the source.
|
||||
func filterObjectKeys(objRaw json.RawMessage, fields []string) (json.RawMessage, error) {
|
||||
var src map[string]json.RawMessage
|
||||
if err := json.Unmarshal(objRaw, &src); err != nil {
|
||||
return nil, fmt.Errorf("field filter: parse object keys: %w", err)
|
||||
}
|
||||
dst := make(map[string]json.RawMessage, len(fields))
|
||||
for _, k := range fields {
|
||||
if v, ok := src[k]; ok {
|
||||
dst[k] = v
|
||||
}
|
||||
}
|
||||
return json.Marshal(dst)
|
||||
}
|
||||
|
||||
// writeJQ evaluates expr against envelopeJSON and writes each result line by
|
||||
// line to w. Mirrors gh's pkg/export/filter.go semantics: results that are
|
||||
// strings render without quotes; everything else uses encoding/json.
|
||||
//
|
||||
// Returns input.invalid_argument-shaped errors via plain errors.New + fmt;
|
||||
// the caller is responsible for wrapping with cmdutil.NewError if it wants
|
||||
// the typed envelope code.
|
||||
func writeJQ(w io.Writer, envelopeJSON []byte, expr string) error {
|
||||
query, err := gojq.Parse(expr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("jq parse: %w", err)
|
||||
}
|
||||
var input any
|
||||
if err := json.Unmarshal(envelopeJSON, &input); err != nil {
|
||||
return fmt.Errorf("jq input parse: %w", err)
|
||||
}
|
||||
iter := query.Run(input)
|
||||
for {
|
||||
v, ok := iter.Next()
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
if e, ok := v.(error); ok {
|
||||
return fmt.Errorf("jq eval: %w", e)
|
||||
}
|
||||
if s, ok := v.(string); ok {
|
||||
if _, err := fmt.Fprintln(w, s); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
out, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := w.Write(append(out, '\n')); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package format_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/cli/internal/format"
|
||||
)
|
||||
|
||||
func TestWriteEnvelopeFiltered_NoFilter(t *testing.T) {
|
||||
env := format.Success(map[string]any{"items": []any{
|
||||
map[string]any{"id": "1", "name": "alpha"},
|
||||
}}, &format.Meta{KBID: "kb_x"})
|
||||
buf := &bytes.Buffer{}
|
||||
if err := format.WriteEnvelopeFiltered(buf, env, nil, ""); err != nil {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
var got map[string]any
|
||||
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
if got["ok"] != true {
|
||||
t.Errorf("ok = %v, want true", got["ok"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteEnvelopeFiltered_FieldsOnListItems(t *testing.T) {
|
||||
env := format.Success(map[string]any{"items": []any{
|
||||
map[string]any{"id": "1", "name": "alpha", "kb_id": "kb_x", "updated_at": "2026-01-01"},
|
||||
map[string]any{"id": "2", "name": "beta", "kb_id": "kb_x", "updated_at": "2026-01-02"},
|
||||
}}, nil)
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
if err := format.WriteEnvelopeFiltered(buf, env, []string{"id", "name"}, ""); err != nil {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
|
||||
var got struct {
|
||||
OK bool `json:"ok"`
|
||||
Data struct {
|
||||
Items []map[string]string `json:"items"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
||||
t.Fatalf("parse: %v\n%s", err, buf.String())
|
||||
}
|
||||
if len(got.Data.Items) != 2 {
|
||||
t.Fatalf("items len = %d, want 2", len(got.Data.Items))
|
||||
}
|
||||
for i, item := range got.Data.Items {
|
||||
if _, has := item["kb_id"]; has {
|
||||
t.Errorf("item[%d] should not have kb_id: %v", i, item)
|
||||
}
|
||||
if _, has := item["updated_at"]; has {
|
||||
t.Errorf("item[%d] should not have updated_at: %v", i, item)
|
||||
}
|
||||
if item["id"] == "" || item["name"] == "" {
|
||||
t.Errorf("item[%d] missing required keys: %v", i, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteEnvelopeFiltered_FieldsOnSingleObject(t *testing.T) {
|
||||
env := format.Success(map[string]any{
|
||||
"id": "kb_x",
|
||||
"name": "Engineering",
|
||||
"owner": "alice",
|
||||
}, nil)
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
if err := format.WriteEnvelopeFiltered(buf, env, []string{"id", "name"}, ""); err != nil {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
|
||||
var got struct {
|
||||
Data map[string]string `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
||||
t.Fatalf("parse: %v\n%s", err, buf.String())
|
||||
}
|
||||
if _, has := got.Data["owner"]; has {
|
||||
t.Errorf("should not have owner: %v", got.Data)
|
||||
}
|
||||
if got.Data["id"] != "kb_x" || got.Data["name"] != "Engineering" {
|
||||
t.Errorf("missing kept fields: %v", got.Data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteEnvelopeFiltered_UnknownFieldSilent(t *testing.T) {
|
||||
env := format.Success(map[string]any{"id": "1"}, nil)
|
||||
buf := &bytes.Buffer{}
|
||||
if err := format.WriteEnvelopeFiltered(buf, env, []string{"id", "nonexistent"}, ""); err != nil {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
var got struct {
|
||||
Data map[string]string `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
if got.Data["id"] != "1" {
|
||||
t.Errorf("id missing: %v", got.Data)
|
||||
}
|
||||
if _, has := got.Data["nonexistent"]; has {
|
||||
t.Errorf("nonexistent should be silently dropped: %v", got.Data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteEnvelopeFiltered_PreservesEnvelopeFields(t *testing.T) {
|
||||
// Even with field filter, meta/risk/dry_run/error must be preserved.
|
||||
env := format.Success(map[string]any{"items": []any{
|
||||
map[string]any{"id": "1", "name": "x", "kb_id": "kb"},
|
||||
}}, &format.Meta{KBID: "kb_x", RequestID: "req_123"})
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
if err := format.WriteEnvelopeFiltered(buf, env, []string{"id"}, ""); err != nil {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
var got struct {
|
||||
OK bool `json:"ok"`
|
||||
Meta *format.Meta `json:"_meta"`
|
||||
Data struct {
|
||||
Items []map[string]any `json:"items"`
|
||||
} `json:"data"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
}
|
||||
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
||||
t.Fatalf("parse: %v\n%s", err, buf.String())
|
||||
}
|
||||
if got.Meta == nil || got.Meta.RequestID != "req_123" {
|
||||
t.Errorf("_meta lost or mangled: %+v", got.Meta)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteEnvelopeFiltered_JQOnly(t *testing.T) {
|
||||
env := format.Success(map[string]any{"items": []any{
|
||||
map[string]any{"id": "1", "name": "alpha"},
|
||||
map[string]any{"id": "2", "name": "beta"},
|
||||
}}, nil)
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
if err := format.WriteEnvelopeFiltered(buf, env, nil, ".data.items[].id"); err != nil {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
// gh CLI parity: string results render without JSON quotes
|
||||
// (see https://github.com/cli/cli/blob/trunk/pkg/export/filter.go).
|
||||
lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")
|
||||
if len(lines) != 2 {
|
||||
t.Fatalf("expected 2 lines, got %d: %q", len(lines), buf.String())
|
||||
}
|
||||
if lines[0] != "1" || lines[1] != "2" {
|
||||
t.Errorf("wrong output: %q", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteEnvelopeFiltered_FieldsAndJQ(t *testing.T) {
|
||||
env := format.Success(map[string]any{"items": []any{
|
||||
map[string]any{"id": "1", "name": "alpha", "kb_id": "kb"},
|
||||
map[string]any{"id": "2", "name": "beta", "kb_id": "kb"},
|
||||
}}, nil)
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
if err := format.WriteEnvelopeFiltered(buf, env, []string{"id", "name"}, ".data.items | length"); err != nil {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
out := strings.TrimSpace(buf.String())
|
||||
if out != "2" {
|
||||
t.Errorf("expected '2', got %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteEnvelopeFiltered_JQParseError(t *testing.T) {
|
||||
env := format.Success(map[string]any{"x": 1}, nil)
|
||||
buf := &bytes.Buffer{}
|
||||
err := format.WriteEnvelopeFiltered(buf, env, nil, ".[invalid jq")
|
||||
if err == nil {
|
||||
t.Fatal("expected parse error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "jq parse") {
|
||||
t.Errorf("expected 'jq parse' in error, got %q", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteEnvelopeFiltered_ScalarDataPassThrough(t *testing.T) {
|
||||
// Defensive: scalar data should not crash field filter.
|
||||
env := format.Success("just a string", nil)
|
||||
buf := &bytes.Buffer{}
|
||||
if err := format.WriteEnvelopeFiltered(buf, env, []string{"id"}, ""); err != nil {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
if !strings.Contains(buf.String(), `"just a string"`) {
|
||||
t.Errorf("scalar data lost: %s", buf.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user