2026-03-26 12:48:47 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"axolotl/db"
|
|
|
|
|
"axolotl/output"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var editCmd = &cobra.Command{
|
2026-03-27 02:11:46 +01:00
|
|
|
Use: "edit <id>", Short: "Edit node content in $EDITOR", Args: cobra.ExactArgs(1),
|
2026-03-26 12:48:47 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
d, err := db.GetDB()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-27 02:11:46 +01:00
|
|
|
n, err := d.NodeByID(args[0])
|
2026-03-26 12:48:47 +00:00
|
|
|
if err != nil {
|
2026-03-27 02:11:46 +01:00
|
|
|
fmt.Fprintln(os.Stderr, "node not found:", args[0])
|
2026-03-26 12:48:47 +00:00
|
|
|
return
|
|
|
|
|
}
|
2026-03-27 02:11:46 +01:00
|
|
|
|
2026-03-26 12:48:47 +00:00
|
|
|
tmp, err := os.CreateTemp("", "ax-*.md")
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintln(os.Stderr, "failed to create temp file:", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
tmp.WriteString(n.Content)
|
|
|
|
|
tmp.Close()
|
|
|
|
|
defer os.Remove(tmp.Name())
|
|
|
|
|
|
|
|
|
|
editor := os.Getenv("EDITOR")
|
|
|
|
|
if editor == "" {
|
|
|
|
|
editor = "vi"
|
|
|
|
|
}
|
|
|
|
|
c := exec.Command(editor, tmp.Name())
|
2026-03-27 02:11:46 +01:00
|
|
|
c.Stdin, c.Stdout, c.Stderr = os.Stdin, os.Stdout, os.Stderr
|
2026-03-26 12:48:47 +00:00
|
|
|
if err := c.Run(); err != nil {
|
|
|
|
|
fmt.Fprintln(os.Stderr, "editor failed:", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-27 02:11:46 +01:00
|
|
|
|
|
|
|
|
if content, err := os.ReadFile(tmp.Name()); err == nil {
|
2026-03-27 02:41:27 +01:00
|
|
|
c := string(content)
|
|
|
|
|
if err := d.UpdateNode(args[0], db.UpdateParams{Content: &c}); err != nil {
|
2026-03-27 02:11:46 +01:00
|
|
|
fmt.Fprintln(os.Stderr, "failed to update:", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
n, _ = d.NodeByID(args[0])
|
|
|
|
|
output.PrintNode(cmd.OutOrStdout(), n, jsonFlag)
|
|
|
|
|
} else {
|
2026-03-26 12:48:47 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "failed to read temp file:", err)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
rootCmd.AddCommand(editCmd)
|
|
|
|
|
}
|