|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +// canonicalPasteSkillHeader is used by hasGrokSkill to validate that a probed |
| 14 | +// source tree contains *our* canonical skill (defense against malicious trees |
| 15 | +// in probed locations like cwd or ~/src). |
| 16 | +var canonicalPasteSkillHeader = []byte("# PasteLocal /paste — Grok Skill") |
| 17 | + |
| 18 | +// ── grok (agent integration) ───────────────────────────────────────────────── |
| 19 | + |
| 20 | +var grokCmd = &cobra.Command{ |
| 21 | + Use: "grok", |
| 22 | + Short: "Grok / agentic coding tool integration helpers", |
| 23 | + GroupID: "grok", |
| 24 | +} |
| 25 | + |
| 26 | +var grokInstallCmd = &cobra.Command{ |
| 27 | + Use: "install-skills", |
| 28 | + Short: "Install or update PasteLocal Grok skills (paste, history, snippet, send, recall) into ~/.grok/skills/", |
| 29 | + Args: cobra.NoArgs, |
| 30 | + RunE: runGrokInstallSkills, |
| 31 | +} |
| 32 | + |
| 33 | +func init() { |
| 34 | + grokCmd.AddCommand(grokInstallCmd) |
| 35 | + rootCmd.AddCommand(grokCmd) |
| 36 | +} |
| 37 | + |
| 38 | +func runGrokInstallSkills(cmd *cobra.Command, args []string) error { |
| 39 | + srcDir, err := findGrokSourceDir() |
| 40 | + if err != nil { |
| 41 | + return fail("%v", err) |
| 42 | + } |
| 43 | + |
| 44 | + home, err := os.UserHomeDir() |
| 45 | + if err != nil { |
| 46 | + return fail("could not determine home directory: %v", err) |
| 47 | + } |
| 48 | + dstBase := filepath.Join(home, ".grok", "skills") |
| 49 | + // 0755 is intentional for a user-discoverable skills directory (Grok/AI tools must read SKILL.md). |
| 50 | + // Contrast with 0700/0600 used only for private tokens/keys (see SECURITY.md and auth/store). |
| 51 | + if err := os.MkdirAll(dstBase, 0755); err != nil { |
| 52 | + return fail("failed to create ~/.grok/skills: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + skills := []string{"paste", "paste-send", "recall", "paste-history", "paste-snippet"} |
| 56 | + copied := 0 |
| 57 | + for _, name := range skills { |
| 58 | + src := filepath.Join(srcDir, name) |
| 59 | + if _, err := os.Stat(filepath.Join(src, "SKILL.md")); err != nil { |
| 60 | + continue // skip missing (future-proof) |
| 61 | + } |
| 62 | + dst := filepath.Join(dstBase, "pastelocal-"+name) |
| 63 | + _ = os.RemoveAll(dst) // clean previous |
| 64 | + cpBin := "/bin/cp" |
| 65 | + if _, err := os.Stat(cpBin); err != nil { |
| 66 | + cpBin = "/usr/bin/cp" |
| 67 | + } |
| 68 | + cp := exec.Command(cpBin, "-r", src, dst) |
| 69 | + if output, err := cp.CombinedOutput(); err != nil { |
| 70 | + return fail("failed to install skill %s: %v\n%s", name, err, string(output)) |
| 71 | + } |
| 72 | + copied++ |
| 73 | + } |
| 74 | + |
| 75 | + if copied == 0 { |
| 76 | + return fail("no skills were copied from %s (run from PasteLocal source tree or provide correct layout)", srcDir) |
| 77 | + } |
| 78 | + fmt.Printf("PasteLocal Grok skills installed successfully (%d skills).\n", copied) |
| 79 | + fmt.Println(" Discoverable as: /paste, /paste-send, /paste-history, /paste-snippet, /recall") |
| 80 | + fmt.Println("\nUpdate anytime with: pastelocal grok install-skills (after git pull)") |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +// findGrokSourceDir locates the skill/grok tree relative to the running binary, |
| 85 | +// cwd, or common install locations. Prefers source tree clones. |
| 86 | +func findGrokSourceDir() (string, error) { |
| 87 | + // Try relative to the executable (works when running from a build next to the repo tree). |
| 88 | + if exe, err := os.Executable(); err == nil { |
| 89 | + dir := filepath.Dir(exe) |
| 90 | + for _, rel := range []string{"../skill/grok", "skill/grok", "../../skill/grok"} { |
| 91 | + cand := filepath.Join(dir, rel) |
| 92 | + if hasGrokSkill(cand) { |
| 93 | + return filepath.Clean(cand), nil |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // CWD or parent (user is inside the cloned repo). |
| 99 | + if cwd, err := os.Getwd(); err == nil { |
| 100 | + for _, rel := range []string{"skill/grok", "../skill/grok", "PasteLocal/skill/grok"} { |
| 101 | + cand := filepath.Join(cwd, rel) |
| 102 | + if hasGrokSkill(cand) { |
| 103 | + return filepath.Clean(cand), nil |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Fallback locations for packaged or user installs. |
| 109 | + home, homeErr := os.UserHomeDir() |
| 110 | + for _, cand := range []string{ |
| 111 | + filepath.Join(home, ".local", "share", "pastelocal", "skill", "grok"), |
| 112 | + filepath.Join(home, "src", "PasteLocal", "skill", "grok"), |
| 113 | + "/usr/local/share/pastelocal/skill/grok", |
| 114 | + } { |
| 115 | + if hasGrokSkill(cand) { |
| 116 | + return cand, nil |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if homeErr != nil { |
| 121 | + return "", fmt.Errorf("PasteLocal grok skills source not found (home probe err: %v). Run from a clone of the official repo (git instructions below)", homeErr) |
| 122 | + } |
| 123 | + return "", fmt.Errorf("PasteLocal grok skills source not found. Run from a clone of the official repo: git clone https://github.com/Zen-Open-Source/PasteLocal && cd PasteLocal && ./pastelocal grok install-skills (or go run ./cmd/pastelocal grok install-skills)") |
| 124 | +} |
| 125 | + |
| 126 | +func hasGrokSkill(dir string) bool { |
| 127 | + p := filepath.Join(dir, "paste", "SKILL.md") |
| 128 | + data, err := os.ReadFile(p) |
| 129 | + if err != nil { |
| 130 | + return false |
| 131 | + } |
| 132 | + return bytes.Contains(data, canonicalPasteSkillHeader) |
| 133 | +} |
0 commit comments