grokkit/cmd/edit.go
Greg Gauthier e355142c05 feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00

89 lines
2.5 KiB
Go

package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/spf13/cobra"
"gmgauthier.com/grokkit/config"
"gmgauthier.com/grokkit/internal/grok"
)
var editCmd = &cobra.Command{
Use: "edit FILE INSTRUCTION",
Short: "Edit a file in-place with Grok (safe preview + backup)",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
filePath := args[0]
instruction := args[1]
modelFlag, _ := cmd.Flags().GetString("model")
model := config.GetModel(modelFlag)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
color.Red("File not found: %s", filePath)
os.Exit(1)
}
original, err := os.ReadFile(filePath)
if err != nil {
color.Red("Failed to read file: %v", err)
os.Exit(1)
}
cleanedOriginal := removeLastModifiedComments(string(original))
backupPath := filePath + ".bak"
if err := os.WriteFile(backupPath, original, 0644); err != nil {
color.Red("Failed to create backup: %v", err)
os.Exit(1)
}
client := grok.NewClient()
messages := []map[string]string{
{"role": "system", "content": "You are an expert programmer. Remove all unnecessary comments including last modified timestamps and ownership comments. Return only the cleaned code with no explanations, no markdown, no extra text."},
{"role": "user", "content": fmt.Sprintf("File: %s\n\nOriginal content:\n%s\n\nTask: %s", filepath.Base(filePath), cleanedOriginal, instruction)},
}
color.Yellow("Asking Grok to %s...\n", instruction)
raw := client.StreamSilent(messages, model)
newContent := grok.CleanCodeResponse(raw)
color.Green("✓ Response received")
color.Cyan("\nProposed changes:")
fmt.Println("--- a/" + filepath.Base(filePath))
fmt.Println("+++ b/" + filepath.Base(filePath))
fmt.Print(newContent)
fmt.Print("\n\nApply these changes? (y/n): ")
var confirm string
fmt.Scanln(&confirm)
if confirm != "y" && confirm != "Y" {
color.Yellow("Changes discarded. Backup saved as %s", backupPath)
return
}
if err := os.WriteFile(filePath, []byte(newContent), 0644); err != nil {
color.Red("Failed to write file: %v", err)
os.Exit(1)
}
color.Green("✅ Applied successfully! Backup: %s", backupPath)
},
}
func removeLastModifiedComments(content string) string {
lines := strings.Split(content, "\n")
var cleanedLines []string
for _, line := range lines {
if strings.Contains(line, "Last modified") {
continue
}
cleanedLines = append(cleanedLines, line)
}
return strings.Join(cleanedLines, "\n")
}