Files
Enterprise-Knowledge-Base/cli/cmd/kb/pin.go
T
nullkey 0e081aec5c feat(cli): --log-level + kb/agent status & check + cross-cutting refactor
Operability surface and the bulk of the jopts→fopts migration:

* --log-level error|warn|info|debug + WEKNORA_LOG_LEVEL env, wired to
  the SDK via client.SetDebugLevel. Invalid --log-level returns FlagError
  (exit 2).
* kb status <kb-id> / kb check <kb-id> verb split (1 HTTP vs 1+N for
  failed_count aggregation).
* agent status <agent-id> / agent check <agent-id> verb split
  (probes kb_scope_all_reachable via 1+N HTTP).
* kb create <name> positional (matches agent create).
* Positional id help strings namespaced (<kb-id> / <agent-id>).
* All auth / context / link / doctor / kb / agent CRUD commands migrated
  to the FormatOptions API.
* root.go Execute(ctx) takes a context so signal-cancellation propagates
  via cmd.Context() into long-running commands.
* Pagination termination uses len(accum) >= total (not page*pageSize)
  so server-capped page sizes do not truncate aggregations.
2026-05-18 11:10:19 +08:00

102 lines
3.1 KiB
Go

package kb
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
"github.com/Tencent/WeKnora/cli/internal/iostreams"
sdk "github.com/Tencent/WeKnora/client"
)
// kbPinFields enumerates the fields surfaced for `--format json` discovery on
// `kb pin` / `kb unpin`. The toggle result is the KnowledgeBase; the user-
// relevant fields here are the id and the new pin state.
var kbPinFields = []string{"id", "is_pinned"}
type PinOptions struct{}
// PinService is the narrow SDK surface this command depends on. The CLI
// reads current state before toggling so `pin`/`unpin` are idempotent -
// the server endpoint is only a non-idempotent toggle.
type PinService interface {
GetKnowledgeBase(ctx context.Context, id string) (*sdk.KnowledgeBase, error)
TogglePinKnowledgeBase(ctx context.Context, id string) (*sdk.KnowledgeBase, error)
}
// NewCmdPin builds `weknora kb pin <id>`.
func NewCmdPin(f *cmdutil.Factory) *cobra.Command {
return newPinCmd(f, "pin", true, "Pin a knowledge base to the top of the list")
}
// NewCmdUnpin builds `weknora kb unpin <id>`.
func NewCmdUnpin(f *cmdutil.Factory) *cobra.Command {
return newPinCmd(f, "unpin", false, "Unpin a knowledge base")
}
func newPinCmd(f *cmdutil.Factory, use string, want bool, short string) *cobra.Command {
opts := &PinOptions{}
cmd := &cobra.Command{
Use: use + " <kb-id>",
Short: short,
Long: short + ". Idempotent: reads the current pin state and toggles only if different, so re-running on a KB already in the target state is a no-op.",
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
fopts, err := cmdutil.CheckFormatFlag(c)
if err != nil {
return err
}
fopts.ResolveDefault(iostreams.IO.IsStdoutTTY())
cli, err := f.Client()
if err != nil {
return err
}
return runPin(c.Context(), opts, fopts, cli, args[0], want)
},
}
cmdutil.AddFormatFlag(cmd, kbPinFields...)
return cmd
}
func runPin(ctx context.Context, opts *PinOptions, fopts *cmdutil.FormatOptions, svc PinService, id string, want bool) error {
verb := "pin"
if !want {
verb = "unpin"
}
current, err := svc.GetKnowledgeBase(ctx, id)
if err != nil {
return cmdutil.WrapHTTP(err, "get knowledge base %s", id)
}
if current.IsPinned == want {
state := "pinned"
if !want {
state = "unpinned"
}
// No-op path: the resource is already in the requested state. We
// emit the current resource so callers see the canonical shape on
// both fresh-toggle and no-op paths. Human path prints a confirming
// line; agents observe via the unchanged is_pinned field.
if fopts.WantsJSON() {
return fopts.Emit(iostreams.IO.Out, current)
}
fmt.Fprintf(iostreams.IO.Out, "✓ %s is already %s\n", id, state)
return nil
}
updated, err := svc.TogglePinKnowledgeBase(ctx, id)
if err != nil {
return cmdutil.WrapHTTP(err, "%s knowledge base %s", verb, id)
}
if fopts.WantsJSON() {
return fopts.Emit(iostreams.IO.Out, updated)
}
state := "pinned"
if !updated.IsPinned {
state = "unpinned"
}
fmt.Fprintf(iostreams.IO.Out, "✓ %s %s\n", id, state)
return nil
}