From 104ee66f5930f4a37ac84538c29a291bf1d08f4f Mon Sep 17 00:00:00 2001 From: Guangxiong Lin Date: Tue, 24 Jan 2023 20:27:22 +0800 Subject: Use cobra as command selector --- cmd/mv.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/mv_test.go | 39 ++++++++++++++++++++++++++++++++++++++ cmd/root.go | 19 +++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 cmd/mv.go create mode 100644 cmd/mv_test.go create mode 100644 cmd/root.go (limited to 'cmd') 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) + } +} -- cgit v1.2.3