From 0a6e2b075ccd717f8abc34e7ec2080dac77f090e Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 24 Sep 2025 08:30:00 +0100 Subject: [PATCH] Add `view` command Signed-off-by: Oliver Davies --- cmd/root.go | 6 +---- cmd/view.go | 39 +++++++++++++++++++++++++++++++++ internal/lib/file.go | 22 +++++++++++++++++++ internal/lib/{lib.go => zet.go} | 6 +++++ 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 cmd/view.go create mode 100644 internal/lib/file.go rename internal/lib/{lib.go => zet.go} (91%) diff --git a/cmd/root.go b/cmd/root.go index b885b02..54a606e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,6 +1,5 @@ /* Copyright © 2025 NAME HERE - */ package cmd @@ -10,8 +9,6 @@ import ( "github.com/spf13/cobra" ) - - // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "zet", @@ -38,6 +35,7 @@ func Execute() { func init() { rootCmd.AddCommand(titlesCmd) + rootCmd.AddCommand(viewCmd) // Here you will define your flags and configuration settings. // Cobra supports persistent flags, which, if defined here, @@ -45,5 +43,3 @@ func init() { // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.zet.yaml)") } - - diff --git a/cmd/view.go b/cmd/view.go new file mode 100644 index 0000000..4ece1d9 --- /dev/null +++ b/cmd/view.go @@ -0,0 +1,39 @@ +package cmd + +import ( + "fmt" + "os" + "strconv" + + "code.oliverdavies.uk/opdavies/cmd-zet/internal/lib" + "github.com/spf13/cobra" + // "code.oliverdavies.uk/opdavies/cmd-zet/internal/lib" +) + +var viewCmd = &cobra.Command{ + Use: "view", + Aliases: []string{"v"}, + 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) + } + + zetDir := "/home/opdavies/Documents/zet" + + fmt.Println(lib.ViewZet(zetDir, idInt)) + }, +} diff --git a/internal/lib/file.go b/internal/lib/file.go new file mode 100644 index 0000000..254a95b --- /dev/null +++ b/internal/lib/file.go @@ -0,0 +1,22 @@ +package lib + +import ( + "fmt" + "os" +) + +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) + os.Exit(1) + } + + content, err := os.ReadFile(filePath) + + if err != nil { + fmt.Println("Error opening the file:", err) + os.Exit(1) + } + + return string(content) +} diff --git a/internal/lib/lib.go b/internal/lib/zet.go similarity index 91% rename from internal/lib/lib.go rename to internal/lib/zet.go index a10699c..2273ccb 100644 --- a/internal/lib/lib.go +++ b/internal/lib/zet.go @@ -60,6 +60,12 @@ func ParseZetList(ids []int) []string { return lines } +func ViewZet(zetDir string, id int) string { + zetPath := path.Join(zetDir, strconv.Itoa(id), "index.adoc") + + return ViewFile(zetPath) +} + func getTitle(id int) string { return getTitleFromFile(path.Join(strconv.Itoa(id), "index.adoc")) }