aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--cmd/mv.go59
-rw-r--r--cmd/mv_test.go39
-rw-r--r--cmd/root.go19
-rw-r--r--command.go32
-rw-r--r--go.mod10
-rw-r--r--go.sum10
-rw-r--r--main.go16
-rw-r--r--pkg/doc.go (renamed from doc.go)8
-rw-r--r--pkg/map.go (renamed from map.go)2
-rw-r--r--pkg/set.go (renamed from set.go)2
11 files changed, 156 insertions, 46 deletions
diff --git a/Makefile b/Makefile
index 45238f4..0d23689 100644
--- a/Makefile
+++ b/Makefile
@@ -4,9 +4,14 @@ all: zk
zk: main.go command.go go.mod go.sum map.go set.go doc.go
go build .
+.PHONY: test
+test:
+ go test ./...
+
.PHONY: install
install: zk
@install -vm0755 zk /usr/bin/zk
+.PHONY: uninstall
uninstall:
@rm -vrf /usr/bin/zk
diff --git a/cmd/mv.go b/cmd/mv.go
new file mode 100644
index 0000000..c3f4b5b
--- /dev/null
+++ b/cmd/mv.go
@@ -0,0 +1,59 @@
+package cmd
+
+import (
+ "errors"
+ "os"
+ "path/filepath"
+
+ "github.com/spf13/cobra"
+
+ "gxlin.org/zk/pkg"
+)
+
+func init() {
+ rootCmd.AddCommand(mvCmd)
+}
+
+var mvCmd = &cobra.Command{
+ Use: "mv [source] [target]",
+ Args: mvCmdArgs,
+ Run: mvCmdRun,
+}
+
+func mvCmdArgs(cmd *cobra.Command, args []string) error {
+ if len(args) != 2 {
+ return errors.New("requires 2 args")
+ }
+
+ return nil
+}
+
+func mvCmdRun(cmd *cobra.Command, args []string) {
+ rename(args[0], args[1])
+}
+
+func rename(source, target string) {
+ source, err := filepath.Abs(source)
+ if err != nil {
+ panic("Unable to get abspath of " + source)
+ }
+
+ target, err = filepath.Abs(target)
+ if err != nil {
+ panic("Unable to get abspath of " + target)
+ }
+
+ if !pkg.DocCollection.Contain(source) {
+ panic("Database doesn't contain " + source)
+ }
+
+ if err := os.Rename(source, target); err != nil {
+ panic(err)
+ }
+
+ doc := pkg.DocCollection[source]
+ for backlink := range doc.Backlinks {
+ pkg.DocCollection[backlink].UpdateLinks(source, target)
+ }
+
+}
diff --git a/cmd/mv_test.go b/cmd/mv_test.go
new file mode 100644
index 0000000..4e0544f
--- /dev/null
+++ b/cmd/mv_test.go
@@ -0,0 +1,39 @@
+package cmd
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "gxlin.org/zk/pkg"
+)
+
+func TestRename(t *testing.T) {
+ tmpDir := t.TempDir()
+ backlinkedFilename := filepath.Join(tmpDir, "backlinked.md")
+ sourceFilename := filepath.Join(tmpDir, "source.md")
+ targetFilename := filepath.Join(tmpDir, "target.md")
+
+ os.WriteFile(backlinkedFilename, []byte("[Title](source.md)"), 0644)
+ os.WriteFile(sourceFilename, []byte("Hello World"), 0644)
+
+ pkg.DocCollection = pkg.NewDocs(tmpDir)
+ rename(sourceFilename, targetFilename)
+
+ targetFileContent, err := os.ReadFile(targetFilename)
+ if err != nil {
+ t.Error(err)
+ } else if string(targetFileContent) != "Hello World" {
+ t.Errorf("File content or matched after moved")
+ }
+
+ backlinkedFileContent, err := os.ReadFile(backlinkedFilename)
+ if err != nil {
+ t.Error(err)
+ } else if string(backlinkedFileContent) != "[Title](target.md)" {
+ t.Errorf(
+ "The link in backlinked file doesn't match. File content:\n%s",
+ string(backlinkedFileContent))
+
+ }
+}
diff --git a/cmd/root.go b/cmd/root.go
new file mode 100644
index 0000000..19bf463
--- /dev/null
+++ b/cmd/root.go
@@ -0,0 +1,19 @@
+package cmd
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/spf13/cobra"
+)
+
+var rootCmd = &cobra.Command{
+ Use: "zk",
+}
+
+func Execute() {
+ if err := rootCmd.Execute(); err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+}
diff --git a/command.go b/command.go
deleted file mode 100644
index 803970b..0000000
--- a/command.go
+++ /dev/null
@@ -1,32 +0,0 @@
-package main
-
-import (
- "os"
- "path/filepath"
-)
-
-func Rename(source, target string) {
- source, err := filepath.Abs(source)
- if err != nil {
- panic("Unable to get abspath of " + source)
- }
-
- target, err = filepath.Abs(target)
- if err != nil {
- panic("Unable to get abspath of " + target)
- }
-
- if !DocCollection.Contain(source) {
- panic("Database doesn't contain " + source)
- }
-
- if err := os.Rename(source, target); err != nil {
- panic(err)
- }
-
- doc := DocCollection[source]
- for backlink := range doc.backlinks {
- DocCollection[backlink].UpdateLinks(source, target)
- }
-
-}
diff --git a/go.mod b/go.mod
index f952513..ecb1020 100644
--- a/go.mod
+++ b/go.mod
@@ -2,4 +2,12 @@ module gxlin.org/zk
go 1.19
-require github.com/yuin/goldmark v1.5.3
+require (
+ github.com/spf13/cobra v1.6.1
+ github.com/yuin/goldmark v1.5.3
+)
+
+require (
+ github.com/inconshreveable/mousetrap v1.0.1 // indirect
+ github.com/spf13/pflag v1.0.5 // indirect
+)
diff --git a/go.sum b/go.sum
index b36a911..fd883d6 100644
--- a/go.sum
+++ b/go.sum
@@ -1,2 +1,12 @@
+github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
+github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
+github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
+github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
+github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
+github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
+github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/yuin/goldmark v1.5.3 h1:3HUJmBFbQW9fhQOzMgseU134xfi6hU+mjWywx5Ty+/M=
github.com/yuin/goldmark v1.5.3/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/main.go b/main.go
index 99ae88c..c967b6e 100644
--- a/main.go
+++ b/main.go
@@ -1,15 +1,17 @@
package main
-import "os"
+// import "os"
+
+import (
+ "gxlin.org/zk/cmd"
+ "gxlin.org/zk/pkg"
+)
func main() {
- DocCollection = NewDocs(".")
- if DocCollection == nil {
+ pkg.DocCollection = pkg.NewDocs(".")
+ if pkg.DocCollection == nil {
panic("Unable to initialize collection")
}
- if len(os.Args) > 3 && os.Args[1] == "mv" {
- Rename(os.Args[2], os.Args[3])
- }
-
+ cmd.Execute()
}
diff --git a/doc.go b/pkg/doc.go
index 4971e8e..9f85b3a 100644
--- a/doc.go
+++ b/pkg/doc.go
@@ -1,4 +1,4 @@
-package main
+package pkg
import (
"bytes"
@@ -23,7 +23,7 @@ type Doc struct {
path string
content []byte
links Set[string]
- backlinks Set[string]
+ Backlinks Set[string]
}
func (doc *Doc) UpdateLinks(source, target string) {
@@ -89,7 +89,7 @@ func NewDoc(filename string) *Doc {
path: filename,
content: content,
links: links,
- backlinks: Set[string]{},
+ Backlinks: Set[string]{},
}
}
@@ -139,7 +139,7 @@ func NewDocs(dirname string) Docs {
continue
}
- docs[abslink].backlinks.Insert(filename)
+ docs[abslink].Backlinks.Insert(filename)
}
}
diff --git a/map.go b/pkg/map.go
index 5692065..a9f8ccb 100644
--- a/map.go
+++ b/pkg/map.go
@@ -1,4 +1,4 @@
-package main
+package pkg
type Map[K comparable, V interface{}] map[K]V
diff --git a/set.go b/pkg/set.go
index 760c9ae..634bb59 100644
--- a/set.go
+++ b/pkg/set.go
@@ -1,4 +1,4 @@
-package main
+package pkg
type Set[T comparable] map[T]bool