search and replace package name instead of rewriting package

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

Branch: refs/heads/mynewt_1_0_0
Commit: b3f044d2911a82fb4a4b0f096a705947d4218828
Parents: d419f2a
Author: Sterling Hughes <[email protected]>
Authored: Sat Feb 11 15:48:37 2017 -0800
Committer: Marko Kiiskila <[email protected]>
Committed: Mon Mar 6 13:33:12 2017 -0800

----------------------------------------------------------------------
 newt/cli/pkg_cmds.go | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/b3f044d2/newt/cli/pkg_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/pkg_cmds.go b/newt/cli/pkg_cmds.go
index 0b05f05..13937d6 100644
--- a/newt/cli/pkg_cmds.go
+++ b/newt/cli/pkg_cmds.go
@@ -20,8 +20,10 @@
 package cli
 
 import (
+       "io/ioutil"
        "os"
        "path"
+       "regexp"
        "strings"
 
        "github.com/spf13/cobra"
@@ -123,6 +125,20 @@ func pkgMoveCmd(cmd *cobra.Command, args []string) {
                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)
+       }
+
+       re := regexp.MustCompile(regexp.QuoteMeta(srcName))
+       res := re.ReplaceAllString(string(pkgData), pkgName)
+
+       if err := ioutil.WriteFile(dstPath+"/pkg.yml", []byte(res), 0666); err 
!= nil {
+               NewtUsage(cmd, util.ChildNewtError(err))
+       }
+
        dstPkg, err := pkg.LoadLocalPackage(repo, pkgName)
        if err != nil {
                os.Chdir(wd)

Reply via email to