Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
Oliver Davies 2025-09-24 18:39:03 +01:00
parent 97445ca2cd
commit ba5f26056f
6 changed files with 17 additions and 18 deletions

View file

@ -20,15 +20,13 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files This application is a tool to generate the needed files
to quickly create a Cobra application.`, to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
zetDir := "/home/opdavies/Documents/zet"
if len(args) < 1 { if len(args) < 1 {
fmt.Println("No query") fmt.Println("No query")
os.Exit(1) os.Exit(1)
} }
zets := lib.SearchZets(zetDir, args[0]) zets := lib.SearchZets(args[0])
lib.ParseZetList(zets) lib.ParseZetList(zets)
}, },

View file

@ -17,9 +17,7 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files This application is a tool to generate the needed files
to quickly create a Cobra application.`, to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
zetDir := "/home/opdavies/Documents/zet" zets := lib.GetAllZets()
zets := lib.GetAllZets(zetDir)
lib.ParseZetList(zets) lib.ParseZetList(zets)
}, },

View file

@ -32,8 +32,6 @@ to quickly create a Cobra application.`,
os.Exit(1) os.Exit(1)
} }
zetDir := "/home/opdavies/Documents/zet" fmt.Println(lib.ViewZet(idInt))
fmt.Println(lib.ViewZet(zetDir, idInt))
}, },
} }

5
internal/lib/config.go Normal file
View file

@ -0,0 +1,5 @@
package lib
func GetZetDir() string {
return "/home/opdavies/Documents/zet"
}

View file

@ -2,8 +2,8 @@ package lib
import "os/exec" import "os/exec"
func execGitCommand(dir string, parts ...string) (string, error) { func execGitCommand(parts ...string) (string, error) {
args := append([]string{"-C", dir}, parts...) args := append([]string{"-C", GetZetDir()}, parts...)
command := exec.Command("git", args...) command := exec.Command("git", args...)
output, err := command.CombinedOutput() output, err := command.CombinedOutput()

View file

@ -12,8 +12,8 @@ import (
"strings" "strings"
) )
func GetAllZets(dir string) []int { func GetAllZets() []int {
zets, err := execGitCommand(dir, "ls-files") zets, err := execGitCommand("ls-files")
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -43,8 +43,8 @@ func GetAllZets(dir string) []int {
return sorted return sorted
} }
func SearchZets(dir string, query string) []int { func SearchZets(query string) []int {
zets, err := execGitCommand(dir, "grep", "-i", "--name-only", "--word-regex", query) zets, err := execGitCommand("grep", "-i", "--name-only", "--word-regex", query)
if err != nil { if err != nil {
fmt.Printf("No matches found for %s.\n", query) fmt.Printf("No matches found for %s.\n", query)
@ -93,8 +93,8 @@ func ParseZetList(ids []int) []string {
return lines return lines
} }
func ViewZet(zetDir string, id int) string { func ViewZet(id int) string {
zetPath := path.Join(zetDir, strconv.Itoa(id), "index.adoc") zetPath := path.Join(GetZetDir(), strconv.Itoa(id), "index.adoc")
return ViewFile(zetPath) return ViewFile(zetPath)
} }
@ -104,7 +104,7 @@ func getTitle(id int) string {
} }
func getTitleFromFile(filePath string) string { func getTitleFromFile(filePath string) string {
filePath = path.Join("/home/opdavies/Documents/zet", filePath) filePath = path.Join(GetZetDir(), filePath)
file, err := os.Open(filePath) file, err := os.Open(filePath)