aboutsummaryrefslogtreecommitdiff
path: root/cmd/mv.go
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/mv.go
parent3f6aeea8c795b8bff59c1a8b45700d7bc44da4b9 (diff)
downloadzk-104ee66f5930f4a37ac84538c29a291bf1d08f4f.tar.gz
zk-104ee66f5930f4a37ac84538c29a291bf1d08f4f.tar.bz2
zk-104ee66f5930f4a37ac84538c29a291bf1d08f4f.zip
Use cobra as command selector
Diffstat (limited to 'cmd/mv.go')
-rw-r--r--cmd/mv.go59
1 files changed, 59 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)
+ }
+
+}