This commit is contained in:
2026-03-26 12:48:47 +00:00
commit 2d4cff717b
21 changed files with 1835 additions and 0 deletions

66
cmd/edit.go Normal file
View File

@@ -0,0 +1,66 @@
package cmd
import (
"axolotl/db"
"axolotl/output"
"fmt"
"os"
"os/exec"
"github.com/spf13/cobra"
)
var editCmd = &cobra.Command{
Use: "edit <id>",
Short: "Edit node content in $EDITOR",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
d, err := db.GetDB()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
id := args[0]
n, err := d.NodeByID(id)
if err != nil {
fmt.Fprintln(os.Stderr, "node not found:", id)
return
}
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())
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
fmt.Fprintln(os.Stderr, "editor failed:", err)
return
}
content, err := os.ReadFile(tmp.Name())
if err != nil {
fmt.Fprintln(os.Stderr, "failed to read temp file:", err)
return
}
if err := d.UpdateNode(id, db.UpdateParams{Content: string(content)}); err != nil {
fmt.Fprintln(os.Stderr, "failed to update:", err)
return
}
n, _ = d.NodeByID(id)
output.PrintNode(cmd.OutOrStdout(), n, jsonFlag)
},
}
func init() {
rootCmd.AddCommand(editCmd)
}