You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by st...@apache.org on 2017/02/14 02:31:03 UTC

[1/2] incubator-mynewt-newt git commit: add the pkg remove command to newt

Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/develop 30b0d8e23 -> 128feabdb


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/develop
Commit: 432b8829aa184e2b21d1724845a8d24d8f2cc1e9
Parents: 30b0d8e
Author: Sterling Hughes <st...@runtime.io>
Authored: Sat Feb 11 16:29:02 2017 -0800
Committer: Sterling Hughes <st...@runtime.io>
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)
 }


[2/2] incubator-mynewt-newt git commit: use defer to exit function instead of calling os.Chdir() in exit path

Posted by st...@apache.org.
use defer to exit function instead of calling os.Chdir() in exit path


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

Branch: refs/heads/develop
Commit: 128feabdb78930d38312ed9464b5034f861a4ed6
Parents: 432b882
Author: Sterling Hughes <st...@runtime.io>
Authored: Mon Feb 13 18:27:19 2017 -0800
Committer: Sterling Hughes <st...@runtime.io>
Committed: Mon Feb 13 18:27:19 2017 -0800

----------------------------------------------------------------------
 newt/cli/pkg_cmds.go | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/128feabd/newt/cli/pkg_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/pkg_cmds.go b/newt/cli/pkg_cmds.go
index 75e9a00..4fdecca 100644
--- a/newt/cli/pkg_cmds.go
+++ b/newt/cli/pkg_cmds.go
@@ -63,6 +63,7 @@ func pkgMoveCmd(cmd *cobra.Command, args []string) {
 	if err != nil {
 		NewtUsage(cmd, util.ChildNewtError(err))
 	}
+	defer os.Chdir(wd)
 
 	if err := os.Chdir(proj.Path() + "/"); err != nil {
 		NewtUsage(cmd, util.ChildNewtError(err))
@@ -73,7 +74,6 @@ func pkgMoveCmd(cmd *cobra.Command, args []string) {
 	 */
 	srcRepoName, srcName, err := newtutil.ParsePackageString(srcLoc)
 	if err != nil {
-		os.Chdir(wd)
 		NewtUsage(cmd, err)
 	}
 
@@ -84,7 +84,6 @@ func pkgMoveCmd(cmd *cobra.Command, args []string) {
 
 	srcPkg, err := proj.ResolvePackage(srcRepo, srcName)
 	if err != nil {
-		os.Chdir(wd)
 		NewtUsage(cmd, err)
 	}
 
@@ -94,7 +93,6 @@ func pkgMoveCmd(cmd *cobra.Command, args []string) {
 	 */
 	repoName, pkgName, err := newtutil.ParsePackageString(dstLoc)
 	if err != nil {
-		os.Chdir(wd)
 		NewtUsage(cmd, err)
 	}
 
@@ -104,7 +102,6 @@ func pkgMoveCmd(cmd *cobra.Command, args []string) {
 		dstPath += "repos/" + repoName + "/"
 		repo = proj.FindRepo(repoName)
 		if repo == nil {
-			os.Chdir(wd)
 			NewtUsage(cmd, util.NewNewtError("Destination repo "+
 				repoName+" does not exist"))
 		}
@@ -112,7 +109,6 @@ func pkgMoveCmd(cmd *cobra.Command, args []string) {
 	dstPath += pkgName + "/"
 
 	if util.NodeExist(dstPath) {
-		os.Chdir(wd)
 		NewtUsage(cmd, util.NewNewtError("Cannot overwrite existing package, "+
 			"use pkg delete first"))
 	}
@@ -121,14 +117,12 @@ func pkgMoveCmd(cmd *cobra.Command, args []string) {
 		srcLoc, dstLoc)
 
 	if err := util.MoveDir(srcPkg.BasePath(), dstPath); err != nil {
-		os.Chdir(wd)
 		NewtUsage(cmd, err)
 	}
 
 	/* Replace the package name in the pkg.yml file */
 	pkgData, err := ioutil.ReadFile(dstPath + "/pkg.yml")
 	if err != nil {
-		os.Chdir(wd)
 		NewtUsage(cmd, err)
 	}
 
@@ -146,8 +140,6 @@ func pkgMoveCmd(cmd *cobra.Command, args []string) {
 		util.MoveDir(dstPath+"/include/"+path.Base(srcPkg.Name()),
 			dstPath+"/include/"+path.Base(pkgName))
 	}
-
-	os.Chdir(wd)
 }
 
 func pkgRemoveCmd(cmd *cobra.Command, args []string) {
@@ -162,6 +154,7 @@ func pkgRemoveCmd(cmd *cobra.Command, args []string) {
 	if err != nil {
 		NewtUsage(cmd, util.ChildNewtError(err))
 	}
+	defer os.Chdir(wd)
 
 	if err := os.Chdir(proj.Path() + "/"); err != nil {
 		NewtUsage(cmd, util.ChildNewtError(err))