|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "embed" |
| 5 | + "fmt" |
| 6 | + "io/fs" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/docker/model-runner/cmd/cli/commands/completion" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +//go:embed skills/* |
| 15 | +var skillsFS embed.FS |
| 16 | + |
| 17 | +type skillsOptions struct { |
| 18 | + codex bool |
| 19 | + claude bool |
| 20 | + opencode bool |
| 21 | + dest string |
| 22 | + force bool |
| 23 | +} |
| 24 | + |
| 25 | +func newSkillsCmd() *cobra.Command { |
| 26 | + opts := &skillsOptions{} |
| 27 | + |
| 28 | + c := &cobra.Command{ |
| 29 | + Use: "skills", |
| 30 | + Short: "Install Docker Model Runner skills for AI coding assistants", |
| 31 | + Long: `Install Docker Model Runner skills for AI coding assistants. |
| 32 | +
|
| 33 | +Skills are configuration files that help AI coding assistants understand |
| 34 | +how to use Docker Model Runner effectively for local model inference. |
| 35 | +
|
| 36 | +Supported targets: |
| 37 | + --codex Install to ~/.codex/skills (OpenAI Codex CLI) |
| 38 | + --claude Install to ~/.claude/skills (Claude Code) |
| 39 | + --opencode Install to ~/.config/opencode/skills (OpenCode) |
| 40 | + --dest Install to a custom directory |
| 41 | +
|
| 42 | +Example: |
| 43 | + docker model skills --claude |
| 44 | + docker model skills --codex --claude |
| 45 | + docker model skills --dest /path/to/skills`, |
| 46 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 47 | + return runSkills(cmd, opts) |
| 48 | + }, |
| 49 | + ValidArgsFunction: completion.NoComplete, |
| 50 | + } |
| 51 | + |
| 52 | + c.Flags().BoolVar(&opts.codex, "codex", false, "Install skills for OpenAI Codex CLI (~/.codex/skills)") |
| 53 | + c.Flags().BoolVar(&opts.claude, "claude", false, "Install skills for Claude Code (~/.claude/skills)") |
| 54 | + c.Flags().BoolVar(&opts.opencode, "opencode", false, "Install skills for OpenCode (~/.config/opencode/skills)") |
| 55 | + c.Flags().StringVar(&opts.dest, "dest", "", "Install skills to a custom directory") |
| 56 | + c.Flags().BoolVarP(&opts.force, "force", "f", false, "Overwrite existing skills without prompting") |
| 57 | + |
| 58 | + return c |
| 59 | +} |
| 60 | + |
| 61 | +func runSkills(cmd *cobra.Command, opts *skillsOptions) error { |
| 62 | + // Collect target directories |
| 63 | + var targets []string |
| 64 | + homeDir, err := os.UserHomeDir() |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("failed to get home directory: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + if opts.codex { |
| 70 | + targets = append(targets, filepath.Join(homeDir, ".codex", "skills")) |
| 71 | + } |
| 72 | + if opts.claude { |
| 73 | + targets = append(targets, filepath.Join(homeDir, ".claude", "skills")) |
| 74 | + } |
| 75 | + if opts.opencode { |
| 76 | + targets = append(targets, filepath.Join(homeDir, ".config", "opencode", "skills")) |
| 77 | + } |
| 78 | + if opts.dest != "" { |
| 79 | + targets = append(targets, opts.dest) |
| 80 | + } |
| 81 | + |
| 82 | + if len(targets) == 0 { |
| 83 | + return fmt.Errorf("no target specified. Use --codex, --claude, --opencode, or --dest") |
| 84 | + } |
| 85 | + |
| 86 | + // Install skills to each target |
| 87 | + for _, target := range targets { |
| 88 | + if err := installSkills(cmd, target, opts.force); err != nil { |
| 89 | + return fmt.Errorf("failed to install skills to %s: %w", target, err) |
| 90 | + } |
| 91 | + cmd.Printf("Installed Docker Model Runner skills to %s\n", target) |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func installSkills(cmd *cobra.Command, targetDir string, force bool) error { |
| 98 | + // Walk through embedded skills directory |
| 99 | + return fs.WalkDir(skillsFS, "skills", func(path string, d fs.DirEntry, err error) error { |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + // Skip the root "skills" directory itself |
| 105 | + if path == "skills" { |
| 106 | + return nil |
| 107 | + } |
| 108 | + |
| 109 | + // Calculate the relative path from "skills/" |
| 110 | + relPath, err := filepath.Rel("skills", path) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + destPath := filepath.Join(targetDir, relPath) |
| 116 | + |
| 117 | + if d.IsDir() { |
| 118 | + // Create directory |
| 119 | + return os.MkdirAll(destPath, 0755) |
| 120 | + } |
| 121 | + |
| 122 | + // Check if file exists and handle force flag |
| 123 | + if _, err := os.Stat(destPath); err == nil && !force { |
| 124 | + return fmt.Errorf("file already exists: %s (use --force to overwrite)", destPath) |
| 125 | + } |
| 126 | + |
| 127 | + // Read the embedded file |
| 128 | + content, err := skillsFS.ReadFile(path) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("failed to read embedded file %s: %w", path, err) |
| 131 | + } |
| 132 | + |
| 133 | + // Ensure parent directory exists |
| 134 | + if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil { |
| 135 | + return fmt.Errorf("failed to create directory for %s: %w", destPath, err) |
| 136 | + } |
| 137 | + |
| 138 | + // Write the file |
| 139 | + if err := os.WriteFile(destPath, content, 0644); err != nil { |
| 140 | + return fmt.Errorf("failed to write file %s: %w", destPath, err) |
| 141 | + } |
| 142 | + |
| 143 | + return nil |
| 144 | + }) |
| 145 | +} |
0 commit comments