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

Branch: refs/heads/mynewt_1_0_0
Commit: 85bc49bfaa1aaa7df9f63e4c0557a48cc365fd06
Parents: f497cab
Author: Sterling Hughes <sterl...@runtime.io>
Authored: Mon Feb 13 18:27:19 2017 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Mar 6 13:33:39 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/85bc49bf/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))

Reply via email to