Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
a3202aaff3
commit
948872b933
7 changed files with 206 additions and 150 deletions
60
internal/zet/create.go
Normal file
60
internal/zet/create.go
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
package zet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/config"
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/file"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateZet(title string) {
|
||||||
|
zid := newZid()
|
||||||
|
|
||||||
|
path := path.Join(config.GetZetDir(), strconv.Itoa(zid))
|
||||||
|
|
||||||
|
os.Mkdir(path, 0750)
|
||||||
|
|
||||||
|
filePath := fmt.Sprintf("%s/index.adoc", path)
|
||||||
|
|
||||||
|
f, err := os.Create(filePath)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
_, err = fmt.Fprintf(f, "= %s", title)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
file.Edit(filePath)
|
||||||
|
|
||||||
|
onSave(zid)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newZid() int {
|
||||||
|
cmd := exec.Command("ls", config.GetZetDir())
|
||||||
|
output, _ := cmd.CombinedOutput()
|
||||||
|
|
||||||
|
zets := strings.Split(string(output), "\n")
|
||||||
|
|
||||||
|
var zetCount int
|
||||||
|
|
||||||
|
for _, zet := range zets {
|
||||||
|
num, err := strconv.Atoi(zet)
|
||||||
|
if err == nil && num > zetCount {
|
||||||
|
zetCount = num
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return zetCount + 1
|
||||||
|
}
|
18
internal/zet/edit.go
Normal file
18
internal/zet/edit.go
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
package zet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/config"
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/file"
|
||||||
|
)
|
||||||
|
|
||||||
|
func EditZet(id int) {
|
||||||
|
zetPath := path.Join(config.GetZetDir(), strconv.Itoa(id), "index.adoc")
|
||||||
|
|
||||||
|
file.Edit(zetPath)
|
||||||
|
|
||||||
|
onSave(id)
|
||||||
|
}
|
||||||
|
|
48
internal/zet/get.go
Normal file
48
internal/zet/get.go
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
package zet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/git"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetAllZets() []int {
|
||||||
|
zets, err := git.ExecGitCommand("ls-files")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
re := regexp.MustCompile(`[0-9]+`)
|
||||||
|
matches := re.FindAllString(zets, -1)
|
||||||
|
|
||||||
|
sort.Strings(matches)
|
||||||
|
|
||||||
|
ids := make(map[int]struct{})
|
||||||
|
for _, id := range matches {
|
||||||
|
num, err := strconv.Atoi(id)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
ids[num] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var sorted []int
|
||||||
|
for num := range ids {
|
||||||
|
sorted = append(sorted, num)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Ints(sorted)
|
||||||
|
|
||||||
|
return sorted
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetLatestZet() int {
|
||||||
|
z := GetAllZets()
|
||||||
|
|
||||||
|
return z[len(z)-1]
|
||||||
|
}
|
||||||
|
|
21
internal/zet/parse.go
Normal file
21
internal/zet/parse.go
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
package zet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ParseZetList(ids []int) []string {
|
||||||
|
var lines []string
|
||||||
|
|
||||||
|
green := "\033[32m"
|
||||||
|
reset := "\033[0m"
|
||||||
|
|
||||||
|
for _, num := range ids {
|
||||||
|
line := fmt.Sprintf("%s%s%s %s", green, strconv.Itoa(num), reset, getTitle(num))
|
||||||
|
|
||||||
|
lines = append(lines, line)
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
|
}
|
44
internal/zet/search.go
Normal file
44
internal/zet/search.go
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
package zet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/git"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SearchZets(query string) []int {
|
||||||
|
zets, err := git.ExecGitCommand("grep", "-i", "--name-only", "--word-regex", query)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("No matches found for %s.\n", query)
|
||||||
|
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
re := regexp.MustCompile(`[0-9]+`)
|
||||||
|
matches := re.FindAllString(zets, -1)
|
||||||
|
|
||||||
|
sort.Strings(matches)
|
||||||
|
|
||||||
|
ids := make(map[int]struct{})
|
||||||
|
for _, id := range matches {
|
||||||
|
num, err := strconv.Atoi(id)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
ids[num] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var sorted []int
|
||||||
|
for num := range ids {
|
||||||
|
sorted = append(sorted, num)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Ints(sorted)
|
||||||
|
|
||||||
|
return sorted
|
||||||
|
}
|
15
internal/zet/view.go
Normal file
15
internal/zet/view.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package zet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/config"
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/file"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ViewZet(id int) string {
|
||||||
|
zetPath := path.Join(config.GetZetDir(), strconv.Itoa(id), "index.adoc")
|
||||||
|
|
||||||
|
return file.View(zetPath)
|
||||||
|
}
|
|
@ -3,147 +3,15 @@ package zet
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/config"
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/config"
|
||||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/file"
|
|
||||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/git"
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/git"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateZet(title string) {
|
|
||||||
zid := newZid()
|
|
||||||
|
|
||||||
path := path.Join(config.GetZetDir(), strconv.Itoa(zid))
|
|
||||||
|
|
||||||
os.Mkdir(path, 0750)
|
|
||||||
|
|
||||||
filePath := fmt.Sprintf("%s/index.adoc", path)
|
|
||||||
|
|
||||||
f, err := os.Create(filePath)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
_, err = fmt.Fprintf(f, "= %s", title)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
file.Edit(filePath)
|
|
||||||
|
|
||||||
onSave(zid)
|
|
||||||
}
|
|
||||||
|
|
||||||
func EditZet(id int) {
|
|
||||||
zetPath := path.Join(config.GetZetDir(), strconv.Itoa(id), "index.adoc")
|
|
||||||
|
|
||||||
file.Edit(zetPath)
|
|
||||||
|
|
||||||
onSave(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAllZets() []int {
|
|
||||||
zets, err := git.ExecGitCommand("ls-files")
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
re := regexp.MustCompile(`[0-9]+`)
|
|
||||||
matches := re.FindAllString(zets, -1)
|
|
||||||
|
|
||||||
sort.Strings(matches)
|
|
||||||
|
|
||||||
ids := make(map[int]struct{})
|
|
||||||
for _, id := range matches {
|
|
||||||
num, err := strconv.Atoi(id)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
ids[num] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var sorted []int
|
|
||||||
for num := range ids {
|
|
||||||
sorted = append(sorted, num)
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Ints(sorted)
|
|
||||||
|
|
||||||
return sorted
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetLatestZet() int {
|
|
||||||
z := GetAllZets()
|
|
||||||
|
|
||||||
return z[len(z)-1]
|
|
||||||
}
|
|
||||||
|
|
||||||
func SearchZets(query string) []int {
|
|
||||||
zets, err := git.ExecGitCommand("grep", "-i", "--name-only", "--word-regex", query)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("No matches found for %s.\n", query)
|
|
||||||
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
re := regexp.MustCompile(`[0-9]+`)
|
|
||||||
matches := re.FindAllString(zets, -1)
|
|
||||||
|
|
||||||
sort.Strings(matches)
|
|
||||||
|
|
||||||
ids := make(map[int]struct{})
|
|
||||||
for _, id := range matches {
|
|
||||||
num, err := strconv.Atoi(id)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
ids[num] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var sorted []int
|
|
||||||
for num := range ids {
|
|
||||||
sorted = append(sorted, num)
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Ints(sorted)
|
|
||||||
|
|
||||||
return sorted
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseZetList(ids []int) []string {
|
|
||||||
var lines []string
|
|
||||||
|
|
||||||
green := "\033[32m"
|
|
||||||
reset := "\033[0m"
|
|
||||||
|
|
||||||
for _, num := range ids {
|
|
||||||
line := fmt.Sprintf("%s%s%s %s", green, strconv.Itoa(num), reset, getTitle(num))
|
|
||||||
|
|
||||||
lines = append(lines, line)
|
|
||||||
}
|
|
||||||
|
|
||||||
return lines
|
|
||||||
}
|
|
||||||
|
|
||||||
func ViewZet(id int) string {
|
|
||||||
zetPath := path.Join(config.GetZetDir(), strconv.Itoa(id), "index.adoc")
|
|
||||||
|
|
||||||
return file.View(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"))
|
||||||
}
|
}
|
||||||
|
@ -176,24 +44,6 @@ func getTitleFromFile(filePath string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func newZid() int {
|
|
||||||
cmd := exec.Command("ls", config.GetZetDir())
|
|
||||||
output, _ := cmd.CombinedOutput()
|
|
||||||
|
|
||||||
zets := strings.Split(string(output), "\n")
|
|
||||||
|
|
||||||
var zetCount int
|
|
||||||
|
|
||||||
for _, zet := range zets {
|
|
||||||
num, err := strconv.Atoi(zet)
|
|
||||||
if err == nil && num > zetCount {
|
|
||||||
zetCount = num
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return zetCount + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func onSave(id int) {
|
func onSave(id int) {
|
||||||
title := getTitle(id)
|
title := getTitle(id)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue