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

36
cmd/edit.go Normal file
View file

@ -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)
},
}

View file

@ -31,6 +31,7 @@ func Execute() {
}
func init() {
rootCmd.AddCommand(editCmd)
rootCmd.AddCommand(findCmd)
rootCmd.AddCommand(titlesCmd)
rootCmd.AddCommand(viewCmd)

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")