Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
97445ca2cd
commit
ba5f26056f
6 changed files with 17 additions and 18 deletions
|
@ -20,15 +20,13 @@ 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) {
|
||||
zetDir := "/home/opdavies/Documents/zet"
|
||||
|
||||
if len(args) < 1 {
|
||||
fmt.Println("No query")
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
zets := lib.SearchZets(zetDir, args[0])
|
||||
zets := lib.SearchZets(args[0])
|
||||
|
||||
lib.ParseZetList(zets)
|
||||
},
|
||||
|
|
|
@ -17,9 +17,7 @@ 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) {
|
||||
zetDir := "/home/opdavies/Documents/zet"
|
||||
|
||||
zets := lib.GetAllZets(zetDir)
|
||||
zets := lib.GetAllZets()
|
||||
|
||||
lib.ParseZetList(zets)
|
||||
},
|
||||
|
|
|
@ -32,8 +32,6 @@ to quickly create a Cobra application.`,
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
zetDir := "/home/opdavies/Documents/zet"
|
||||
|
||||
fmt.Println(lib.ViewZet(zetDir, idInt))
|
||||
fmt.Println(lib.ViewZet(idInt))
|
||||
},
|
||||
}
|
||||
|
|
5
internal/lib/config.go
Normal file
5
internal/lib/config.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package lib
|
||||
|
||||
func GetZetDir() string {
|
||||
return "/home/opdavies/Documents/zet"
|
||||
}
|
|
@ -2,8 +2,8 @@ package lib
|
|||
|
||||
import "os/exec"
|
||||
|
||||
func execGitCommand(dir string, parts ...string) (string, error) {
|
||||
args := append([]string{"-C", dir}, parts...)
|
||||
func execGitCommand(parts ...string) (string, error) {
|
||||
args := append([]string{"-C", GetZetDir()}, parts...)
|
||||
command := exec.Command("git", args...)
|
||||
|
||||
output, err := command.CombinedOutput()
|
||||
|
|
|
@ -12,8 +12,8 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func GetAllZets(dir string) []int {
|
||||
zets, err := execGitCommand(dir, "ls-files")
|
||||
func GetAllZets() []int {
|
||||
zets, err := execGitCommand("ls-files")
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
@ -43,8 +43,8 @@ func GetAllZets(dir string) []int {
|
|||
return sorted
|
||||
}
|
||||
|
||||
func SearchZets(dir string, query string) []int {
|
||||
zets, err := execGitCommand(dir, "grep", "-i", "--name-only", "--word-regex", query)
|
||||
func SearchZets(query string) []int {
|
||||
zets, err := execGitCommand("grep", "-i", "--name-only", "--word-regex", query)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("No matches found for %s.\n", query)
|
||||
|
@ -93,8 +93,8 @@ func ParseZetList(ids []int) []string {
|
|||
return lines
|
||||
}
|
||||
|
||||
func ViewZet(zetDir string, id int) string {
|
||||
zetPath := path.Join(zetDir, strconv.Itoa(id), "index.adoc")
|
||||
func ViewZet(id int) string {
|
||||
zetPath := path.Join(GetZetDir(), strconv.Itoa(id), "index.adoc")
|
||||
|
||||
return ViewFile(zetPath)
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ func getTitle(id int) string {
|
|||
}
|
||||
|
||||
func getTitleFromFile(filePath string) string {
|
||||
filePath = path.Join("/home/opdavies/Documents/zet", filePath)
|
||||
filePath = path.Join(GetZetDir(), filePath)
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue