refactor: replace explicit fields with tag-based property system
This commit is contained in:
32
cmd/edit.go
32
cmd/edit.go
@@ -11,21 +11,19 @@ import (
|
||||
)
|
||||
|
||||
var editCmd = &cobra.Command{
|
||||
Use: "edit <id>",
|
||||
Short: "Edit node content in $EDITOR",
|
||||
Args: cobra.ExactArgs(1),
|
||||
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)
|
||||
n, err := d.NodeByID(args[0])
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "node not found:", id)
|
||||
fmt.Fprintln(os.Stderr, "node not found:", args[0])
|
||||
return
|
||||
}
|
||||
|
||||
tmp, err := os.CreateTemp("", "ax-*.md")
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "failed to create temp file:", err)
|
||||
@@ -40,24 +38,22 @@ var editCmd = &cobra.Command{
|
||||
editor = "vi"
|
||||
}
|
||||
c := exec.Command(editor, tmp.Name())
|
||||
c.Stdin = os.Stdin
|
||||
c.Stdout = os.Stdout
|
||||
c.Stderr = os.Stderr
|
||||
c.Stdin, c.Stdout, c.Stderr = os.Stdin, os.Stdout, 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 {
|
||||
|
||||
if content, err := os.ReadFile(tmp.Name()); err == nil {
|
||||
if err := d.UpdateNode(args[0], db.UpdateParams{Content: string(content)}); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "failed to update:", err)
|
||||
return
|
||||
}
|
||||
n, _ = d.NodeByID(args[0])
|
||||
output.PrintNode(cmd.OutOrStdout(), n, jsonFlag)
|
||||
} else {
|
||||
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)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user