mirror of
https://github.com/onyx-dot-app/onyx.git
synced 2026-10-21 19:02:45 -05:00
206 lines
6.5 KiB
Go
206 lines
6.5 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/onyx-dot-app/onyx/cli/internal/embedded"
|
|
"github.com/onyx-dot-app/onyx/cli/internal/fsutil"
|
|
"github.com/onyx-dot-app/onyx/cli/internal/iostreams"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// agentSkillDirs maps agent names to their skill directory paths (relative to
|
|
// the project or home root). "Universal" agents like Cursor and Codex read
|
|
// from .agents/skills directly, so they don't need their own entry here.
|
|
var agentSkillDirs = map[string]string{
|
|
"claude-code": filepath.Join(".claude", "skills"),
|
|
}
|
|
|
|
const (
|
|
canonicalDir = ".agents/skills"
|
|
skillName = "onyx-cli"
|
|
)
|
|
|
|
func newInstallSkillCmd(ios *iostreams.IOStreams) *cobra.Command {
|
|
var (
|
|
global bool
|
|
copyMode bool
|
|
agents []string
|
|
)
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "install-skill",
|
|
Short: "Install the Onyx CLI agent skill file",
|
|
Long: `Install the bundled SKILL.md so that AI coding agents can discover and use
|
|
the Onyx CLI as a tool.
|
|
|
|
Files are written to the canonical .agents/skills/onyx-cli/ directory. For
|
|
agents that use their own skill directory (e.g. Claude Code uses .claude/skills/),
|
|
a symlink is created pointing back to the canonical copy.
|
|
|
|
By default the skill is installed at the project level (current directory).
|
|
Use --global to install under your home directory instead.
|
|
|
|
Use --copy to write independent copies instead of symlinks.
|
|
Use --agent to target specific agents (can be repeated).`,
|
|
Example: ` onyx-cli install-skill
|
|
onyx-cli install-skill --global
|
|
onyx-cli install-skill --agent claude-code
|
|
onyx-cli install-skill --copy`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
base, err := installBase(global)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// A project checkout may hold planted symlinks, so writes there
|
|
// never follow them. The home directory is the user's own.
|
|
writeSkill := func(dest string, data []byte) error {
|
|
if global {
|
|
if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(dest, data, 0o644)
|
|
}
|
|
return fsutil.WriteFileNoFollow(base, dest, data, 0o644)
|
|
}
|
|
|
|
// Write the canonical copy.
|
|
canonicalSkillDir := filepath.Join(base, canonicalDir, skillName)
|
|
dest := filepath.Join(canonicalSkillDir, "SKILL.md")
|
|
content := []byte(embedded.SkillMD)
|
|
|
|
status, err := fsutil.CompareFile(dest, content)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch status {
|
|
case fsutil.StatusUpToDate:
|
|
fmt.Fprintf(ios.Out, "Up to date %s\n", dest)
|
|
default:
|
|
if status == fsutil.StatusDiffers {
|
|
fmt.Fprintf(ios.ErrOut, "Warning: overwriting modified %s\n", dest)
|
|
}
|
|
if err := writeSkill(dest, content); err != nil {
|
|
return fmt.Errorf("could not write skill file: %w", err)
|
|
}
|
|
fmt.Fprintf(ios.Out, "Installed %s\n", dest)
|
|
}
|
|
|
|
// Determine which agents to link.
|
|
targets := agentSkillDirs
|
|
if len(agents) > 0 {
|
|
targets = make(map[string]string)
|
|
for _, a := range agents {
|
|
dir, ok := agentSkillDirs[a]
|
|
if !ok {
|
|
fmt.Fprintf(ios.ErrOut, "Unknown agent %q (skipped) — known agents:", a)
|
|
for name := range agentSkillDirs {
|
|
fmt.Fprintf(ios.ErrOut, " %s", name)
|
|
}
|
|
fmt.Fprintln(ios.ErrOut)
|
|
continue
|
|
}
|
|
targets[a] = dir
|
|
}
|
|
}
|
|
|
|
// Create symlinks (or copies) from agent-specific dirs to canonical.
|
|
for name, skillsDir := range targets {
|
|
agentSkillDir := filepath.Join(base, skillsDir, skillName)
|
|
// The link and copy steps below follow links in the parents,
|
|
// so a parent may only resolve inside the project.
|
|
if !global {
|
|
if err := fsutil.CheckResolvesInside(base, filepath.Dir(agentSkillDir)); err != nil {
|
|
return fmt.Errorf("could not prepare %s directory: %w", name, err)
|
|
}
|
|
}
|
|
|
|
// A committed link such as .claude/skills -> ../.agents/skills
|
|
// already makes the agent dir the canonical one. --copy still
|
|
// replaces a leaf link to the canonical dir with a real copy.
|
|
if sameDir(agentSkillDir, canonicalSkillDir) && (!copyMode || !isSymlink(agentSkillDir)) {
|
|
fmt.Fprintf(ios.Out, "Up to date %s\n", agentSkillDir)
|
|
continue
|
|
}
|
|
|
|
if copyMode {
|
|
copyDest := filepath.Join(agentSkillDir, "SKILL.md")
|
|
if err := fsutil.EnsureDirForCopy(agentSkillDir); err != nil {
|
|
return fmt.Errorf("could not prepare %s directory: %w", name, err)
|
|
}
|
|
if err := writeSkill(copyDest, []byte(embedded.SkillMD)); err != nil {
|
|
return fmt.Errorf("could not write %s skill file: %w", name, err)
|
|
}
|
|
fmt.Fprintf(ios.Out, "Copied %s\n", copyDest)
|
|
continue
|
|
}
|
|
|
|
// Compute relative symlink target. Symlinks resolve relative to
|
|
// the parent directory of the link, not the link itself.
|
|
rel, err := filepath.Rel(filepath.Dir(agentSkillDir), canonicalSkillDir)
|
|
if err != nil {
|
|
return fmt.Errorf("could not compute relative path for %s: %w", name, err)
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(agentSkillDir), 0o755); err != nil {
|
|
return fmt.Errorf("could not create %s directory: %w", name, err)
|
|
}
|
|
|
|
// Remove existing symlink/dir before creating.
|
|
_ = os.Remove(agentSkillDir)
|
|
|
|
if err := os.Symlink(rel, agentSkillDir); err != nil {
|
|
// Fall back to copy if symlink fails (e.g. Windows without dev mode).
|
|
copyDest := filepath.Join(agentSkillDir, "SKILL.md")
|
|
if wErr := writeSkill(copyDest, []byte(embedded.SkillMD)); wErr != nil {
|
|
return fmt.Errorf("could not write %s skill file: %w", name, wErr)
|
|
}
|
|
fmt.Fprintf(ios.Out, "Copied %s (symlink failed)\n", copyDest)
|
|
continue
|
|
}
|
|
fmt.Fprintf(ios.Out, "Linked %s -> %s\n", agentSkillDir, rel)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().BoolVarP(&global, "global", "g", false, "Install to home directory instead of project")
|
|
cmd.Flags().BoolVar(©Mode, "copy", false, "Copy files instead of symlinking")
|
|
cmd.Flags().StringSliceVarP(&agents, "agent", "a", nil, "Target specific agents (e.g. claude-code)")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func installBase(global bool) (string, error) {
|
|
if global {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", fmt.Errorf("could not determine home directory: %w", err)
|
|
}
|
|
return home, nil
|
|
}
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return "", fmt.Errorf("could not determine working directory: %w", err)
|
|
}
|
|
return cwd, nil
|
|
}
|
|
|
|
func sameDir(a, b string) bool {
|
|
ai, err := os.Stat(a)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
bi, err := os.Stat(b)
|
|
return err == nil && os.SameFile(ai, bi)
|
|
}
|
|
|
|
func isSymlink(path string) bool {
|
|
info, err := os.Lstat(path)
|
|
return err == nil && info.Mode()&os.ModeSymlink != 0
|
|
}
|