Add edit command to edit a zettel

Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
Oliver Davies 2025-09-24 19:35:56 +01:00
parent 1986b9deca
commit 125aa05e0d
5 changed files with 80 additions and 0 deletions

View file

@ -5,6 +5,21 @@ import (
"os"
)
func EditFile(filePath string) {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
fmt.Printf("Error: The file for path '%s' was not found\n", filePath)
os.Exit(1)
}
editor := os.Getenv("EDITOR")
err := Exec(editor, filePath)
if err != nil {
fmt.Println(err)
}
}
func ViewFile(filePath string) string {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
fmt.Printf("Error: The file for path '%s' was not found\n", filePath)

22
internal/lib/run.go Normal file
View file

@ -0,0 +1,22 @@
package lib
import (
"fmt"
"os"
"os/exec"
)
func Exec(args ...string) error {
path, err := exec.LookPath(args[0])
if err != nil {
fmt.Println(err)
}
cmd := exec.Command(path, args[1:]...)
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
return cmd.Run()
}

View file

@ -12,6 +12,12 @@ import (
"strings"
)
func EditZet(id int) {
zetPath := path.Join(GetZetDir(), strconv.Itoa(id), "index.adoc")
EditFile(zetPath)
}
func GetAllZets() []int {
zets, err := execGitCommand("ls-files")