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:
@@ -0,0 +1,122 @@
|
||||
// Package linkcmd implements `weknora link` — re-points an existing
|
||||
// .weknora/project.yaml at a different knowledge base. Distinct from
|
||||
// `weknora init`: link assumes the directory is already a project (or you
|
||||
// want one without going through the init wizard) and never prompts.
|
||||
package linkcmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/Tencent/WeKnora/cli/internal/agent"
|
||||
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
||||
"github.com/Tencent/WeKnora/cli/internal/format"
|
||||
"github.com/Tencent/WeKnora/cli/internal/iostreams"
|
||||
"github.com/Tencent/WeKnora/cli/internal/projectlink"
|
||||
)
|
||||
|
||||
// Options captures `weknora link` flags.
|
||||
type Options struct {
|
||||
KBID string // --kb-id
|
||||
KBName string // --kb
|
||||
JSONOut bool // --json
|
||||
}
|
||||
|
||||
// linkResult is the typed payload emitted under data. Keep schema-aligned
|
||||
// with init.initResult so agents see the same shape regardless of entry point.
|
||||
type linkResult struct {
|
||||
Context string `json:"context"`
|
||||
KBID string `json:"kb_id"`
|
||||
KBName string `json:"kb_name,omitempty"`
|
||||
ProjectLinkPath string `json:"project_link_path"`
|
||||
}
|
||||
|
||||
// NewCmd builds the `weknora link` command.
|
||||
func NewCmd(f *cmdutil.Factory) *cobra.Command {
|
||||
opts := &Options{}
|
||||
cmd := &cobra.Command{
|
||||
Use: "link",
|
||||
Short: "Update or create the project link in the current directory",
|
||||
Long: `Writes (overwriting if present) .weknora/project.yaml in the current
|
||||
working directory pointing at the supplied knowledge base. Unlike init, link
|
||||
never prompts and never fails on an existing file — it is the non-interactive
|
||||
re-link path used by scripts that switch between knowledge bases.`,
|
||||
Example: ` weknora link --kb-id kb_abc
|
||||
weknora link --kb staging`,
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(c *cobra.Command, _ []string) error {
|
||||
return runLink(c.Context(), opts, f)
|
||||
},
|
||||
}
|
||||
cmd.Flags().StringVar(&opts.KBID, "kb-id", "", "Knowledge base id to link")
|
||||
cmd.Flags().StringVar(&opts.KBName, "kb", "", "Knowledge base name (resolved to id)")
|
||||
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
|
||||
cmd.MarkFlagsMutuallyExclusive("kb-id", "kb")
|
||||
cmd.MarkFlagsOneRequired("kb-id", "kb")
|
||||
agent.SetAgentHelp(cmd, "Re-points .weknora/project.yaml to a KB. Pass --kb-id (or --kb name); never prompts, always overwrites.")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func runLink(ctx context.Context, opts *Options, f *cmdutil.Factory) error {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return cmdutil.Wrapf(cmdutil.CodeLocalFileIO, err, "get cwd")
|
||||
}
|
||||
linkPath := filepath.Join(cwd, projectlink.DirName, projectlink.FileName)
|
||||
|
||||
cfg, err := f.Config()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctxName := cfg.CurrentContext
|
||||
if ctxName == "" {
|
||||
return cmdutil.NewError(cmdutil.CodeAuthUnauthenticated, "no active context; run `weknora auth login` first")
|
||||
}
|
||||
|
||||
kbID, kbName := opts.KBID, ""
|
||||
if opts.KBName != "" {
|
||||
cli, err := f.Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id, err := cmdutil.ResolveKBNameToID(ctx, cli, opts.KBName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
kbID, kbName = id, opts.KBName
|
||||
}
|
||||
|
||||
link := &projectlink.Project{
|
||||
Context: ctxName,
|
||||
KBID: kbID,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
if err := projectlink.Save(linkPath, link); err != nil {
|
||||
return cmdutil.Wrapf(cmdutil.CodeLocalFileIO, err, "write project link")
|
||||
}
|
||||
|
||||
r := linkResult{
|
||||
Context: ctxName,
|
||||
KBID: kbID,
|
||||
KBName: kbName,
|
||||
ProjectLinkPath: linkPath,
|
||||
}
|
||||
if opts.JSONOut {
|
||||
return format.WriteEnvelope(iostreams.IO.Out, format.Success(r, &format.Meta{
|
||||
Context: ctxName,
|
||||
KBID: kbID,
|
||||
}))
|
||||
}
|
||||
if kbName != "" {
|
||||
fmt.Fprintf(iostreams.IO.Out, "✓ Linked %s to %s (kb=%s, id=%s)\n", linkPath, ctxName, kbName, kbID)
|
||||
} else {
|
||||
fmt.Fprintf(iostreams.IO.Out, "✓ Linked %s to %s (kb_id=%s)\n", linkPath, ctxName, kbID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
package linkcmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
||||
"github.com/Tencent/WeKnora/cli/internal/config"
|
||||
"github.com/Tencent/WeKnora/cli/internal/iostreams"
|
||||
"github.com/Tencent/WeKnora/cli/internal/projectlink"
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
func chdir(t *testing.T, dir string) {
|
||||
t.Helper()
|
||||
prev, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.Chdir(dir))
|
||||
t.Cleanup(func() { _ = os.Chdir(prev) })
|
||||
}
|
||||
|
||||
func fakeKBServer(t *testing.T, kbs []sdk.KnowledgeBase) *httptest.Server {
|
||||
t.Helper()
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/api/v1/knowledge-bases", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(sdk.KnowledgeBaseListResponse{Success: true, Data: kbs})
|
||||
})
|
||||
srv := httptest.NewServer(mux)
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
}
|
||||
|
||||
func newFactory(currentCtx string, client *sdk.Client) *cmdutil.Factory {
|
||||
cfg := &config.Config{
|
||||
CurrentContext: currentCtx,
|
||||
Contexts: map[string]config.Context{
|
||||
currentCtx: {Host: "https://example"},
|
||||
},
|
||||
}
|
||||
return &cmdutil.Factory{
|
||||
Config: func() (*config.Config, error) { return cfg, nil },
|
||||
Client: func() (*sdk.Client, error) {
|
||||
if client == nil {
|
||||
return nil, errors.New("client not configured")
|
||||
}
|
||||
return client, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestLink_ByID(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
chdir(t, dir)
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
|
||||
f := newFactory("default", nil)
|
||||
opts := &Options{KBID: "kb_xxx"}
|
||||
require.NoError(t, runLink(context.Background(), opts, f))
|
||||
|
||||
linkPath := filepath.Join(dir, ".weknora", "project.yaml")
|
||||
p, err := projectlink.Load(linkPath)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "kb_xxx", p.KBID)
|
||||
assert.Equal(t, "default", p.Context)
|
||||
assert.Contains(t, out.String(), "✓")
|
||||
}
|
||||
|
||||
func TestLink_ByName(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
chdir(t, dir)
|
||||
_, _ = iostreams.SetForTest(t)
|
||||
|
||||
srv := fakeKBServer(t, []sdk.KnowledgeBase{
|
||||
{ID: "kb_a", Name: "foo"},
|
||||
{ID: "kb_b", Name: "bar"},
|
||||
})
|
||||
cli := sdk.NewClient(srv.URL)
|
||||
f := newFactory("default", cli)
|
||||
opts := &Options{KBName: "foo"}
|
||||
require.NoError(t, runLink(context.Background(), opts, f))
|
||||
|
||||
p, err := projectlink.Load(filepath.Join(dir, ".weknora", "project.yaml"))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "kb_a", p.KBID)
|
||||
}
|
||||
|
||||
func TestLink_KBNotFound(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
chdir(t, dir)
|
||||
_, _ = iostreams.SetForTest(t)
|
||||
|
||||
srv := fakeKBServer(t, []sdk.KnowledgeBase{{ID: "kb_a", Name: "foo"}})
|
||||
cli := sdk.NewClient(srv.URL)
|
||||
f := newFactory("default", cli)
|
||||
opts := &Options{KBName: "missing"}
|
||||
err := runLink(context.Background(), opts, f)
|
||||
require.Error(t, err)
|
||||
var typed *cmdutil.Error
|
||||
require.ErrorAs(t, err, &typed)
|
||||
assert.Equal(t, cmdutil.CodeKBNotFound, typed.Code)
|
||||
}
|
||||
|
||||
// TestLink_MutuallyExclusive exercises the cobra flag-parse layer. Driving
|
||||
// the command with both --kb-id and --kb must surface a usage error before
|
||||
// runLink runs.
|
||||
func TestLink_MutuallyExclusive(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
chdir(t, dir)
|
||||
_, _ = iostreams.SetForTest(t)
|
||||
|
||||
f := newFactory("default", nil)
|
||||
cmd := NewCmd(f)
|
||||
var stderr bytes.Buffer
|
||||
cmd.SetErr(&stderr)
|
||||
cmd.SetOut(&bytes.Buffer{})
|
||||
cmd.SetArgs([]string{"--kb-id", "kb_a", "--kb", "foo"})
|
||||
cmd.SetContext(context.Background())
|
||||
|
||||
err := cmd.Execute()
|
||||
require.Error(t, err, "expected mutually-exclusive flag error")
|
||||
assert.True(t,
|
||||
strings.Contains(err.Error(), "if any flags in the group") ||
|
||||
strings.Contains(err.Error(), "mutually exclusive") ||
|
||||
strings.Contains(err.Error(), "exclusive"),
|
||||
"error should mention mutual exclusivity, got %q", err.Error())
|
||||
}
|
||||
|
||||
// TestLink_OneRequired exercises MarkFlagsOneRequired: zero of {kb-id, kb}
|
||||
// must be a usage error.
|
||||
func TestLink_OneRequired(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
chdir(t, dir)
|
||||
_, _ = iostreams.SetForTest(t)
|
||||
|
||||
f := newFactory("default", nil)
|
||||
cmd := NewCmd(f)
|
||||
var stderr bytes.Buffer
|
||||
cmd.SetErr(&stderr)
|
||||
cmd.SetOut(&bytes.Buffer{})
|
||||
cmd.SetArgs([]string{}) // neither flag
|
||||
cmd.SetContext(context.Background())
|
||||
|
||||
err := cmd.Execute()
|
||||
require.Error(t, err, "expected required-flag error")
|
||||
}
|
||||
Reference in New Issue
Block a user