mynewt-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sterl...@apache.org
Subject [2/2] incubator-mynewt-newt git commit: use defer to exit function instead of calling os.Chdir() in exit path
Date Tue, 14 Feb 2017 02:31:04 GMT
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 <sterling@runtime.io>
Authored: Mon Feb 13 18:27:19 2017 -0800
Committer: Sterling Hughes <sterling@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))


Mime
View raw message