github.com/sirkon/goproxy@v1.4.8/internal/run/run.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package run implements the ``go run'' command. 6 package run 7 8 import ( 9 "fmt" 10 "os" 11 "strings" 12 13 "github.com/sirkon/goproxy/internal/base" 14 "github.com/sirkon/goproxy/internal/cfg" 15 "github.com/sirkon/goproxy/internal/load" 16 "github.com/sirkon/goproxy/internal/str" 17 "github.com/sirkon/goproxy/internal/work" 18 ) 19 20 var CmdRun = &base.Command{ 21 UsageLine: "go run [build flags] [-exec xprog] package [arguments...]", 22 Short: "compile and run Go program", 23 Long: ` 24 Run compiles and runs the named main Go package. 25 Typically the package is specified as a list of .go source files, 26 but it may also be an import path, file system path, or pattern 27 matching a single known package, as in 'go run .' or 'go run my/cmd'. 28 29 By default, 'go run' runs the compiled binary directly: 'a.out arguments...'. 30 If the -exec flag is given, 'go run' invokes the binary using xprog: 31 'xprog a.out arguments...'. 32 If the -exec flag is not given, GOOS or GOARCH is different from the system 33 default, and a program named go_$GOOS_$GOARCH_exec can be found 34 on the current search path, 'go run' invokes the binary using that program, 35 for example 'go_nacl_386_exec a.out arguments...'. This allows execution of 36 cross-compiled programs when a simulator or other execution method is 37 available. 38 39 The exit status of Run is not the exit status of the compiled binary. 40 41 For more about build flags, see 'go help build'. 42 For more about specifying packages, see 'go help packages'. 43 44 See also: go build. 45 `, 46 } 47 48 func init() { 49 CmdRun.Run = runRun // break init loop 50 51 work.AddBuildFlags(CmdRun) 52 CmdRun.Flag.Var((*base.StringsFlag)(&work.ExecCmd), "exec", "") 53 } 54 55 func printStderr(args ...interface{}) (int, error) { 56 return fmt.Fprint(os.Stderr, args...) 57 } 58 59 func runRun(cmd *base.Command, args []string) { 60 work.BuildInit() 61 var b work.Builder 62 b.Init() 63 b.Print = printStderr 64 i := 0 65 for i < len(args) && strings.HasSuffix(args[i], ".go") { 66 i++ 67 } 68 var p *load.Package 69 if i > 0 { 70 files := args[:i] 71 for _, file := range files { 72 if strings.HasSuffix(file, "_test.go") { 73 // GoFilesPackage is going to assign this to TestGoFiles. 74 // Reject since it won't be part of the build. 75 base.Fatalf("go run: cannot run *_test.go files (%s)", file) 76 } 77 } 78 p = load.GoFilesPackage(files) 79 } else if len(args) > 0 && !strings.HasPrefix(args[0], "-") { 80 pkgs := load.PackagesAndErrors(args[:1]) 81 if len(pkgs) > 1 { 82 var names []string 83 for _, p := range pkgs { 84 names = append(names, p.ImportPath) 85 } 86 base.Fatalf("go run: pattern %s matches multiple packages:\n\t%s", args[0], strings.Join(names, "\n\t")) 87 } 88 p = pkgs[0] 89 i++ 90 } else { 91 base.Fatalf("go run: no go files listed") 92 } 93 cmdArgs := args[i:] 94 95 if p.Error != nil { 96 base.Fatalf("%s", p.Error) 97 } 98 p.Internal.OmitDebug = true 99 if len(p.DepsErrors) > 0 { 100 // Since these are errors in dependencies, 101 // the same error might show up multiple times, 102 // once in each package that depends on it. 103 // Only print each once. 104 printed := map[*load.PackageError]bool{} 105 for _, err := range p.DepsErrors { 106 if !printed[err] { 107 printed[err] = true 108 base.Errorf("%s", err) 109 } 110 } 111 } 112 base.ExitIfErrors() 113 if p.Name != "main" { 114 base.Fatalf("go run: cannot run non-main package") 115 } 116 p.Target = "" // must build - not up to date 117 var src string 118 if len(p.GoFiles) > 0 { 119 src = p.GoFiles[0] 120 } else if len(p.CgoFiles) > 0 { 121 src = p.CgoFiles[0] 122 } else { 123 // this case could only happen if the provided source uses cgo 124 // while cgo is disabled. 125 hint := "" 126 if !cfg.BuildContext.CgoEnabled { 127 hint = " (cgo is disabled)" 128 } 129 base.Fatalf("go run: no suitable source files%s", hint) 130 } 131 p.Internal.ExeName = src[:len(src)-len(".go")] // name temporary executable for first go file 132 a1 := b.LinkAction(work.ModeBuild, work.ModeBuild, p) 133 a := &work.Action{Mode: "go run", Func: buildRunProgram, Args: cmdArgs, Deps: []*work.Action{a1}} 134 b.Do(a) 135 } 136 137 // buildRunProgram is the action for running a binary that has already 138 // been compiled. We ignore exit status. 139 func buildRunProgram(b *work.Builder, a *work.Action) error { 140 cmdline := str.StringList(work.FindExecCmd(), a.Deps[0].Target, a.Args) 141 if cfg.BuildN || cfg.BuildX { 142 b.Showcmd("", "%s", strings.Join(cmdline, " ")) 143 if cfg.BuildN { 144 return nil 145 } 146 } 147 148 base.RunStdin(cmdline) 149 return nil 150 }