grokkit/cmd/edit.go
Greg Gauthier f0858e08c1 refactor(cli): simplify commands and remove TUI dependencies
- Switch chat command from Bubble Tea TUI to basic CLI with bufio.
- Hardcode Grok model in commands, remove Viper config.
- Stream responses directly in client, remove channel-based streaming.
- Add safe previews/backups in edit, simplify prompts across tools.
- Update git helper and trim unused deps in go.mod.
2026-02-28 20:17:12 +00:00

54 lines
1.5 KiB
Go

package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/fatih/color"
"github.com/spf13/cobra"
"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]
if _, err := os.Stat(filePath); os.IsNotExist(err) {
color.Red("File not found: %s", filePath)
os.Exit(1)
}
original, _ := os.ReadFile(filePath)
backupPath := filePath + ".bak"
_ = os.WriteFile(backupPath, original, 0644)
client := grok.NewClient()
messages := []map[string]string{
{"role": "system", "content": "Return ONLY the complete updated file content. No explanations, no markdown, no diffs."},
{"role": "user", "content": fmt.Sprintf("File: %s\n\nOriginal content:\n%s\n\nTask: %s", filepath.Base(filePath), original, instruction)},
}
color.Yellow("Asking Grok to %s...", instruction)
newContent := client.Stream(messages, "grok-4-1-fast-non-reasoning")
color.Cyan("\nProposed changes (new file content):")
color.White(newContent)
fmt.Print("\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
}
_ = os.WriteFile(filePath, []byte(newContent), 0644)
color.Green("✅ Applied successfully! Backup: %s", backupPath)
},
}