fix: resolve golangci-lint issues in prompts package

- Fix gofmt formatting in loader.go and loader_test.go
- Modernize strings.Index to strings.Cut (template.go:162,231)
- Use min() builtin instead of manual if check (template.go:273)
This commit is contained in:
Ed Zynda
2026-03-22 19:11:52 +03:00
parent 017eb99d44
commit 7ff9e84894
3 changed files with 30 additions and 40 deletions
+5 -5
View File
@@ -41,11 +41,11 @@ type Diagnostic struct {
// to highest), with later templates overriding earlier ones of the same name.
//
// Discovery paths searched in order:
// 1. Default templates (if IncludeDefaults)
// 2. ~/.kit/prompts/ (global user templates)
// 3. .kit/prompts/ (project-local templates)
// 4. ConfigPaths (from configuration)
// 5. ExtraPaths (explicit paths, highest precedence)
// 1. Default templates (if IncludeDefaults)
// 2. ~/.kit/prompts/ (global user templates)
// 3. .kit/prompts/ (project-local templates)
// 4. ConfigPaths (from configuration)
// 5. ExtraPaths (explicit paths, highest precedence)
func LoadAll(opts LoadOptions) ([]*PromptTemplate, []Diagnostic, error) {
if opts.Cwd == "" {
opts.Cwd, _ = os.Getwd()
+20 -20
View File
@@ -9,51 +9,51 @@ import (
func TestLoadAll_Integration(t *testing.T) {
// Create a temp directory for testing
tempDir := t.TempDir()
// Create the .kit/prompts subdirectory structure
promptsDir := filepath.Join(tempDir, ".kit", "prompts")
if err := os.MkdirAll(promptsDir, 0755); err != nil {
t.Fatalf("Failed to create prompts dir: %v", err)
}
// Create a test template file
templateContent := `---
description: Test template for integration
---
Review $1 with focus on $2`
testFile := filepath.Join(promptsDir, "test.md")
if err := os.WriteFile(testFile, []byte(templateContent), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Test loading from the temp directory
tpls, diags, err := LoadAll(LoadOptions{
HomeDir: tempDir,
IncludeDefaults: false, // Skip default locations for this test
})
if err != nil {
t.Fatalf("LoadAll failed: %v", err)
}
if len(diags) > 0 {
t.Logf("Got %d diagnostics", len(diags))
}
if len(tpls) != 1 {
t.Fatalf("Expected 1 template, got %d", len(tpls))
}
tpl := tpls[0]
if tpl.Name != "test" {
t.Errorf("Expected name 'test', got '%s'", tpl.Name)
}
if tpl.Description != "Test template for integration" {
t.Errorf("Expected description 'Test template for integration', got '%s'", tpl.Description)
}
// Test expansion
expanded := tpl.Expand("code security")
expected := "Review code with focus on security"
@@ -69,25 +69,25 @@ func TestParseTemplate_WithFrontmatter(t *testing.T) {
description: A test template
---
Create a $1 component with $2 features`
testFile := filepath.Join(tempDir, "component.md")
if err := os.WriteFile(testFile, []byte(templateContent), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
tpl, err := ParseTemplate(testFile)
if err != nil {
t.Fatalf("ParseTemplate failed: %v", err)
}
if tpl.Name != "component" {
t.Errorf("Expected name 'component', got '%s'", tpl.Name)
}
if tpl.Description != "A test template" {
t.Errorf("Expected description 'A test template', got '%s'", tpl.Description)
}
expectedContent := "Create a $1 component with $2 features"
if tpl.Content != expectedContent {
t.Errorf("Expected content '%s', got '%s'", expectedContent, tpl.Content)
@@ -99,26 +99,26 @@ func TestParseTemplate_WithoutFrontmatter(t *testing.T) {
tempDir := t.TempDir()
templateContent := `Simple template without frontmatter
Supports $1 and $2 placeholders`
testFile := filepath.Join(tempDir, "simple.md")
if err := os.WriteFile(testFile, []byte(templateContent), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
tpl, err := ParseTemplate(testFile)
if err != nil {
t.Fatalf("ParseTemplate failed: %v", err)
}
if tpl.Name != "simple" {
t.Errorf("Expected name 'simple', got '%s'", tpl.Name)
}
// Description should be empty since there's no frontmatter
if tpl.Description != "" {
t.Errorf("Expected empty description, got '%s'", tpl.Description)
}
// Content should include everything
if tpl.Content != templateContent {
t.Errorf("Content mismatch\nExpected:\n%s\nGot:\n%s", templateContent, tpl.Content)
+5 -15
View File
@@ -159,8 +159,8 @@ func SubstituteArgs(content string, args []string) string {
}
// Check if there's a second colon for length ${N:M:L}
if secondColonIdx := strings.Index(rest, ":"); secondColonIdx >= 0 {
lengthStr := rest[secondColonIdx+1:]
lengthStr, _, ok := strings.Cut(rest, ":")
if ok {
length, err := strconv.Atoi(lengthStr)
if err != nil || length < 0 {
return match
@@ -228,15 +228,7 @@ func expandAtArgs(inner string, args []string) string {
rest = rest[1:]
// Parse start index
colonIdx := strings.Index(rest, ":")
var startStr, lengthStr string
if colonIdx >= 0 {
startStr = rest[:colonIdx]
lengthStr = rest[colonIdx+1:]
} else {
startStr = rest
}
startStr, lengthStr, hasLength := strings.Cut(rest, ":")
start, err := strconv.Atoi(startStr)
if err != nil || start < 0 {
@@ -249,7 +241,7 @@ func expandAtArgs(inner string, args []string) string {
start--
}
if lengthStr != "" {
if hasLength {
length, err := strconv.Atoi(lengthStr)
if err != nil || length < 0 {
return "${" + inner + "}"
@@ -270,9 +262,7 @@ func joinArgsRange(args []string, start, length int) string {
return ""
}
end := start + length
if end > len(args) {
end = len(args)
}
end = min(end, len(args))
return strings.Join(args[start:end], " ")
}