-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskill_diff.go
More file actions
235 lines (189 loc) · 6.47 KB
/
skill_diff.go
File metadata and controls
235 lines (189 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package cli
import (
"fmt"
"path/filepath"
"strings"
"github.com/devrimcavusoglu/skern/internal/output"
"github.com/devrimcavusoglu/skern/internal/platform"
"github.com/devrimcavusoglu/skern/internal/skill"
"github.com/spf13/cobra"
)
func newSkillDiffCmd() *cobra.Command {
var (
scope string
platformFlag string
)
cmd := &cobra.Command{
Use: "diff <name> [name-b]",
Short: "Compare two skills or a registry skill against its installed copy",
Long: `Compare two skills side by side.
With one argument, compares a registry skill against its installed copy on a platform
(requires --platform and --scope flags).
With two arguments, compares two registry skills by name.`,
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := getContext(cmd)
if len(args) == 2 {
return diffTwoSkills(ctx, args[0], args[1], scope)
}
return diffRegistryVsPlatform(ctx, args[0], scope, platformFlag)
},
}
cmd.Flags().StringVar(&scope, "scope", "", "skill scope (user or project)")
cmd.Flags().StringVar(&platformFlag, "platform", "", "platform to compare against (claude-code, codex-cli, opencode)")
return cmd
}
// diffTwoSkills compares two registry skills by name.
func diffTwoSkills(ctx *CommandContext, nameA, nameB, scopeStr string) error {
reg, err := ctx.NewRegistry()
if err != nil {
return err
}
skillA, _, scopeA, err := resolveSkill(reg, nameA, scopeStr)
if err != nil {
return fmt.Errorf("resolving skill %q: %w", nameA, err)
}
skillB, _, scopeB, err := resolveSkill(reg, nameB, scopeStr)
if err != nil {
return fmt.Errorf("resolving skill %q: %w", nameB, err)
}
sourceA := fmt.Sprintf("registry (%s)", scopeA)
sourceB := fmt.Sprintf("registry (%s)", scopeB)
result := compareSkills(skillA, nameA, sourceA, skillB, nameB, sourceB)
text := formatDiffResult(result)
ctx.Printer.PrintResult(result, text)
return nil
}
// diffRegistryVsPlatform compares a registry skill against its installed copy on a platform.
func diffRegistryVsPlatform(ctx *CommandContext, name, scopeStr, platformFlag string) error {
if platformFlag == "" {
return &ValidationError{Message: "comparing a registry skill against a platform requires --platform flag"}
}
if scopeStr == "" {
scopeStr = "user"
}
scopeVal, err := parseScope(scopeStr)
if err != nil {
return err
}
platformType, err := platform.ParsePlatformType(platformFlag)
if err != nil {
return &ValidationError{Message: err.Error()}
}
if platformType == platform.TypeAll {
return &ValidationError{Message: "diff requires a specific platform, not \"all\""}
}
reg, err := ctx.NewRegistry()
if err != nil {
return err
}
registrySkill, _, err := reg.Get(name, scopeVal)
if err != nil {
return fmt.Errorf("skill %q not found in %s scope: %w", name, scopeStr, err)
}
det, err := ctx.NewDetector()
if err != nil {
return err
}
p := det.Get(platformType)
if p == nil {
return &ValidationError{Message: fmt.Sprintf("platform %q not recognized", platformFlag)}
}
var platformDir string
if scopeVal == skill.ScopeProject {
platformDir = p.ProjectSkillsDir()
} else {
platformDir = p.UserSkillsDir()
}
manifestPath := filepath.Join(platformDir, name, "SKILL.md")
platformSkill, err := skill.ParseManifest(manifestPath)
if err != nil {
return fmt.Errorf("skill %q not installed on %s (%s scope): %w", name, platformFlag, scopeStr, err)
}
sourceA := fmt.Sprintf("registry (%s)", scopeStr)
sourceB := fmt.Sprintf("platform (%s)", platformFlag)
result := compareSkills(registrySkill, name, sourceA, platformSkill, name, sourceB)
text := formatDiffResult(result)
ctx.Printer.PrintResult(result, text)
return nil
}
// compareSkills compares two skills and produces a SkillDiffResult.
func compareSkills(a *skill.Skill, nameA, sourceA string, b *skill.Skill, nameB, sourceB string) output.SkillDiffResult {
var fields []output.FieldDiff
if a.Name != b.Name {
fields = append(fields, output.FieldDiff{Field: "name", Left: a.Name, Right: b.Name})
}
descA := strings.TrimSpace(a.Description)
descB := strings.TrimSpace(b.Description)
if descA != descB {
fields = append(fields, output.FieldDiff{Field: "description", Left: descA, Right: descB})
}
if a.Metadata.Version != b.Metadata.Version {
fields = append(fields, output.FieldDiff{Field: "version", Left: a.Metadata.Version, Right: b.Metadata.Version})
}
if a.Metadata.Author.Name != b.Metadata.Author.Name {
fields = append(fields, output.FieldDiff{Field: "author.name", Left: a.Metadata.Author.Name, Right: b.Metadata.Author.Name})
}
if a.Metadata.Author.Type != b.Metadata.Author.Type {
fields = append(fields, output.FieldDiff{Field: "author.type", Left: a.Metadata.Author.Type, Right: b.Metadata.Author.Type})
}
if a.Metadata.Author.Platform != b.Metadata.Author.Platform {
fields = append(fields, output.FieldDiff{Field: "author.platform", Left: a.Metadata.Author.Platform, Right: b.Metadata.Author.Platform})
}
tagsA := strings.Join(a.Tags, ", ")
tagsB := strings.Join(b.Tags, ", ")
if tagsA != tagsB {
fields = append(fields, output.FieldDiff{Field: "tags", Left: tagsA, Right: tagsB})
}
toolsA := strings.Join(a.AllowedTools, ", ")
toolsB := strings.Join(b.AllowedTools, ", ")
if toolsA != toolsB {
fields = append(fields, output.FieldDiff{Field: "allowed-tools", Left: toolsA, Right: toolsB})
}
bodyDiff := a.Body != b.Body
result := output.SkillDiffResult{
LeftName: nameA,
LeftSource: sourceA,
RightName: nameB,
RightSource: sourceB,
Identical: len(fields) == 0 && !bodyDiff,
Fields: fields,
BodyDiff: bodyDiff,
}
if bodyDiff {
result.LeftBody = a.Body
result.RightBody = b.Body
}
return result
}
// formatDiffResult formats a diff result for text output.
func formatDiffResult(r output.SkillDiffResult) string {
var b strings.Builder
fmt.Fprintf(&b, "Comparing %s (%s) vs %s (%s)\n\n", r.LeftName, r.LeftSource, r.RightName, r.RightSource)
if r.Identical {
b.WriteString("Skills are identical.\n")
return b.String()
}
if len(r.Fields) > 0 {
b.WriteString("Metadata differences:\n")
for _, f := range r.Fields {
fmt.Fprintf(&b, " %s:\n", f.Field)
fmt.Fprintf(&b, " - %s\n", displayValue(f.Left))
fmt.Fprintf(&b, " + %s\n", displayValue(f.Right))
}
}
if r.BodyDiff {
if len(r.Fields) > 0 {
b.WriteString("\n")
}
b.WriteString("Body content differs.\n")
}
return b.String()
}
// displayValue returns the value or "(empty)" if blank.
func displayValue(v string) string {
if v == "" {
return "(empty)"
}
return v
}