Add newt pkg move command to move packages within a repository
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/9c9a777f Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/9c9a777f Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/9c9a777f Branch: refs/heads/master Commit: 9c9a777f7d2ca2230824972495a018ae8b456f02 Parents: 4789254 Author: Sterling Hughes <[email protected]> Authored: Sat Feb 11 15:25:05 2017 -0800 Committer: Sterling Hughes <[email protected]> Committed: Sat Feb 11 15:25:23 2017 -0800 ---------------------------------------------------------------------- newt/cli/pkg_cmds.go | 116 +++++++++++++++++++ newt/vendor/mynewt.apache.org/newt/util/util.go | 12 ++ util/util.go | 12 ++ 3 files changed, 140 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/9c9a777f/newt/cli/pkg_cmds.go ---------------------------------------------------------------------- diff --git a/newt/cli/pkg_cmds.go b/newt/cli/pkg_cmds.go index 0972957..0b05f05 100644 --- a/newt/cli/pkg_cmds.go +++ b/newt/cli/pkg_cmds.go @@ -20,10 +20,16 @@ package cli import ( + "os" + "path" "strings" "github.com/spf13/cobra" + "mynewt.apache.org/newt/newt/interfaces" + "mynewt.apache.org/newt/newt/newtutil" + "mynewt.apache.org/newt/newt/pkg" "mynewt.apache.org/newt/newt/project" + "mynewt.apache.org/newt/util" ) var NewTypeStr = "pkg" @@ -40,6 +46,103 @@ func pkgNewCmd(cmd *cobra.Command, args []string) { } } +func pkgMoveCmd(cmd *cobra.Command, args []string) { + if len(args) != 2 { + NewtUsage(cmd, util.NewNewtError("Exactly two arguments required to pkg move")) + } + + srcLoc := args[0] + dstLoc := args[1] + + proj := TryGetProject() + interfaces.SetProject(proj) + + wd, err := os.Getwd() + if err != nil { + NewtUsage(cmd, util.NewNewtError(err.Error())) + } + + if err := os.Chdir(proj.Path() + "/"); err != nil { + NewtUsage(cmd, util.NewNewtError(err.Error())) + } + + /* Find source package, defaulting search to the local project if no + * repository descriptor is found. + */ + srcRepoName, srcName, err := newtutil.ParsePackageString(srcLoc) + if err != nil { + os.Chdir(wd) + NewtUsage(cmd, err) + } + + srcRepo := proj.LocalRepo() + if srcRepoName != "" { + srcRepo = proj.FindRepo(srcRepoName) + } + + srcPkg, err := proj.ResolvePackage(srcRepo, srcName) + if err != nil { + os.Chdir(wd) + NewtUsage(cmd, err) + } + + /* Resolve the destination package to a physical location, and then + * move the source package to that location. + * dstLoc is assumed to be in the format "@repo/pkg/loc" + */ + repoName, pkgName, err := newtutil.ParsePackageString(dstLoc) + if err != nil { + os.Chdir(wd) + NewtUsage(cmd, err) + } + + dstPath := proj.Path() + "/" + repo := proj.LocalRepo() + if repoName != "" { + dstPath += "repos/" + repoName + "/" + repo = proj.FindRepo(repoName) + if repo == nil { + os.Chdir(wd) + NewtUsage(cmd, util.NewNewtError("Destination repo "+ + repoName+" does not exist")) + } + } + dstPath += pkgName + "/" + + if util.NodeExist(dstPath) { + os.Chdir(wd) + NewtUsage(cmd, util.NewNewtError("Cannot overwrite existing package, "+ + "use pkg delete first")) + } + + util.StatusMessage(util.VERBOSITY_DEFAULT, "Moving package %s to %s\n", + srcLoc, dstLoc) + + if err := util.MoveDir(srcPkg.BasePath(), dstPath); err != nil { + os.Chdir(wd) + NewtUsage(cmd, err) + } + + dstPkg, err := pkg.LoadLocalPackage(repo, pkgName) + if err != nil { + os.Chdir(wd) + NewtUsage(cmd, err) + } + + dstPkg.SetName(pkgName) + dstPkg.Save() + + /* If the last element of the package path changes, rename the include + * directory. + */ + if path.Base(pkgName) != path.Base(srcPkg.Name()) { + util.MoveDir(dstPath+"/include/"+path.Base(srcPkg.Name()), + dstPath+"/include/"+path.Base(pkgName)) + } + + os.Chdir(wd) +} + func AddPackageCommands(cmd *cobra.Command) { /* Add the base package command, on top of which other commands are * keyed @@ -75,4 +178,17 @@ func AddPackageCommands(cmd *cobra.Command) { "pkg", "Type of package to create: pkg, bsp, sdk. Default pkg.") pkgCmd.AddCommand(newCmd) + + moveCmdHelpText := "" + moveCmdHelpEx := "" + + moveCmd := &cobra.Command{ + Use: "move", + Short: "Move a package from one location to another", + Long: moveCmdHelpText, + Example: moveCmdHelpEx, + Run: pkgMoveCmd, + } + + pkgCmd.AddCommand(moveCmd) } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/9c9a777f/newt/vendor/mynewt.apache.org/newt/util/util.go ---------------------------------------------------------------------- diff --git a/newt/vendor/mynewt.apache.org/newt/util/util.go b/newt/vendor/mynewt.apache.org/newt/util/util.go index bc7848b..fa3e60f 100644 --- a/newt/vendor/mynewt.apache.org/newt/util/util.go +++ b/newt/vendor/mynewt.apache.org/newt/util/util.go @@ -458,6 +458,18 @@ func MoveFile(srcFile string, destFile string) error { return nil } +func MoveDir(srcDir string, destDir string) error { + if err := CopyDir(srcDir, destDir); err != nil { + return err + } + + if err := os.RemoveAll(srcDir); err != nil { + return err + } + + return nil +} + // Reads each line from the specified text file into an array of strings. If a // line ends with a backslash, it is concatenated with the following line. func ReadLines(path string) ([]string, error) { http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/9c9a777f/util/util.go ---------------------------------------------------------------------- diff --git a/util/util.go b/util/util.go index bc7848b..fa3e60f 100644 --- a/util/util.go +++ b/util/util.go @@ -458,6 +458,18 @@ func MoveFile(srcFile string, destFile string) error { return nil } +func MoveDir(srcDir string, destDir string) error { + if err := CopyDir(srcDir, destDir); err != nil { + return err + } + + if err := os.RemoveAll(srcDir); err != nil { + return err + } + + return nil +} + // Reads each line from the specified text file into an array of strings. If a // line ends with a backslash, it is concatenated with the following line. func ReadLines(path string) ([]string, error) {
