diff --git a/cmd/skills.go b/cmd/skills.go index a73f21a2..99a75670 100644 --- a/cmd/skills.go +++ b/cmd/skills.go @@ -6,6 +6,7 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/spf13/cobra" + "github.com/render-oss/cli/pkg/command" "github.com/render-oss/cli/pkg/tui" "github.com/render-oss/cli/pkg/tui/views" ) @@ -16,6 +17,10 @@ var skillsCmd = &cobra.Command{ Long: `Install and manage Render agent skills for AI coding tools such as Claude Code, Codex, OpenCode, and Cursor. Skills add deployment, debugging, and monitoring capabilities to your AI coding assistant.`, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() + // The command palette needs the TUI; otherwise list the subcommands. + if !command.IsInteractive(ctx) { + return cmd.Help() + } stack := tui.GetStackFromContext(ctx) commands := []views.PaletteCommand{ diff --git a/cmd/skills_test.go b/cmd/skills_test.go new file mode 100644 index 00000000..01840533 --- /dev/null +++ b/cmd/skills_test.go @@ -0,0 +1,38 @@ +package cmd + +import ( + "fmt" + "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// Outside interactive mode, `render skills update` and bare `render skills` +// have no TUI stack to push onto. They used to panic; see +// https://github.com/render-oss/cli/issues/79. +func TestSkillsUpdate_NonInteractive(t *testing.T) { + for _, args := range [][]string{nil, {"-o", "text"}, {"-o", "json"}, {"--force", "--scope", "user", "-o", "text"}} { + t.Run(fmt.Sprint(args), func(t *testing.T) { + _, err := executeSkillsCommand(t, newSkillsUpdateCmd(), args...) + + assert.EqualError(t, err, "`render skills update` can only be used in interactive mode") + }) + } +} + +func TestSkills_NonInteractivePrintsHelp(t *testing.T) { + for _, args := range [][]string{nil, {"-o", "text"}, {"-o", "json"}} { + t.Run(fmt.Sprint(args), func(t *testing.T) { + skills := &cobra.Command{Use: "skills", Short: skillsCmd.Short, RunE: skillsCmd.RunE} + skills.AddCommand(&cobra.Command{Use: "list", Short: "List installed Render skills", Run: func(*cobra.Command, []string) {}}) + + result, err := executeSkillsArgs(t, skills, args...) + + require.NoError(t, err) + assert.Contains(t, result.Stdout, "render skills [subcommand]") + assert.Regexp(t, `list\s+List installed Render skills`, result.Stdout) + }) + } +} diff --git a/cmd/skillslist.go b/cmd/skillslist.go index e698f790..8fde56c8 100644 --- a/cmd/skillslist.go +++ b/cmd/skillslist.go @@ -1,26 +1,42 @@ package cmd import ( + "fmt" + "github.com/spf13/cobra" + "github.com/render-oss/cli/pkg/command" "github.com/render-oss/cli/pkg/skills" + "github.com/render-oss/cli/pkg/style" + "github.com/render-oss/cli/pkg/text" "github.com/render-oss/cli/pkg/tui" "github.com/render-oss/cli/pkg/tui/views" ) -var skillsListCmd = &cobra.Command{ - Use: "list", - Short: "List installed Render skills and detected tools", - Long: `List installed Render skills and the AI tools they've been installed in. This reads from local state only, so the command doesn't require network access. +// newSkillsListCmd builds `render skills list`. loadState reads the local +// skills state, so tests can supply it without touching the home directory. +func newSkillsListCmd(loadState func() (*skills.LoadedState, error)) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List installed Render skills and detected tools", + Long: `List installed Render skills and the AI tools they've been installed in. This reads from local state only, so the command doesn't require network access. Use --scope to filter by installation scope (user or project).`, - Example: ` # List all installed skills + Example: ` # List all installed skills render skills list # List project-scoped skills only - render skills list --scope project`, - SilenceUsage: true, - RunE: func(cmd *cobra.Command, args []string) error { + render skills list --scope project + + # Output as JSON + render skills list -o json`, + SilenceUsage: true, + } + + cmd.Flags().String("scope", "", "Filter skills by installation scope (user or project)") + setFlagPlaceholder(cmd.Flags(), "scope", "SCOPE") + + cmd.RunE = func(cmd *cobra.Command, args []string) error { scopeFilter, _ := cmd.Flags().GetString("scope") var scope skills.Scope @@ -32,6 +48,32 @@ Use --scope to filter by installation scope (user or project).`, } } + if nonInteractive, err := command.NonInteractive(cmd, func() (*skills.ListResult, error) { + loaded, err := loadState() + if err != nil { + return nil, err + } + // JSON and YAML carry the warnings in the output. Text prints them + // to stderr so they don't mix into the tables. + if format := command.GetFormatFromContext(cmd.Context()); format != nil && *format == command.TEXT { + warning := style.Status.Foreground(style.ColorWarning).Render("Warning:") + for _, w := range loaded.Warnings { + _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%s %s\n", warning, w) + } + } + // Project skills are listed for the current repository; outside + // one, their folders are unknown. + var repoRoot string + if scope == skills.ScopeProject { + repoRoot, _ = skills.GetRepoRoot() + } + return skills.NewListResult(loaded, scope, repoRoot), nil + }, text.SkillTables); err != nil { + return err + } else if nonInteractive { + return nil + } + // Push TUI view onto the stack. // We push directly (not via AddToStackFunc) because skills are // purely local — there's no CLI command string to copy. @@ -42,11 +84,11 @@ Use --scope to filter by installation scope (user or project).`, Breadcrumb: "List Skills", }) return nil - }, + } + + return cmd } func init() { - skillsCmd.AddCommand(skillsListCmd) - skillsListCmd.Flags().String("scope", "", "Filter skills by installation scope (user or project)") - setFlagPlaceholder(skillsListCmd.Flags(), "scope", "SCOPE") + skillsCmd.AddCommand(newSkillsListCmd(skills.LoadOrRebuildState)) } diff --git a/cmd/skillslist_test.go b/cmd/skillslist_test.go new file mode 100644 index 00000000..134e5bb1 --- /dev/null +++ b/cmd/skillslist_test.go @@ -0,0 +1,275 @@ +package cmd + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "os/exec" + "path/filepath" + "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" + + "github.com/render-oss/cli/pkg/command" + "github.com/render-oss/cli/pkg/dependencies" + "github.com/render-oss/cli/pkg/skills" + "github.com/render-oss/cli/pkg/tui" +) + +// executeSkillsList runs `render skills list ` through the root command +// without a TTY, reading loaded in place of the local skills state. +func executeSkillsList(t *testing.T, loaded *skills.LoadedState, args ...string) (CommandResult, error) { + t.Helper() + return executeSkillsListWithLoader(t, func() (*skills.LoadedState, error) { + return loaded, nil + }, args...) +} + +func executeSkillsListWithLoader(t *testing.T, loadState func() (*skills.LoadedState, error), args ...string) (CommandResult, error) { + t.Helper() + return executeSkillsCommand(t, newSkillsListCmd(loadState), args...) +} + +// executeSkillsCommand runs `render skills ` through the root +// command without a TTY. sub is a fresh command, so flag values don't carry +// over between tests. HOME is a temporary directory, so tool detection never +// reads the real one. The skills state path is fixed when the package loads, +// so a test that gets past a command's argument checks must supply the state, +// as the list tests do. +func executeSkillsCommand(t *testing.T, sub *cobra.Command, args ...string) (CommandResult, error) { + t.Helper() + skillsParent := &cobra.Command{Use: "skills"} + skillsParent.AddCommand(sub) + return executeSkillsArgs(t, skillsParent, append([]string{sub.Name()}, args...)...) +} + +// executeSkillsArgs runs `render skills `, with skillsParent as the +// skills command, in the same way as executeSkillsCommand. A panic is returned +// as an error, so it fails only the test that caused it. +func executeSkillsArgs(t *testing.T, skillsParent *cobra.Command, args ...string) (result CommandResult, err error) { + t.Helper() + t.Setenv("HOME", t.TempDir()) + t.Setenv("USERPROFILE", t.TempDir()) + + deps := dependencies.New(nil) + deps.DetectRuntimeSignals = func() (command.RuntimeSignals, error) { + return command.RuntimeSignals{}, nil + } + + root := newRootCmd() + root.AddCommand(skillsParent) + setupRootCmdPersistentRun(root, deps) + + var stdout, stderr bytes.Buffer + root.SetOut(&stdout) + root.SetErr(&stderr) + root.SetArgs(append([]string{"skills"}, args...)) + + defer func() { + if r := recover(); r != nil { + err = fmt.Errorf("panic: %v", r) + } + result = CommandResult{Stdout: stdout.String(), Stderr: stderr.String()} + }() + return result, root.Execute() +} + +// Regression test for https://github.com/render-oss/cli/issues/79: outside +// interactive mode there is no TUI stack, and pushing a view onto it panicked. +func TestSkillsList_NonInteractiveDoesNotPanic(t *testing.T) { + for _, tc := range []struct { + name string + args []string + }{ + {name: "no TTY", args: nil}, + {name: "text", args: []string{"-o", "text"}}, + {name: "json", args: []string{"-o", "json"}}, + {name: "yaml", args: []string{"-o", "yaml"}}, + } { + t.Run(tc.name, func(t *testing.T) { + loaded := &skills.LoadedState{State: &skills.SkillsState{}} + + result, err := executeSkillsList(t, loaded, tc.args...) + + require.NoError(t, err) + assert.NotEmpty(t, result.Stdout) + }) + } +} + +func skillsListTestState() *skills.LoadedState { + return &skills.LoadedState{ + State: &skills.SkillsState{ + Tools: []string{"Claude Code", "Cursor"}, + Skills: []skills.InstalledSkill{ + {Name: "render-deploy", Version: "1.2.0", Scope: skills.ScopeUser}, + {Name: "render-debug", Version: "unknown"}, + {Name: "render-monitor", Version: "0.3.0", Scope: skills.ScopeProject}, + }, + InstalledAt: "2026-11-30T12:00:00Z", + }, + DetectedTools: []skills.Tool{ + {Name: "Claude Code", SkillsDir: "/home/me/.claude/skills"}, + }, + } +} + +func TestSkillsList_JSON(t *testing.T) { + result, err := executeSkillsList(t, skillsListTestState(), "-o", "json") + require.NoError(t, err) + + var got skills.ListResult + require.NoError(t, json.Unmarshal([]byte(result.Stdout), &got)) + assert.Equal(t, skills.ListResult{ + Skills: []skills.ListedSkill{ + {Name: "render-deploy", Version: "1.2.0", Scope: skills.ScopeUser}, + {Name: "render-debug", Scope: skills.ScopeUser}, + {Name: "render-monitor", Version: "0.3.0", Scope: skills.ScopeProject}, + }, + Tools: []skills.ListedTool{ + {Name: "Claude Code", Detected: true, SkillsDir: "/home/me/.claude/skills"}, + {Name: "Cursor", Detected: false}, + }, + InstalledAt: "2026-11-30T12:00:00Z", + }, got) +} + +func TestSkillsList_ScopeFilter(t *testing.T) { + t.Run("json", func(t *testing.T) { + result, err := executeSkillsList(t, skillsListTestState(), "--scope", "project", "-o", "json") + require.NoError(t, err) + + var got skills.ListResult + require.NoError(t, json.Unmarshal([]byte(result.Stdout), &got)) + require.Len(t, got.Skills, 1) + assert.Equal(t, "render-monitor", got.Skills[0].Name) + }) + + t.Run("json reports the project skills folder", func(t *testing.T) { + repoRoot := newGitRepo(t) + + result, err := executeSkillsList(t, skillsListTestState(), "--scope", "project", "-o", "json") + require.NoError(t, err) + + var got skills.ListResult + require.NoError(t, json.Unmarshal([]byte(result.Stdout), &got)) + assert.Equal(t, []skills.ListedTool{ + {Name: "Claude Code", Detected: true, SkillsDir: filepath.Join(repoRoot, ".claude", "skills")}, + {Name: "Cursor", Detected: false}, + }, got.Tools) + }) + + t.Run("json outside a git repository omits the project skills folder", func(t *testing.T) { + t.Chdir(t.TempDir()) + + result, err := executeSkillsList(t, skillsListTestState(), "--scope", "project", "-o", "json") + require.NoError(t, err) + + var got skills.ListResult + require.NoError(t, json.Unmarshal([]byte(result.Stdout), &got)) + assert.Equal(t, []skills.ListedTool{ + {Name: "Claude Code", Detected: true}, + {Name: "Cursor", Detected: false}, + }, got.Tools) + }) + + t.Run("yaml", func(t *testing.T) { + result, err := executeSkillsList(t, skillsListTestState(), "--scope", "project", "-o", "yaml") + require.NoError(t, err) + + var got map[string]any + require.NoError(t, yaml.Unmarshal([]byte(result.Stdout), &got)) + assert.Equal(t, []any{ + map[string]any{"name": "render-monitor", "version": "0.3.0", "scope": "project"}, + }, got["skills"]) + }) +} + +func TestSkillsList_Text(t *testing.T) { + result, err := executeSkillsList(t, skillsListTestState(), "-o", "text") + require.NoError(t, err) + + assert.Regexp(t, `render-deploy\s+1\.2\.0\s+user`, result.Stdout) + assert.Regexp(t, `render-debug\s+-\s+user`, result.Stdout) + assert.Regexp(t, `render-monitor\s+0\.3\.0\s+project`, result.Stdout) + assert.Regexp(t, `Claude Code\s+true\s+/home/me/\.claude/skills`, result.Stdout) + assert.Regexp(t, `Cursor\s+false\s+-`, result.Stdout) +} + +func TestSkillsList_NoSkillsPrintsEmptyLists(t *testing.T) { + loaded := &skills.LoadedState{State: &skills.SkillsState{}} + + result, err := executeSkillsList(t, loaded, "-o", "json") + require.NoError(t, err) + + assert.JSONEq(t, `{"skills": [], "tools": []}`, result.Stdout) +} + +func TestSkillsList_Warnings(t *testing.T) { + loaded := skillsListTestState() + loaded.Warnings = []string{"could not hash render-debug: permission denied"} + + t.Run("text prints them to stderr", func(t *testing.T) { + result, err := executeSkillsList(t, loaded, "-o", "text") + require.NoError(t, err) + + assert.Contains(t, result.Stderr, "Warning:") + assert.Contains(t, result.Stderr, "could not hash render-debug: permission denied") + assert.NotContains(t, result.Stdout, "could not hash") + }) + + t.Run("json carries them only in the output", func(t *testing.T) { + result, err := executeSkillsList(t, loaded, "-o", "json") + require.NoError(t, err) + + assert.Empty(t, result.Stderr) + var got skills.ListResult + require.NoError(t, json.Unmarshal([]byte(result.Stdout), &got)) + assert.Equal(t, []string{"could not hash render-debug: permission denied"}, got.Warnings) + }) +} + +// In interactive mode the command still opens the list view. +func TestSkillsList_InteractivePushesView(t *testing.T) { + interactive := command.Interactive + stack := tui.NewStack() + ctx := command.SetFormatInContext(context.Background(), &interactive) + ctx = tui.SetStackInContext(ctx, stack) + + cmd := newSkillsListCmd(func() (*skills.LoadedState, error) { + t.Fatal("interactive mode should not load state in the command") + return nil, nil + }) + cmd.SetArgs(nil) + require.NoError(t, cmd.ExecuteContext(ctx)) + + assert.False(t, stack.IsEmpty()) +} + +// newGitRepo makes the current directory a new git repository and returns its +// root as git reports it. +func newGitRepo(t *testing.T) string { + t.Helper() + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not available") + } + dir := t.TempDir() + t.Chdir(dir) + require.NoError(t, exec.Command("git", "init", "-q").Run()) + root, err := skills.GetRepoRoot() + require.NoError(t, err) + return root +} + +func TestSkillsList_LoadError(t *testing.T) { + _, err := executeSkillsListWithLoader(t, func() (*skills.LoadedState, error) { + return nil, errors.New("failed to load skills state: boom") + }, "-o", "json") + + require.ErrorContains(t, err, "failed to load skills state: boom") +} diff --git a/cmd/skillsremove.go b/cmd/skillsremove.go index 8304c1a9..1fb59c8d 100644 --- a/cmd/skillsremove.go +++ b/cmd/skillsremove.go @@ -12,15 +12,16 @@ import ( "github.com/render-oss/cli/pkg/tui/views" ) -var skillsRemoveCmd = &cobra.Command{ - Use: "remove", - Short: "Remove installed Render skills from AI coding tools", - Long: `Remove previously installed Render skills from detected AI coding tools. +func newSkillsRemoveCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "remove", + Short: "Remove installed Render skills from AI coding tools", + Long: `Remove previously installed Render skills from detected AI coding tools. By default an interactive prompt lets you pick which skills to remove. Use --skill and --all flags to skip the prompts. Use --scope to remove from a specific scope (user or project).`, - Example: ` # Remove skills interactively + Example: ` # Remove skills interactively render skills remove # Remove specific skills @@ -28,50 +29,59 @@ Use --scope to remove from a specific scope (user or project).`, # Remove all project-scoped skills render skills remove --all --scope project`, - SilenceUsage: true, - RunE: func(cmd *cobra.Command, args []string) error { - skillFilter, _ := cmd.Flags().GetStringSlice("skill") - toolFilter, _ := cmd.Flags().GetString("tool") - removeAll, _ := cmd.Flags().GetBool("all") - scopeFilter, _ := cmd.Flags().GetString("scope") - - // Parse scope if provided - var scope skills.Scope - if scopeFilter != "" { - var err error - scope, err = skills.ParseScope(scopeFilter) - if err != nil { - return err + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + skillFilter, _ := cmd.Flags().GetStringSlice("skill") + toolFilter, _ := cmd.Flags().GetString("tool") + removeAll, _ := cmd.Flags().GetBool("all") + scopeFilter, _ := cmd.Flags().GetString("scope") + + // Parse scope if provided + var scope skills.Scope + if scopeFilter != "" { + var err error + scope, err = skills.ParseScope(scopeFilter) + if err != nil { + return err + } } - } - // Non-interactive path: flags provided. - if removeAll || len(skillFilter) > 0 { - return nonInteractiveSkillsRemove(cmd, skillFilter, toolFilter, removeAll, scope) - } + // Non-interactive path: flags provided. + if removeAll || len(skillFilter) > 0 { + return nonInteractiveSkillsRemove(cmd, skillFilter, toolFilter, removeAll, scope) + } - // Interactive path: push TUI view onto the stack. - // We push directly (not via AddToStackFunc) because skills are - // purely local — there's no CLI command string to copy. - ctx := cmd.Context() - stack := tui.GetStackFromContext(ctx) - stack.Push(tui.ModelWithCmd{ - Model: views.NewSkillsRemoveView(scope), - Breadcrumb: "Remove Skills", - }) - return nil - }, + // Choosing skills needs the interactive prompt, so outside + // interactive mode one of the flags is required. + ctx := cmd.Context() + if !command.IsInteractive(ctx) { + return fmt.Errorf("--all or --skill is required in non-interactive mode") + } + + // Interactive path: push TUI view onto the stack. + // We push directly (not via AddToStackFunc) because skills are + // purely local — there's no CLI command string to copy. + stack := tui.GetStackFromContext(ctx) + stack.Push(tui.ModelWithCmd{ + Model: views.NewSkillsRemoveView(scope), + Breadcrumb: "Remove Skills", + }) + return nil + }, + } + + cmd.Flags().StringSlice("skill", nil, "Remove specific skills only (use --skill multiple times)") + cmd.Flags().String("tool", "", "Remove skills from a specific tool only (claude, codex, opencode, or cursor)") + cmd.Flags().Bool("all", false, "Remove all installed Render skills") + cmd.Flags().String("scope", "", "Remove skills from the specified scope (user or project)") + setFlagPlaceholder(cmd.Flags(), "skill", "SKILL") + setFlagPlaceholder(cmd.Flags(), "tool", "TOOL") + setFlagPlaceholder(cmd.Flags(), "scope", "SCOPE") + return cmd } func init() { - skillsCmd.AddCommand(skillsRemoveCmd) - skillsRemoveCmd.Flags().StringSlice("skill", nil, "Remove specific skills only (use --skill multiple times)") - skillsRemoveCmd.Flags().String("tool", "", "Remove skills from a specific tool only (claude, codex, opencode, or cursor)") - skillsRemoveCmd.Flags().Bool("all", false, "Remove all installed Render skills") - skillsRemoveCmd.Flags().String("scope", "", "Remove skills from the specified scope (user or project)") - setFlagPlaceholder(skillsRemoveCmd.Flags(), "skill", "SKILL") - setFlagPlaceholder(skillsRemoveCmd.Flags(), "tool", "TOOL") - setFlagPlaceholder(skillsRemoveCmd.Flags(), "scope", "SCOPE") + skillsCmd.AddCommand(newSkillsRemoveCmd()) } // nonInteractiveSkillsRemove runs the remove flow without prompts. diff --git a/cmd/skillsremove_test.go b/cmd/skillsremove_test.go new file mode 100644 index 00000000..d58191b3 --- /dev/null +++ b/cmd/skillsremove_test.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +// Regression test for https://github.com/render-oss/cli/issues/79: without +// --all or --skill, `render skills remove` pushed its TUI view, and outside +// interactive mode there is no stack to push onto. +func TestSkillsRemove_NonInteractiveRequiresAllOrSkill(t *testing.T) { + for _, tc := range []struct { + name string + args []string + }{ + {name: "no TTY", args: nil}, + {name: "text", args: []string{"-o", "text"}}, + {name: "json", args: []string{"-o", "json"}}, + {name: "tool and scope", args: []string{"--confirm", "--tool", "claude", "--scope", "user", "-o", "text"}}, + } { + t.Run(tc.name, func(t *testing.T) { + _, err := executeSkillsCommand(t, newSkillsRemoveCmd(), tc.args...) + + assert.EqualError(t, err, "--all or --skill is required in non-interactive mode") + }) + } +} diff --git a/cmd/skillsupdate.go b/cmd/skillsupdate.go index 0a5e3432..1de384eb 100644 --- a/cmd/skillsupdate.go +++ b/cmd/skillsupdate.go @@ -1,22 +1,26 @@ package cmd import ( + "fmt" + "github.com/spf13/cobra" + "github.com/render-oss/cli/pkg/command" "github.com/render-oss/cli/pkg/skills" "github.com/render-oss/cli/pkg/tui" "github.com/render-oss/cli/pkg/tui/views" ) -var skillsUpdateCmd = &cobra.Command{ - Use: "update", - Short: "Update previously installed Render skills", - Long: `Reinstall Render skills using the tool and skill selections saved by a previous "render skills install" run. +func newSkillsUpdateCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "update", + Short: "Update previously installed Render skills", + Long: `Reinstall Render skills using the tool and skill selections saved by a previous "render skills install" run. This fetches the latest version of each selected skill from the skills repository, compares with installed versions, and updates any that have changed. Use --scope to update skills at a specific scope (user or project).`, - Example: ` # Update installed skills + Example: ` # Update installed skills render skills update # Force reinstall all skills @@ -24,36 +28,43 @@ Use --scope to update skills at a specific scope (user or project).`, # Update project-scoped skills render skills update --scope project`, - SilenceUsage: true, - RunE: func(cmd *cobra.Command, args []string) error { - force, _ := cmd.Flags().GetBool("force") - scopeFilter, _ := cmd.Flags().GetString("scope") - - var scope skills.Scope - if scopeFilter != "" { - var err error - scope, err = skills.ParseScope(scopeFilter) - if err != nil { - return err + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + if !command.IsInteractive(cmd.Context()) { + return fmt.Errorf("`%s` can only be used in interactive mode", cmd.CommandPath()) + } + + force, _ := cmd.Flags().GetBool("force") + scopeFilter, _ := cmd.Flags().GetString("scope") + + var scope skills.Scope + if scopeFilter != "" { + var err error + scope, err = skills.ParseScope(scopeFilter) + if err != nil { + return err + } } - } - - // Push TUI view onto the stack. - // We push directly (not via AddToStackFunc) because skills are - // purely local — there's no CLI command string to copy. - ctx := cmd.Context() - stack := tui.GetStackFromContext(ctx) - stack.Push(tui.ModelWithCmd{ - Model: views.NewSkillsUpdateView(force, scope), - Breadcrumb: "Update Skills", - }) - return nil - }, + + // Push TUI view onto the stack. + // We push directly (not via AddToStackFunc) because skills are + // purely local — there's no CLI command string to copy. + ctx := cmd.Context() + stack := tui.GetStackFromContext(ctx) + stack.Push(tui.ModelWithCmd{ + Model: views.NewSkillsUpdateView(force, scope), + Breadcrumb: "Update Skills", + }) + return nil + }, + } + + cmd.Flags().Bool("force", false, "Reinstall all skills even if already up to date") + cmd.Flags().String("scope", "", "Update skills at the specified scope (user or project)") + setFlagPlaceholder(cmd.Flags(), "scope", "SCOPE") + return cmd } func init() { - skillsCmd.AddCommand(skillsUpdateCmd) - skillsUpdateCmd.Flags().Bool("force", false, "Reinstall all skills even if already up to date") - skillsUpdateCmd.Flags().String("scope", "", "Update skills at the specified scope (user or project)") - setFlagPlaceholder(skillsUpdateCmd.Flags(), "scope", "SCOPE") + skillsCmd.AddCommand(newSkillsUpdateCmd()) } diff --git a/pkg/skills/list.go b/pkg/skills/list.go new file mode 100644 index 00000000..fbcd26e0 --- /dev/null +++ b/pkg/skills/list.go @@ -0,0 +1,86 @@ +package skills + +// ListResult is the JSON/YAML contract for `render skills list`. +type ListResult struct { + Skills []ListedSkill `json:"skills"` + Tools []ListedTool `json:"tools"` + InstalledAt string `json:"installedAt,omitempty"` + Warnings []string `json:"warnings,omitempty"` +} + +// ListedSkill is an installed Render skill. +type ListedSkill struct { + Name string `json:"name"` + Version string `json:"version,omitempty"` + Scope Scope `json:"scope"` +} + +// ListedTool is an AI coding tool that skills were installed to. Detected is +// false when the tool is recorded in state but is no longer on this machine. +type ListedTool struct { + Name string `json:"name"` + Detected bool `json:"detected"` + SkillsDir string `json:"skillsDir,omitempty"` +} + +// NewListResult builds the list of installed skills and their tools from +// loaded state. A non-empty scope keeps only the skills installed at that scope. +// For project scope, each tool's skills folder is in repoRoot, and is left out +// when repoRoot is empty. +func NewListResult(loaded *LoadedState, scope Scope, repoRoot string) *ListResult { + result := &ListResult{ + Skills: []ListedSkill{}, + Tools: []ListedTool{}, + InstalledAt: loaded.State.InstalledAt, + Warnings: loaded.Warnings, + } + + for _, sk := range FilterSkillsByScope(loaded.State.Skills, scope) { + version := sk.Version + if version == "unknown" { + version = "" + } + result.Skills = append(result.Skills, ListedSkill{ + Name: sk.Name, + Version: version, + Scope: sk.EffectiveScope(), + }) + } + + detected := make(map[string]Tool, len(loaded.DetectedTools)) + for _, t := range loaded.DetectedTools { + detected[t.Name] = t + } + for _, name := range loaded.State.Tools { + t, ok := detected[name] + skillsDir := t.SkillsDir + if ok && scope == ScopeProject { + skillsDir = "" + if repoRoot != "" { + skillsDir = GetScopedSkillsDir(t, scope, repoRoot) + } + } + result.Tools = append(result.Tools, ListedTool{ + Name: name, + Detected: ok, + SkillsDir: skillsDir, + }) + } + + return result +} + +// FilterSkillsByScope returns the skills installed at scope, treating a skill +// with no recorded scope as user scope. An empty scope returns all skills. +func FilterSkillsByScope(installed []InstalledSkill, scope Scope) []InstalledSkill { + if scope == "" { + return installed + } + var filtered []InstalledSkill + for _, sk := range installed { + if sk.EffectiveScope() == scope { + filtered = append(filtered, sk) + } + } + return filtered +} diff --git a/pkg/skills/list_test.go b/pkg/skills/list_test.go new file mode 100644 index 00000000..bb099ab7 --- /dev/null +++ b/pkg/skills/list_test.go @@ -0,0 +1,30 @@ +package skills + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFilterSkillsByScope(t *testing.T) { + user := InstalledSkill{Name: "render-deploy", Scope: ScopeUser} + legacy := InstalledSkill{Name: "render-debug"} // no scope recorded + project := InstalledSkill{Name: "render-monitor", Scope: ScopeProject} + installed := []InstalledSkill{user, legacy, project} + + t.Run("empty scope returns all", func(t *testing.T) { + assert.Equal(t, installed, FilterSkillsByScope(installed, "")) + }) + + t.Run("user scope includes skills with no recorded scope", func(t *testing.T) { + assert.Equal(t, []InstalledSkill{user, legacy}, FilterSkillsByScope(installed, ScopeUser)) + }) + + t.Run("project scope", func(t *testing.T) { + assert.Equal(t, []InstalledSkill{project}, FilterSkillsByScope(installed, ScopeProject)) + }) + + t.Run("no match", func(t *testing.T) { + assert.Empty(t, FilterSkillsByScope([]InstalledSkill{user}, ScopeProject)) + }) +} diff --git a/pkg/text/skills.go b/pkg/text/skills.go new file mode 100644 index 00000000..330a00cb --- /dev/null +++ b/pkg/text/skills.go @@ -0,0 +1,33 @@ +package text + +import ( + "github.com/jedib0t/go-pretty/table" + + "github.com/render-oss/cli/pkg/skills" +) + +// SkillTables renders installed skills and the tools they were installed to +// as two tables. +func SkillTables(r *skills.ListResult) string { + skillTable := newTable() + skillTable.AppendHeader(table.Row{"Name", "Version", "Scope"}) + for _, sk := range r.Skills { + version := sk.Version + if version == "" { + version = "-" + } + skillTable.AppendRow(table.Row{sk.Name, version, sk.Scope}) + } + + toolTable := newTable() + toolTable.AppendHeader(table.Row{"Tool", "Detected", "Skills Dir"}) + for _, t := range r.Tools { + dir := t.SkillsDir + if dir == "" { + dir = "-" + } + toolTable.AppendRow(table.Row{t.Name, t.Detected, dir}) + } + + return FormatString(skillTable.Render() + "\n\n" + toolTable.Render()) +} diff --git a/pkg/text/skills_test.go b/pkg/text/skills_test.go new file mode 100644 index 00000000..f2209ccf --- /dev/null +++ b/pkg/text/skills_test.go @@ -0,0 +1,31 @@ +package text_test + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/render-oss/cli/pkg/skills" + "github.com/render-oss/cli/pkg/text" +) + +func TestSkillTables_ContainsHeadersAndRows(t *testing.T) { + out := text.SkillTables(&skills.ListResult{ + Skills: []skills.ListedSkill{{Name: "render-deploy", Version: "1.2.0", Scope: skills.ScopeProject}}, + Tools: []skills.ListedTool{{Name: "Claude Code", Detected: true, SkillsDir: "/home/u/.claude/skills"}}, + }) + for _, want := range []string{"NAME", "VERSION", "SCOPE", "TOOL", "DETECTED", "SKILLS DIR", + "render-deploy", "1.2.0", "project", "Claude Code", "true", "/home/u/.claude/skills"} { + assert.True(t, strings.Contains(out, want), "expected %q in output:\n%s", want, out) + } +} + +func TestSkillTables_EmptyCellsShowDash(t *testing.T) { + out := text.SkillTables(&skills.ListResult{ + Skills: []skills.ListedSkill{{Name: "render-debug", Scope: skills.ScopeUser}}, + Tools: []skills.ListedTool{{Name: "Cursor"}}, + }) + assert.True(t, strings.Contains(out, "false"), "undetected tool should render as false:\n%s", out) + assert.Equal(t, 2, strings.Count(out, " - "), "missing version and skills dir should render as -:\n%s", out) +} diff --git a/pkg/tui/views/skillslist.go b/pkg/tui/views/skillslist.go index 9e4badb9..34d0d2f0 100644 --- a/pkg/tui/views/skillslist.go +++ b/pkg/tui/views/skillslist.go @@ -144,15 +144,7 @@ func (v *SkillsListView) renderOutput(warnings []string) string { } // Filter skills by scope if specified - filteredSkills := v.state.Skills - if v.scopeFilter != "" { - filteredSkills = nil - for _, sk := range v.state.Skills { - if sk.EffectiveScope() == v.scopeFilter { - filteredSkills = append(filteredSkills, sk) - } - } - } + filteredSkills := skills.FilterSkillsByScope(v.state.Skills, v.scopeFilter) // No skills installed. if len(filteredSkills) == 0 {