From a3202aaff3400e9257b0a69a7212e4022122ba38 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sat, 4 Oct 2025 00:20:04 +0100 Subject: [PATCH] Refactor Signed-off-by: Oliver Davies --- build | 2 +- cmd/root.go | 43 -------------------------- cmd/zet/main.go | 13 ++++++++ internal/ansi/{main.go => ansi.go} | 0 {cmd => internal/cli}/create.go | 8 +++-- {cmd => internal/cli}/edit.go | 8 +++-- {cmd => internal/cli}/find.go | 6 ++-- {cmd => internal/cli}/links.go | 8 +++-- internal/cli/root.go | 17 ++++++++++ {cmd => internal/cli}/titles.go | 6 ++-- {cmd => internal/cli}/view.go | 8 +++-- internal/config/{main.go => config.go} | 0 internal/file/{main.go => file.go} | 0 internal/fzf/{main.go => fzf.go} | 0 internal/git/{main.go => git.go} | 0 internal/json/{main.go => json.go} | 0 internal/zet/{main.go => zet.go} | 0 17 files changed, 63 insertions(+), 56 deletions(-) delete mode 100644 cmd/root.go create mode 100644 cmd/zet/main.go rename internal/ansi/{main.go => ansi.go} (100%) rename {cmd => internal/cli}/create.go (77%) rename {cmd => internal/cli}/edit.go (90%) rename {cmd => internal/cli}/find.go (90%) rename {cmd => internal/cli}/links.go (84%) create mode 100644 internal/cli/root.go rename {cmd => internal/cli}/titles.go (89%) rename {cmd => internal/cli}/view.go (91%) rename internal/config/{main.go => config.go} (100%) rename internal/file/{main.go => file.go} (100%) rename internal/fzf/{main.go => fzf.go} (100%) rename internal/git/{main.go => git.go} (100%) rename internal/json/{main.go => json.go} (100%) rename internal/zet/{main.go => zet.go} (100%) diff --git a/build b/build index af2e46b..e06b5a9 100755 --- a/build +++ b/build @@ -4,6 +4,6 @@ set -euo pipefail echo "Building..." -go build -o zet main.go +go build -o zet cmd/zet/main.go echo "Done." diff --git a/cmd/root.go b/cmd/root.go deleted file mode 100644 index f1dd18c..0000000 --- a/cmd/root.go +++ /dev/null @@ -1,43 +0,0 @@ -package cmd - -import ( - "os" - - "github.com/spf13/cobra" -) - -var jsonOutput bool - -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "zet", - Short: "Zettlekasten note manager", - Long: `Zettlekasten note manager`, - // Uncomment the following line if your bare application - // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { }, -} - -// Execute adds all child commands to the root command and sets flags appropriately. -// This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute() { - err := rootCmd.Execute() - if err != nil { - os.Exit(1) - } -} - -func init() { - rootCmd.AddCommand(createCmd) - rootCmd.AddCommand(editCmd) - rootCmd.AddCommand(findCmd) - rootCmd.AddCommand(linksCmd) - rootCmd.AddCommand(titlesCmd) - rootCmd.AddCommand(viewCmd) - - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. - - // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.zet.yaml)") -} diff --git a/cmd/zet/main.go b/cmd/zet/main.go new file mode 100644 index 0000000..bad633f --- /dev/null +++ b/cmd/zet/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "os" + + "code.oliverdavies.uk/opdavies/cmd-zet/internal/cli" +) + +func main() { + if err := cli.Execute(); err != nil { + os.Exit(1) + } +} diff --git a/internal/ansi/main.go b/internal/ansi/ansi.go similarity index 100% rename from internal/ansi/main.go rename to internal/ansi/ansi.go diff --git a/cmd/create.go b/internal/cli/create.go similarity index 77% rename from cmd/create.go rename to internal/cli/create.go index 164cbb7..a726786 100644 --- a/cmd/create.go +++ b/internal/cli/create.go @@ -1,4 +1,4 @@ -package cmd +package cli import ( "strings" @@ -12,7 +12,7 @@ var createCmd = &cobra.Command{ Aliases: []string{"c", "n", "new"}, Short: "Create a new zettel", Long: `zet create|new|c|n TITLE`, - Run: func(cmd *cobra.Command, args []string) { + Run: func(cli *cobra.Command, args []string) { title := "" if len(args) > 0 { @@ -22,3 +22,7 @@ var createCmd = &cobra.Command{ zet.CreateZet(title) }, } + +func init() { + rootCmd.AddCommand(createCmd) +} diff --git a/cmd/edit.go b/internal/cli/edit.go similarity index 90% rename from cmd/edit.go rename to internal/cli/edit.go index 178f48a..659e336 100644 --- a/cmd/edit.go +++ b/internal/cli/edit.go @@ -1,4 +1,4 @@ -package cmd +package cli import ( "fmt" @@ -19,7 +19,7 @@ var editCmd = &cobra.Command{ Long: `zet edit|e ID zet edit|e latest `, - Run: func(cmd *cobra.Command, args []string) { + Run: func(cli *cobra.Command, args []string) { if len(args) < 1 { fmt.Println("Error: No id provided") os.Exit(1) @@ -71,3 +71,7 @@ zet edit|e latest zet.EditZet(id) }, } + +func init() { + rootCmd.AddCommand(editCmd) +} diff --git a/cmd/find.go b/internal/cli/find.go similarity index 90% rename from cmd/find.go rename to internal/cli/find.go index 37ac52e..842fa9b 100644 --- a/cmd/find.go +++ b/internal/cli/find.go @@ -1,4 +1,4 @@ -package cmd +package cli import ( "fmt" @@ -18,7 +18,7 @@ var findCmd = &cobra.Command{ Long: `zet find QUERY If -j or --json is added, the results will be in JSON format.`, - Run: func(cmd *cobra.Command, args []string) { + Run: func(cli *cobra.Command, args []string) { if len(args) < 1 { fmt.Println("No query") @@ -44,5 +44,7 @@ If -j or --json is added, the results will be in JSON format.`, } func init() { + rootCmd.AddCommand(findCmd) + findCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format") } diff --git a/cmd/links.go b/internal/cli/links.go similarity index 84% rename from cmd/links.go rename to internal/cli/links.go index d6b0fd4..4b8a565 100644 --- a/cmd/links.go +++ b/internal/cli/links.go @@ -1,4 +1,4 @@ -package cmd +package cli import ( "fmt" @@ -14,7 +14,7 @@ var linksCmd = &cobra.Command{ Aliases: []string{"l"}, Short: "TODO", Long: `zet links QUERY`, - Run: func(cmd *cobra.Command, args []string) { + Run: func(cli *cobra.Command, args []string) { if len(args) < 1 { fmt.Println("No query") @@ -37,3 +37,7 @@ var linksCmd = &cobra.Command{ } }, } + +func init() { + rootCmd.AddCommand(linksCmd) +} diff --git a/internal/cli/root.go b/internal/cli/root.go new file mode 100644 index 0000000..86960a4 --- /dev/null +++ b/internal/cli/root.go @@ -0,0 +1,17 @@ +package cli + +import ( + "github.com/spf13/cobra" +) + +var jsonOutput bool + +var rootCmd = &cobra.Command{ + Use: "zet", + Short: "Zettlekasten note manager", + Long: `Zettlekasten note manager`, +} + +func Execute() error { + return rootCmd.Execute() +} diff --git a/cmd/titles.go b/internal/cli/titles.go similarity index 89% rename from cmd/titles.go rename to internal/cli/titles.go index 07e58ed..e336566 100644 --- a/cmd/titles.go +++ b/internal/cli/titles.go @@ -1,4 +1,4 @@ -package cmd +package cli import ( "fmt" @@ -17,7 +17,7 @@ var titlesCmd = &cobra.Command{ Long: `Print the IDs and titles of all zettels. If -j or --json is added, the results will be in JSON format.`, - Run: func(cmd *cobra.Command, args []string) { + Run: func(cli *cobra.Command, args []string) { zets := zet.GetAllZets() result := zet.ParseZetList(zets) @@ -37,5 +37,7 @@ If -j or --json is added, the results will be in JSON format.`, } func init() { + rootCmd.AddCommand(titlesCmd) + titlesCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output results in JSON format") } diff --git a/cmd/view.go b/internal/cli/view.go similarity index 91% rename from cmd/view.go rename to internal/cli/view.go index fde3798..e831fdc 100644 --- a/cmd/view.go +++ b/internal/cli/view.go @@ -1,4 +1,4 @@ -package cmd +package cli import ( "fmt" @@ -19,7 +19,7 @@ var viewCmd = &cobra.Command{ Long: `zet view|v ID zet view|v latest `, - Run: func(cmd *cobra.Command, args []string) { + Run: func(cli *cobra.Command, args []string) { if len(args) < 1 { fmt.Println("Error: No id provided") os.Exit(1) @@ -71,3 +71,7 @@ zet view|v latest fmt.Println(zet.ViewZet(id)) }, } + +func init() { + rootCmd.AddCommand(viewCmd) +} diff --git a/internal/config/main.go b/internal/config/config.go similarity index 100% rename from internal/config/main.go rename to internal/config/config.go diff --git a/internal/file/main.go b/internal/file/file.go similarity index 100% rename from internal/file/main.go rename to internal/file/file.go diff --git a/internal/fzf/main.go b/internal/fzf/fzf.go similarity index 100% rename from internal/fzf/main.go rename to internal/fzf/fzf.go diff --git a/internal/git/main.go b/internal/git/git.go similarity index 100% rename from internal/git/main.go rename to internal/git/git.go diff --git a/internal/json/main.go b/internal/json/json.go similarity index 100% rename from internal/json/main.go rename to internal/json/json.go diff --git a/internal/zet/main.go b/internal/zet/zet.go similarity index 100% rename from internal/zet/main.go rename to internal/zet/zet.go