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
|
@ -5,7 +5,7 @@ import (
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/lib"
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/zet"
|
||||||
)
|
)
|
||||||
|
|
||||||
var createCmd = &cobra.Command{
|
var createCmd = &cobra.Command{
|
||||||
|
@ -20,6 +20,6 @@ var createCmd = &cobra.Command{
|
||||||
title = strings.Join(args, " ")
|
title = strings.Join(args, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
lib.CreateZet(title)
|
zet.CreateZet(title)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
19
cmd/edit.go
19
cmd/edit.go
|
@ -9,7 +9,8 @@ import (
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/lib"
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/fzf"
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/zet"
|
||||||
)
|
)
|
||||||
|
|
||||||
var editCmd = &cobra.Command{
|
var editCmd = &cobra.Command{
|
||||||
|
@ -29,32 +30,32 @@ zet edit|e latest
|
||||||
var query string
|
var query string
|
||||||
|
|
||||||
if args[0] == "latest" {
|
if args[0] == "latest" {
|
||||||
id = lib.GetLatestZet()
|
id = zet.GetLatestZet()
|
||||||
|
|
||||||
lib.EditZet(id)
|
zet.EditZet(id)
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if id, err := strconv.Atoi(args[0]); err == nil {
|
if id, err := strconv.Atoi(args[0]); err == nil {
|
||||||
lib.EditZet(id)
|
zet.EditZet(id)
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
query = args[0]
|
query = args[0]
|
||||||
|
|
||||||
ids := lib.SearchZets(query)
|
ids := zet.SearchZets(query)
|
||||||
|
|
||||||
if len(ids) == 1 {
|
if len(ids) == 1 {
|
||||||
lib.EditZet(ids[0])
|
zet.EditZet(ids[0])
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
zets := lib.ParseZetList(ids)
|
zets := zet.ParseZetList(ids)
|
||||||
|
|
||||||
selected, err := lib.SelectWithFzf(zets)
|
selected, err := fzf.SelectWithFzf(zets)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("No zet selected.")
|
log.Fatalf("No zet selected.")
|
||||||
|
@ -68,6 +69,6 @@ zet edit|e latest
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
lib.EditZet(id)
|
zet.EditZet(id)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,8 @@ import (
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/lib"
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/json"
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/zet"
|
||||||
)
|
)
|
||||||
|
|
||||||
var findCmd = &cobra.Command{
|
var findCmd = &cobra.Command{
|
||||||
|
@ -25,12 +26,12 @@ If -j or --json is added, the results will be in JSON format.`,
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
zets := lib.SearchZets(args[0])
|
zets := zet.SearchZets(args[0])
|
||||||
|
|
||||||
result := lib.ParseZetList(zets)
|
result := zet.ParseZetList(zets)
|
||||||
|
|
||||||
if jsonOutput {
|
if jsonOutput {
|
||||||
json, err := lib.AsJSON(result)
|
json, err := json.AsJSON(result)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/lib"
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/zet"
|
||||||
)
|
)
|
||||||
|
|
||||||
var linksCmd = &cobra.Command{
|
var linksCmd = &cobra.Command{
|
||||||
|
@ -22,9 +22,9 @@ var linksCmd = &cobra.Command{
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
zets := lib.SearchZets(args[0])
|
zets := zet.SearchZets(args[0])
|
||||||
|
|
||||||
lines := lib.ParseZetList(zets)
|
lines := zet.ParseZetList(zets)
|
||||||
|
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
line = strings.TrimSpace(line)
|
line = strings.TrimSpace(line)
|
||||||
|
|
|
@ -7,7 +7,8 @@ import (
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/lib"
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/json"
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/zet"
|
||||||
)
|
)
|
||||||
|
|
||||||
var titlesCmd = &cobra.Command{
|
var titlesCmd = &cobra.Command{
|
||||||
|
@ -18,12 +19,12 @@ var titlesCmd = &cobra.Command{
|
||||||
|
|
||||||
If -j or --json is added, the results will be in JSON format.`,
|
If -j or --json is added, the results will be in JSON format.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
zets := lib.GetAllZets()
|
zets := zet.GetAllZets()
|
||||||
|
|
||||||
result := lib.ParseZetList(zets)
|
result := zet.ParseZetList(zets)
|
||||||
|
|
||||||
if jsonOutput {
|
if jsonOutput {
|
||||||
json, err := lib.AsJSON(result)
|
json, err := json.AsJSON(result)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
19
cmd/view.go
19
cmd/view.go
|
@ -9,7 +9,8 @@ import (
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/lib"
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/fzf"
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/zet"
|
||||||
)
|
)
|
||||||
|
|
||||||
var viewCmd = &cobra.Command{
|
var viewCmd = &cobra.Command{
|
||||||
|
@ -29,32 +30,32 @@ zet view|v latest
|
||||||
var query string
|
var query string
|
||||||
|
|
||||||
if args[0] == "latest" {
|
if args[0] == "latest" {
|
||||||
id = lib.GetLatestZet()
|
id = zet.GetLatestZet()
|
||||||
|
|
||||||
fmt.Println(lib.ViewZet(id))
|
fmt.Println(zet.ViewZet(id))
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if id, err := strconv.Atoi(args[0]); err == nil {
|
if id, err := strconv.Atoi(args[0]); err == nil {
|
||||||
fmt.Println(lib.ViewZet(id))
|
fmt.Println(zet.ViewZet(id))
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
query = args[0]
|
query = args[0]
|
||||||
|
|
||||||
ids := lib.SearchZets(query)
|
ids := zet.SearchZets(query)
|
||||||
|
|
||||||
if len(ids) == 1 {
|
if len(ids) == 1 {
|
||||||
fmt.Println(lib.ViewZet(ids[0]))
|
fmt.Println(zet.ViewZet(ids[0]))
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
zets := lib.ParseZetList(ids)
|
zets := zet.ParseZetList(ids)
|
||||||
|
|
||||||
selected, err := lib.SelectWithFzf(zets)
|
selected, err := fzf.SelectWithFzf(zets)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("No zet selected.")
|
log.Fatalf("No zet selected.")
|
||||||
|
@ -68,6 +69,6 @@ zet view|v latest
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(lib.ViewZet(id))
|
fmt.Println(zet.ViewZet(id))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package lib
|
package ansi
|
||||||
|
|
||||||
import "regexp"
|
import "regexp"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package lib
|
package config
|
||||||
|
|
||||||
import "os"
|
import "os"
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package lib
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/lib"
|
||||||
)
|
)
|
||||||
|
|
||||||
func EditFile(filePath string) {
|
func EditFile(filePath string) {
|
||||||
|
@ -13,7 +15,7 @@ func EditFile(filePath string) {
|
||||||
|
|
||||||
editor := os.Getenv("EDITOR")
|
editor := os.Getenv("EDITOR")
|
||||||
|
|
||||||
err := Exec(editor, filePath)
|
err := lib.Exec(editor, filePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
|
@ -1,4 +1,4 @@
|
||||||
package lib
|
package fzf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
|
@ -1,9 +1,11 @@
|
||||||
package lib
|
package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CommitZettel(id int, title string) {
|
func CommitZettel(id int, title string) {
|
||||||
|
@ -14,8 +16,8 @@ func CommitZettel(id int, title string) {
|
||||||
runGitCommand("push")
|
runGitCommand("push")
|
||||||
}
|
}
|
||||||
|
|
||||||
func execGitCommand(parts ...string) (string, error) {
|
func ExecGitCommand(parts ...string) (string, error) {
|
||||||
args := append([]string{"-C", GetZetDir()}, parts...)
|
args := append([]string{"-C", config.GetZetDir()}, parts...)
|
||||||
command := exec.Command("git", args...)
|
command := exec.Command("git", args...)
|
||||||
|
|
||||||
output, err := command.CombinedOutput()
|
output, err := command.CombinedOutput()
|
||||||
|
@ -24,7 +26,7 @@ func execGitCommand(parts ...string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func runGitCommand(parts ...string) {
|
func runGitCommand(parts ...string) {
|
||||||
args := append([]string{"-C", GetZetDir()}, parts...)
|
args := append([]string{"-C", config.GetZetDir()}, parts...)
|
||||||
command := exec.Command("git", args...)
|
command := exec.Command("git", args...)
|
||||||
|
|
||||||
command.Stderr = os.Stderr
|
command.Stderr = os.Stderr
|
|
@ -1,10 +1,12 @@
|
||||||
package lib
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/ansi"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Item struct {
|
type Item struct {
|
||||||
|
@ -16,7 +18,7 @@ func AsJSON(zets []string) (string, error) {
|
||||||
var items []Item
|
var items []Item
|
||||||
|
|
||||||
for _, entry := range zets {
|
for _, entry := range zets {
|
||||||
cleanEntry := StripANSI(entry)
|
cleanEntry := ansi.StripANSI(entry)
|
||||||
parts := strings.SplitN(cleanEntry, " ", 2)
|
parts := strings.SplitN(cleanEntry, " ", 2)
|
||||||
|
|
||||||
id, _ := strconv.Atoi(parts[0])
|
id, _ := strconv.Atoi(parts[0])
|
|
@ -1,4 +1,4 @@
|
||||||
package lib
|
package zet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
@ -11,46 +11,50 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"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) {
|
func CreateZet(title string) {
|
||||||
zid := newZid()
|
zid := newZid()
|
||||||
|
|
||||||
path := path.Join(GetZetDir(), strconv.Itoa(zid))
|
path := path.Join(config.GetZetDir(), strconv.Itoa(zid))
|
||||||
|
|
||||||
os.Mkdir(path, 0750)
|
os.Mkdir(path, 0750)
|
||||||
|
|
||||||
filePath := fmt.Sprintf("%s/index.adoc", path)
|
filePath := fmt.Sprintf("%s/index.adoc", path)
|
||||||
|
|
||||||
file, err := os.Create(filePath)
|
f, err := os.Create(filePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer f.Close()
|
||||||
|
|
||||||
_, err = fmt.Fprintf(file, "= %s", title)
|
_, err = fmt.Fprintf(f, "= %s", title)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
EditFile(filePath)
|
file.EditFile(filePath)
|
||||||
|
|
||||||
onSave(zid)
|
onSave(zid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func EditZet(id int) {
|
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)
|
onSave(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllZets() []int {
|
func GetAllZets() []int {
|
||||||
zets, err := execGitCommand("ls-files")
|
zets, err := git.ExecGitCommand("ls-files")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -87,7 +91,7 @@ func GetLatestZet() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SearchZets(query string) []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 {
|
if err != nil {
|
||||||
fmt.Printf("No matches found for %s.\n", query)
|
fmt.Printf("No matches found for %s.\n", query)
|
||||||
|
@ -135,9 +139,9 @@ func ParseZetList(ids []int) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ViewZet(id 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 {
|
func getTitle(id int) string {
|
||||||
|
@ -145,7 +149,7 @@ func getTitle(id int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTitleFromFile(filePath string) string {
|
func getTitleFromFile(filePath string) string {
|
||||||
filePath = path.Join(GetZetDir(), filePath)
|
filePath = path.Join(config.GetZetDir(), filePath)
|
||||||
|
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(filePath)
|
||||||
|
|
||||||
|
@ -173,7 +177,7 @@ func getTitleFromFile(filePath string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newZid() int {
|
func newZid() int {
|
||||||
cmd := exec.Command("ls", GetZetDir())
|
cmd := exec.Command("ls", config.GetZetDir())
|
||||||
output, _ := cmd.CombinedOutput()
|
output, _ := cmd.CombinedOutput()
|
||||||
|
|
||||||
zets := strings.Split(string(output), "\n")
|
zets := strings.Split(string(output), "\n")
|
||||||
|
@ -193,7 +197,7 @@ func newZid() int {
|
||||||
func onSave(id int) {
|
func onSave(id int) {
|
||||||
title := getTitle(id)
|
title := getTitle(id)
|
||||||
|
|
||||||
CommitZettel(id, title)
|
git.CommitZettel(id, title)
|
||||||
|
|
||||||
// TODO: Or delete the file if empty.
|
// TODO: Or delete the file if empty.
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue