Add view
command
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
f9a05ab5d9
commit
0a6e2b075c
4 changed files with 68 additions and 5 deletions
|
@ -1,6 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
|
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
|
||||||
|
|
||||||
*/
|
*/
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
|
@ -10,8 +9,6 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// rootCmd represents the base command when called without any subcommands
|
// rootCmd represents the base command when called without any subcommands
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "zet",
|
Use: "zet",
|
||||||
|
@ -38,6 +35,7 @@ func Execute() {
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(titlesCmd)
|
rootCmd.AddCommand(titlesCmd)
|
||||||
|
rootCmd.AddCommand(viewCmd)
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
// Here you will define your flags and configuration settings.
|
||||||
// Cobra supports persistent flags, which, if defined here,
|
// 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)")
|
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.zet.yaml)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
39
cmd/view.go
Normal file
39
cmd/view.go
Normal file
|
@ -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))
|
||||||
|
},
|
||||||
|
}
|
22
internal/lib/file.go
Normal file
22
internal/lib/file.go
Normal file
|
@ -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)
|
||||||
|
}
|
|
@ -60,6 +60,12 @@ func ParseZetList(ids []int) []string {
|
||||||
return lines
|
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 {
|
func getTitle(id int) string {
|
||||||
return getTitleFromFile(path.Join(strconv.Itoa(id), "index.adoc"))
|
return getTitleFromFile(path.Join(strconv.Itoa(id), "index.adoc"))
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue