aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorGuangxiong Lin <[email protected]>2023-01-24 20:27:22 +0800
committerGuangxiong Lin <[email protected]>2023-01-24 20:27:22 +0800
commit104ee66f5930f4a37ac84538c29a291bf1d08f4f (patch)
treec663768184594207fd68533a773740d99956c865 /cmd
parent3f6aeea8c795b8bff59c1a8b45700d7bc44da4b9 (diff)
downloadzk-104ee66f5930f4a37ac84538c29a291bf1d08f4f.tar.gz
zk-104ee66f5930f4a37ac84538c29a291bf1d08f4f.tar.bz2
zk-104ee66f5930f4a37ac84538c29a291bf1d08f4f.zip
Use cobra as command selector
Diffstat (limited to 'cmd')
-rw-r--r--cmd/mv.go59
-rw-r--r--cmd/mv_test.go39
-rw-r--r--cmd/root.go19
3 files changed, 117 insertions, 0 deletions
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)
+ }
+}