github.com/Xenoex/gopm@v0.6.5/cmd/exec.go (about) 1 // Copyright 2013-2014 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 "fmt" 19 "path" 20 "strings" 21 22 "github.com/Unknwon/com" 23 "github.com/codegangsta/cli" 24 25 "github.com/gpmgo/gopm/doc" 26 "github.com/gpmgo/gopm/log" 27 ) 28 29 var CmdExec = cli.Command{ 30 Name: "exec", 31 Usage: "execute go binary in specific version", 32 Description: `Command exec builds and executes go binary in specific version 33 based on the setting of your gopmfile or argument 34 35 gopm exec <import path|package name> 36 gopm exec <import path>@[<tag|commit|branch>:<value>] 37 gopm exec <package name>@[<tag|commit|branch>:<value>] 38 39 Can only specify one each time, and only works for projects that 40 contain main package`, 41 Action: runExec, 42 Flags: []cli.Flag{ 43 cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any"}, 44 cli.BoolFlag{"verbose, v", "show process details"}, 45 }, 46 } 47 48 func runExec(ctx *cli.Context) { 49 setup(ctx) 50 51 if len(ctx.Args()) == 0 { 52 log.Error("exec", "Cannot start command:") 53 log.Fatal("", "\tNo package specified") 54 } 55 56 installRepoPath = doc.HomeDir + "/repos" 57 log.Log("Local repository path: %s", installRepoPath) 58 59 // Parse package version. 60 info := ctx.Args()[0] 61 pkgPath := info 62 node := doc.NewNode(pkgPath, pkgPath, doc.BRANCH, "", true) 63 if i := strings.Index(info, "@"); i > -1 { 64 // Specify version by argument. 65 pkgPath = info[:i] 66 node.Type, node.Value = validPath(info[i+1:]) 67 } 68 69 // Check package name. 70 if !strings.Contains(pkgPath, "/") { 71 pkgPath = doc.GetPkgFullPath(pkgPath) 72 } 73 74 node.ImportPath = pkgPath 75 node.DownloadURL = pkgPath 76 77 if len(node.Value) == 0 && com.IsFile(".gopmfile") { 78 // Specify version by gopmfile. 79 gf := doc.NewGopmfile(".") 80 info, err := gf.GetValue("exec", pkgPath) 81 if err == nil && len(info) > 0 { 82 node.Type, node.Value = validPath(info) 83 } 84 } 85 86 // Check if binary exists. 87 binPath := path.Join(doc.HomeDir, "bins", node.ImportPath, path.Base(node.ImportPath)+versionSuffix(node.Value)) 88 log.Log("Binary path: %s", binPath) 89 90 if !com.IsFile(binPath) { 91 // Calling bin command. 92 args := []string{"bin", "-d"} 93 if ctx.Bool("verbose") { 94 args = append(args, "-v") 95 } 96 if ctx.Bool("update") { 97 args = append(args, "-u") 98 } 99 args = append(args, node.ImportPath+"@"+node.Type+":"+node.Value) 100 args = append(args, path.Dir(binPath)) 101 stdout, stderr, err := com.ExecCmd("gopm", args...) 102 if err != nil { 103 log.Error("exec", "Building binary failed:") 104 log.Fatal("", "\t"+err.Error()) 105 } 106 if len(stderr) > 0 { 107 fmt.Print(stderr) 108 } 109 if len(stdout) > 0 { 110 fmt.Print(stdout) 111 } 112 } 113 fmt.Printf("%+v\n", node) 114 115 return 116 stdout, stderr, err := com.ExecCmd(binPath, ctx.Args()[1:]...) 117 if err != nil { 118 log.Error("exec", "Calling binary failed:") 119 log.Fatal("", "\t"+err.Error()) 120 } 121 if len(stderr) > 0 { 122 fmt.Print(stderr) 123 } 124 if len(stdout) > 0 { 125 fmt.Print(stdout) 126 } 127 }