feat(cli): add project-link foundation with init and link commands
v0.2 grounds the CLI's resource commands (kb / doc / chat / query) in a
per-project link file (.weknora/project.yaml) so users don't have to pass
--kb-id on every invocation. Mirrors npm/cargo/git: walk up the cwd tree
to find the project root, override via flag or env when needed.
This commit ships the foundation layer:
- cli/internal/projectlink/ Discover (walk-up, depth=64) / Load / Save
- cmdutil.Factory.ResolveKB 5-level fallback chain:
--kb-id flag → --kb name (ListKnowledgeBases lookup) →
WEKNORA_KB_ID env → walk-up project link → CodeKBIDRequired
- cmdutil.ResolveKBNameToID shared name→id helper used by init / link
/ Factory.ResolveKB (was duplicated 3 ways
in early implementation; reuse review #2)
- cli/cmd/init/ interactive (huh prompt) or flag-driven
first-time setup; refuses to overwrite
without --force
- cli/cmd/link/ non-interactive update; --kb-id and --kb
are mutually exclusive and one is required
Also registers the v0.2 ErrorCode set (all codes for the eight new
commands) and AST-scan identToErrorCode mapping in one place — keeps the
acceptance/contract suite green across the v0.2 commit chain even before
later commits reference each code.
This commit is contained in:
@@ -3,10 +3,14 @@ package cmdutil
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/Tencent/WeKnora/cli/internal/config"
|
||||
"github.com/Tencent/WeKnora/cli/internal/iostreams"
|
||||
"github.com/Tencent/WeKnora/cli/internal/projectlink"
|
||||
"github.com/Tencent/WeKnora/cli/internal/prompt"
|
||||
"github.com/Tencent/WeKnora/cli/internal/secrets"
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
@@ -142,6 +146,45 @@ func buildClient(f *Factory) (*sdk.Client, error) {
|
||||
return sdk.NewClient(ctx.Host, opts...), nil
|
||||
}
|
||||
|
||||
// ResolveKB returns the active KB id for the running command, applying the
|
||||
// 5-level fallback chain (highest to lowest):
|
||||
// 1. --kb-id flag (explicit ID)
|
||||
// 2. --kb flag (name → id via ListKnowledgeBases)
|
||||
// 3. WEKNORA_KB_ID env
|
||||
// 4. .weknora/project.yaml (walk-up from cwd)
|
||||
// 5. error: kb_id required
|
||||
//
|
||||
// Sub-commands that need a KB declare both --kb-id and --kb local flags;
|
||||
// commands that don't (e.g. kb list, init) skip this method entirely.
|
||||
func (f *Factory) ResolveKB(cmd *cobra.Command) (string, error) {
|
||||
if id, _ := cmd.Flags().GetString("kb-id"); id != "" {
|
||||
return id, nil
|
||||
}
|
||||
if name, _ := cmd.Flags().GetString("kb"); name != "" {
|
||||
c, err := f.Client()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return ResolveKBNameToID(cmd.Context(), c, name)
|
||||
}
|
||||
if v := os.Getenv("WEKNORA_KB_ID"); v != "" {
|
||||
return v, nil
|
||||
}
|
||||
cwd, err := os.Getwd()
|
||||
if err == nil {
|
||||
if path, found, derr := projectlink.Discover(cwd); derr == nil && found {
|
||||
p, lerr := projectlink.Load(path)
|
||||
if lerr != nil {
|
||||
return "", Wrapf(CodeProjectLinkCorrupt, lerr, "read project link")
|
||||
}
|
||||
if p.KBID != "" {
|
||||
return p.KBID, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", NewError(CodeKBIDRequired, "kb id 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) {
|
||||
|
||||
Reference in New Issue
Block a user