Files
doks/cmd/edit.go
2026-01-22 23:03:49 -06:00

60 lines
1.3 KiB
Go

package cmd
import (
"fmt"
"os"
"os/exec"
"github.com/spf13/cobra"
"github.com/yeho/doks/internal/registry"
)
var editCmd = &cobra.Command{
Use: "edit <key>",
Short: "Edit a document in your configured editor",
Long: `Open a document in your configured editor (default: vim). Set defaultEditor in config to change.`,
Args: cobra.ExactArgs(1),
ValidArgsFunction: completeKeys,
Run: func(cmd *cobra.Command, args []string) {
key := args[0]
reg, err := registry.Load(cfg.RegistryPath())
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading registry: %v\n", err)
os.Exit(1)
}
entry, err := reg.Get(key)
if err != nil {
fmt.Fprintf(os.Stderr, "Document not found: %s\n", key)
os.Exit(1)
}
filePath := cfg.FilePath(entry.Filename)
// Get editor from config, fallback to EDITOR env, then vim
editor := cfg.DefaultEditor
if editor == "" {
editor = os.Getenv("EDITOR")
}
if editor == "" {
editor = "vim"
}
// Run editor
editorCmd := exec.Command(editor, filePath)
editorCmd.Stdin = os.Stdin
editorCmd.Stdout = os.Stdout
editorCmd.Stderr = os.Stderr
if err := editorCmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error running editor: %v\n", err)
os.Exit(1)
}
},
}
func init() {
rootCmd.AddCommand(editCmd)
}