Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
a4901b9ae7
commit
d27eca0a07
13 changed files with 71 additions and 57 deletions
|
@ -1,4 +1,4 @@
|
|||
package lib
|
||||
package ansi
|
||||
|
||||
import "regexp"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package lib
|
||||
package config
|
||||
|
||||
import "os"
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
package lib
|
||||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/lib"
|
||||
)
|
||||
|
||||
func EditFile(filePath string) {
|
||||
|
@ -13,7 +15,7 @@ func EditFile(filePath string) {
|
|||
|
||||
editor := os.Getenv("EDITOR")
|
||||
|
||||
err := Exec(editor, filePath)
|
||||
err := lib.Exec(editor, filePath)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
|
@ -1,4 +1,4 @@
|
|||
package lib
|
||||
package fzf
|
||||
|
||||
import (
|
||||
"bytes"
|
|
@ -1,9 +1,11 @@
|
|||
package lib
|
||||
package git
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/config"
|
||||
)
|
||||
|
||||
func CommitZettel(id int, title string) {
|
||||
|
@ -14,8 +16,8 @@ func CommitZettel(id int, title string) {
|
|||
runGitCommand("push")
|
||||
}
|
||||
|
||||
func execGitCommand(parts ...string) (string, error) {
|
||||
args := append([]string{"-C", GetZetDir()}, parts...)
|
||||
func ExecGitCommand(parts ...string) (string, error) {
|
||||
args := append([]string{"-C", config.GetZetDir()}, parts...)
|
||||
command := exec.Command("git", args...)
|
||||
|
||||
output, err := command.CombinedOutput()
|
||||
|
@ -24,7 +26,7 @@ func execGitCommand(parts ...string) (string, error) {
|
|||
}
|
||||
|
||||
func runGitCommand(parts ...string) {
|
||||
args := append([]string{"-C", GetZetDir()}, parts...)
|
||||
args := append([]string{"-C", config.GetZetDir()}, parts...)
|
||||
command := exec.Command("git", args...)
|
||||
|
||||
command.Stderr = os.Stderr
|
|
@ -1,10 +1,12 @@
|
|||
package lib
|
||||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/ansi"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
|
@ -16,7 +18,7 @@ func AsJSON(zets []string) (string, error) {
|
|||
var items []Item
|
||||
|
||||
for _, entry := range zets {
|
||||
cleanEntry := StripANSI(entry)
|
||||
cleanEntry := ansi.StripANSI(entry)
|
||||
parts := strings.SplitN(cleanEntry, " ", 2)
|
||||
|
||||
id, _ := strconv.Atoi(parts[0])
|
|
@ -1,4 +1,4 @@
|
|||
package lib
|
||||
package zet
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
|
@ -11,46 +11,50 @@ import (
|
|||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/config"
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/file"
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/git"
|
||||
)
|
||||
|
||||
func CreateZet(title string) {
|
||||
zid := newZid()
|
||||
|
||||
path := path.Join(GetZetDir(), strconv.Itoa(zid))
|
||||
path := path.Join(config.GetZetDir(), strconv.Itoa(zid))
|
||||
|
||||
os.Mkdir(path, 0750)
|
||||
|
||||
filePath := fmt.Sprintf("%s/index.adoc", path)
|
||||
|
||||
file, err := os.Create(filePath)
|
||||
f, err := os.Create(filePath)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
defer f.Close()
|
||||
|
||||
_, err = fmt.Fprintf(file, "= %s", title)
|
||||
_, err = fmt.Fprintf(f, "= %s", title)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
EditFile(filePath)
|
||||
file.EditFile(filePath)
|
||||
|
||||
onSave(zid)
|
||||
}
|
||||
|
||||
func EditZet(id int) {
|
||||
zetPath := path.Join(GetZetDir(), strconv.Itoa(id), "index.adoc")
|
||||
zetPath := path.Join(config.GetZetDir(), strconv.Itoa(id), "index.adoc")
|
||||
|
||||
EditFile(zetPath)
|
||||
file.EditFile(zetPath)
|
||||
|
||||
onSave(id)
|
||||
}
|
||||
|
||||
func GetAllZets() []int {
|
||||
zets, err := execGitCommand("ls-files")
|
||||
zets, err := git.ExecGitCommand("ls-files")
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
@ -87,7 +91,7 @@ func GetLatestZet() int {
|
|||
}
|
||||
|
||||
func SearchZets(query string) []int {
|
||||
zets, err := execGitCommand("grep", "-i", "--name-only", "--word-regex", query)
|
||||
zets, err := git.ExecGitCommand("grep", "-i", "--name-only", "--word-regex", query)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("No matches found for %s.\n", query)
|
||||
|
@ -135,9 +139,9 @@ func ParseZetList(ids []int) []string {
|
|||
}
|
||||
|
||||
func ViewZet(id int) string {
|
||||
zetPath := path.Join(GetZetDir(), strconv.Itoa(id), "index.adoc")
|
||||
zetPath := path.Join(config.GetZetDir(), strconv.Itoa(id), "index.adoc")
|
||||
|
||||
return ViewFile(zetPath)
|
||||
return file.ViewFile(zetPath)
|
||||
}
|
||||
|
||||
func getTitle(id int) string {
|
||||
|
@ -145,7 +149,7 @@ func getTitle(id int) string {
|
|||
}
|
||||
|
||||
func getTitleFromFile(filePath string) string {
|
||||
filePath = path.Join(GetZetDir(), filePath)
|
||||
filePath = path.Join(config.GetZetDir(), filePath)
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
|
||||
|
@ -173,7 +177,7 @@ func getTitleFromFile(filePath string) string {
|
|||
}
|
||||
|
||||
func newZid() int {
|
||||
cmd := exec.Command("ls", GetZetDir())
|
||||
cmd := exec.Command("ls", config.GetZetDir())
|
||||
output, _ := cmd.CombinedOutput()
|
||||
|
||||
zets := strings.Split(string(output), "\n")
|
||||
|
@ -193,7 +197,7 @@ func newZid() int {
|
|||
func onSave(id int) {
|
||||
title := getTitle(id)
|
||||
|
||||
CommitZettel(id, title)
|
||||
git.CommitZettel(id, title)
|
||||
|
||||
// TODO: Or delete the file if empty.
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue