Files
Enterprise-Knowledge-Base/cli/internal/cmdutil/confirm.go
T
nullkey 8a0674186e feat(cli): add kb create/delete and doc list/upload/delete commands
Add the resource-management surface to the v0.2 CLI:

  - weknora kb create --name X [--description Y] [--embedding-model Z]
  - weknora kb delete <id> [--force]
  - weknora doc list [--kb-id X | --kb NAME] [--page N] [--page-size M]
  - weknora doc upload <file> [--kb-id X | --kb NAME] [--name custom]
  - weknora doc delete <id> [--force]

doc/* uses Factory.ResolveKB so the cwd's project link is honored when
--kb-id is omitted. doc upload validates path existence with os.Stat
(rejects directories; follows symlinks to mirror SDK os.Open behavior).
doc list sorts by updated_at desc so newer items surface first.

Both delete commands route through cmdutil.ConfirmDestructive — the
"destructive op needs explicit user opt-in" pattern was about to be
copy-pasted across the new subtree, so it was extracted with the delete
commands as their first consumer. Saves the same dedup pass when v0.3
adds session/agent delete. (PR-12 later renames the flag from --force
to the global -y/--yes for gh-style consistency.)

iostreams.SetForTestWithTTY pairs with the existing SetForTest helper:
the latter never reports stdout as a TTY (singleton replacement uses an
in-memory buffer), so the confirm-yes / confirm-no test branches need a
TTY-on variant.
2026-05-12 13:20:42 +08:00

36 lines
1.2 KiB
Go

package cmdutil
import (
"fmt"
"github.com/Tencent/WeKnora/cli/internal/iostreams"
"github.com/Tencent/WeKnora/cli/internal/prompt"
)
// ConfirmDestructive prompts the user to confirm a destructive operation
// (e.g. delete). Returns nil if the operation should proceed, or a typed
// error otherwise:
// - CodeInputMissingFlag when the prompter rejects (non-TTY / agent mode):
// the caller forgot --force.
// - CodeUserAborted when the user explicitly answers no — also writes
// "Aborted." to stderr so the rejection is visible above the envelope.
//
// Skipped entirely when force is true, or when stdout is non-TTY, or when
// the caller is going to emit a JSON envelope (jsonOut=true) — envelope
// mode is by definition scripted, so a confirmation prompt would deadlock
// the consumer.
func ConfirmDestructive(p prompt.Prompter, force, jsonOut bool, what, id string) error {
if force || !iostreams.IO.IsStdoutTTY() || jsonOut {
return nil
}
ok, err := p.Confirm(fmt.Sprintf("Delete %s %s? This cannot be undone.", what, id), false)
if err != nil {
return Wrapf(CodeInputMissingFlag, err, "confirm delete")
}
if !ok {
fmt.Fprintln(iostreams.IO.Err, "Aborted.")
return NewError(CodeUserAborted, "delete aborted")
}
return nil
}