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) } }