add the pkg remove command to newt

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/432b8829
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/432b8829
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/432b8829

Branch: refs/heads/master
Commit: 432b8829aa184e2b21d1724845a8d24d8f2cc1e9
Parents: 30b0d8e
Author: Sterling Hughes <[email protected]>
Authored: Sat Feb 11 16:29:02 2017 -0800
Committer: Sterling Hughes <[email protected]>
Committed: Sat Feb 11 16:29:02 2017 -0800

----------------------------------------------------------------------
 newt/cli/pkg_cmds.go | 72 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 69 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/432b8829/newt/cli/pkg_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/pkg_cmds.go b/newt/cli/pkg_cmds.go
index d04691e..75e9a00 100644
--- a/newt/cli/pkg_cmds.go
+++ b/newt/cli/pkg_cmds.go
@@ -29,6 +29,7 @@ import (
        "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"
 )
@@ -60,11 +61,11 @@ func pkgMoveCmd(cmd *cobra.Command, args []string) {
 
        wd, err := os.Getwd()
        if err != nil {
-               NewtUsage(cmd, util.NewNewtError(err.Error()))
+               NewtUsage(cmd, util.ChildNewtError(err))
        }
 
        if err := os.Chdir(proj.Path() + "/"); err != nil {
-               NewtUsage(cmd, util.NewNewtError(err.Error()))
+               NewtUsage(cmd, util.ChildNewtError(err))
        }
 
        /* Find source package, defaulting search to the local project if no
@@ -149,12 +150,64 @@ func pkgMoveCmd(cmd *cobra.Command, args []string) {
        os.Chdir(wd)
 }
 
+func pkgRemoveCmd(cmd *cobra.Command, args []string) {
+       if len(args) != 1 {
+               NewtUsage(cmd, util.NewNewtError("Must specify a package name 
to delete"))
+       }
+
+       proj := TryGetProject()
+       interfaces.SetProject(proj)
+
+       wd, err := os.Getwd()
+       if err != nil {
+               NewtUsage(cmd, util.ChildNewtError(err))
+       }
+
+       if err := os.Chdir(proj.Path() + "/"); err != nil {
+               NewtUsage(cmd, util.ChildNewtError(err))
+       }
+       /* Resolve package, and get path from package to ensure we're being 
asked
+        * to remove a valid path.
+        */
+       repoName, pkgName, err := newtutil.ParsePackageString(args[0])
+       if err != nil {
+               os.Chdir(wd)
+               NewtUsage(cmd, err)
+       }
+
+       repo := proj.LocalRepo()
+       if repoName != "" {
+               repo = proj.FindRepo(repoName)
+               if repo == nil {
+                       os.Chdir(wd)
+                       NewtUsage(cmd, util.NewNewtError("Destination repo "+
+                               repoName+" does not exist"))
+               }
+       }
+
+       pkg, err := pkg.LoadLocalPackage(repo, pkgName)
+       if err != nil {
+               os.Chdir(wd)
+               NewtUsage(cmd, err)
+       }
+
+       util.StatusMessage(util.VERBOSITY_DEFAULT, "Removing package %s\n",
+               args[0])
+
+       if err := os.RemoveAll(pkg.BasePath()); err != nil {
+               os.Chdir(wd)
+               NewtUsage(cmd, util.ChildNewtError(err))
+       }
+
+       os.Chdir(wd)
+}
+
 func AddPackageCommands(cmd *cobra.Command) {
        /* Add the base package command, on top of which other commands are
         * keyed
         */
        pkgHelpText := "Commands for creating and manipulating packages"
-       pkgHelpEx := "newt pkg new --type=pkg libs/mylib"
+       pkgHelpEx := "  newt pkg new --type=pkg libs/mylib"
 
        pkgCmd := &cobra.Command{
                Use:     "pkg",
@@ -197,4 +250,17 @@ func AddPackageCommands(cmd *cobra.Command) {
        }
 
        pkgCmd.AddCommand(moveCmd)
+
+       removeCmdHelpText := ""
+       removeCmdHelpEx := ""
+
+       removeCmd := &cobra.Command{
+               Use:     "remove",
+               Short:   "Remove a package",
+               Long:    removeCmdHelpText,
+               Example: removeCmdHelpEx,
+               Run:     pkgRemoveCmd,
+       }
+
+       pkgCmd.AddCommand(removeCmd)
 }

Reply via email to