From b2f7f7af7f05f877324947fc3db6a5e11a476cfc Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 26 Sep 2025 09:33:16 +0100 Subject: [PATCH] Create a new zettel with a given title Signed-off-by: Oliver Davies --- cmd/create.go | 32 ++++++++++++++++++++++++++++++++ cmd/root.go | 1 + internal/lib/zet.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 cmd/create.go diff --git a/cmd/create.go b/cmd/create.go new file mode 100644 index 0000000..0df2c13 --- /dev/null +++ b/cmd/create.go @@ -0,0 +1,32 @@ +package cmd + +import ( + "strings" + + "github.com/spf13/cobra" + + "code.oliverdavies.uk/opdavies/cmd-zet/internal/lib" +) + +var createCmd = &cobra.Command{ + Use: "create", + 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) { + title := "" + if len(args) > 0 { + title = strings.Join(args, " ") + } + + lib.CreateZet(title) + + // TODO: Edit the file + // TODO: Commit the changes + }, +} diff --git a/cmd/root.go b/cmd/root.go index 6b68650..a3d98ad 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -31,6 +31,7 @@ func Execute() { } func init() { + rootCmd.AddCommand(createCmd) rootCmd.AddCommand(editCmd) rootCmd.AddCommand(findCmd) rootCmd.AddCommand(titlesCmd) diff --git a/internal/lib/zet.go b/internal/lib/zet.go index 766240e..b0e4f92 100644 --- a/internal/lib/zet.go +++ b/internal/lib/zet.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "os/exec" "path" "regexp" "sort" @@ -12,6 +13,32 @@ import ( "strings" ) +func CreateZet(title string) { + zid := newZid() + + fmt.Println(title, zid) + + path := path.Join(GetZetDir(), strconv.Itoa(zid)) + + os.Mkdir(path, 0750) + + filePath := fmt.Sprintf("%s/index.adoc", path) + + file, err := os.Create(filePath) + + if err != nil { + log.Fatal(err) + } + + defer file.Close() + + _, err = fmt.Fprintf(file, "= %s", title) + + if err != nil { + log.Fatal(err) + } +} + func EditZet(id int) { zetPath := path.Join(GetZetDir(), strconv.Itoa(id), "index.adoc") @@ -139,6 +166,24 @@ func getTitleFromFile(filePath string) string { return "" } +func newZid() int { + cmd := exec.Command("ls", GetZetDir()) + output, _ := cmd.CombinedOutput() + + zets := strings.Split(string(output), "\n") + + var zetCount int + + for _, zet := range zets { + num, err := strconv.Atoi(zet) + if err == nil && num > zetCount { + zetCount = num + } + } + + return zetCount + 1 +} + func onSave(id int) { title := getTitle(id)