github.com/Xenoex/gopm@v0.6.5/cmd/run.go (about)

     1  // Copyright 2013 gopm authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"os"
    19  	"strings"
    20  
    21  	"github.com/Unknwon/com"
    22  	"github.com/Unknwon/goconfig"
    23  	"github.com/codegangsta/cli"
    24  
    25  	"github.com/gpmgo/gopm/doc"
    26  	"github.com/gpmgo/gopm/log"
    27  )
    28  
    29  var CmdRun = cli.Command{
    30  	Name:  "run",
    31  	Usage: "link dependencies and go run",
    32  	Description: `Command run links dependencies according to gopmfile,
    33  and execute 'go run'
    34  
    35  gopm run <go run commands>
    36  gopm run -l  will recursively find .gopmfile with value localPath and run the cmd in the .gopmfile,windows os is unspported, you need to run the command right at the localPath dir.`,
    37  	Action: runRun,
    38  	Flags: []cli.Flag{
    39  		cli.BoolFlag{"local,l", "run command with local gopath context"},
    40  	},
    41  }
    42  
    43  func runRun(ctx *cli.Context) {
    44  	setup(ctx)
    45  	//support unix only
    46  	if ctx.Bool("local") {
    47  		var localPath string
    48  		var err error
    49  		var wd string
    50  		var gf *goconfig.ConfigFile
    51  		wd, _ = os.Getwd()
    52  		for wd != "/" {
    53  			gf, _ = goconfig.LoadConfigFile(".gopmfile")
    54  			if gf != nil {
    55  				localPath = gf.MustValue("project", "localPath")
    56  			}
    57  			if localPath != "" {
    58  				break
    59  			}
    60  			os.Chdir("..")
    61  			wd, _ = os.Getwd()
    62  		}
    63  		if wd == "/" {
    64  			log.Fatal("run", "no gopmfile in the directory or parent directory")
    65  		}
    66  		argss := gf.MustValue("run", "cmd")
    67  		if localPath == "" {
    68  			log.Fatal("run", "No localPath set")
    69  		}
    70  		args := strings.Split(argss, " ")
    71  		argsLen := len(args)
    72  		for i := 0; i < argsLen; i++ {
    73  			strings.Trim(args[i], " ")
    74  		}
    75  		if len(args) < 2 {
    76  			log.Fatal("run", "cmd arguments less than 2")
    77  		}
    78  		err = execCmd(localPath, localPath, args...)
    79  		if err != nil {
    80  			log.Error("run", "Fail to run program:")
    81  			log.Fatal("", "\t"+err.Error())
    82  		}
    83  		return
    84  	}
    85  	// Get GOPATH.
    86  	installGopath = com.GetGOPATHs()[0]
    87  	if com.IsDir(installGopath) {
    88  		isHasGopath = true
    89  		log.Log("Indicated GOPATH: %s", installGopath)
    90  		installGopath += "/src"
    91  	}
    92  	// run command with gopm repos context
    93  	// need version control , auto link to GOPATH/src repos
    94  	genNewGoPath(ctx, false)
    95  	defer os.RemoveAll(doc.VENDOR)
    96  
    97  	log.Trace("Running...")
    98  
    99  	cmdArgs := []string{"go", "run"}
   100  	cmdArgs = append(cmdArgs, ctx.Args()...)
   101  	err := execCmd(newGoPath, newCurPath, cmdArgs...)
   102  	if err != nil {
   103  		log.Error("run", "Fail to run program:")
   104  		log.Fatal("", "\t"+err.Error())
   105  	}
   106  
   107  	log.Success("SUCC", "run", "Command executed successfully!")
   108  }