From 125aa05e0dc3ee9380317c04cbaa9006fd32a711 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 24 Sep 2025 19:35:56 +0100 Subject: [PATCH] Add `edit` command to edit a zettel Signed-off-by: Oliver Davies --- cmd/edit.go | 36 ++++++++++++++++++++++++++++++++++++ cmd/root.go | 1 + internal/lib/file.go | 15 +++++++++++++++ internal/lib/run.go | 22 ++++++++++++++++++++++ internal/lib/zet.go | 6 ++++++ 5 files changed, 80 insertions(+) create mode 100644 cmd/edit.go create mode 100644 internal/lib/run.go diff --git a/cmd/edit.go b/cmd/edit.go new file mode 100644 index 0000000..ea5fba5 --- /dev/null +++ b/cmd/edit.go @@ -0,0 +1,36 @@ +package cmd + +import ( + "fmt" + "os" + "strconv" + + "github.com/spf13/cobra" + + "code.oliverdavies.uk/opdavies/cmd-zet/internal/lib" +) + +var editCmd = &cobra.Command{ + Use: "edit", + Aliases: []string{"e"}, + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + Run: func(cmd *cobra.Command, args []string) { + if len(args) < 1 { + fmt.Println("Error: No id provided") + os.Exit(1) + } + + idInt, err := strconv.Atoi(args[0]) + if err != nil { + os.Exit(1) + } + + lib.EditZet(idInt) + }, +} diff --git a/cmd/root.go b/cmd/root.go index d94759f..6b68650 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -31,6 +31,7 @@ func Execute() { } func init() { + rootCmd.AddCommand(editCmd) rootCmd.AddCommand(findCmd) rootCmd.AddCommand(titlesCmd) rootCmd.AddCommand(viewCmd) diff --git a/internal/lib/file.go b/internal/lib/file.go index 254a95b..77781e7 100644 --- a/internal/lib/file.go +++ b/internal/lib/file.go @@ -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) diff --git a/internal/lib/run.go b/internal/lib/run.go new file mode 100644 index 0000000..48318ff --- /dev/null +++ b/internal/lib/run.go @@ -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() +} diff --git a/internal/lib/zet.go b/internal/lib/zet.go index 4ca8c9d..2e2f50d 100644 --- a/internal/lib/zet.go +++ b/internal/lib/zet.go @@ -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")