Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.

main: force -run to use runtime.GOOS and runtime.GOARCH #62

Merged
merged 1 commit into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,12 @@ func mainerr() error {

var stdout bytes.Buffer

installCmd := exec.Command("go", "install", mp.ImportPath)
installCmd := goCommand("install", mp.ImportPath)
installCmd.Dir = pkg.wd
installCmd.Env = append(buildEnv(localCacheProxy), "GOBIN="+gobin)
installCmd.Stdout = &stdout

if err := run(installCmd); err != nil {
if err := installCmd.run(); err != nil {
return err
}

Expand Down Expand Up @@ -436,11 +436,11 @@ var (
func (a *arg) get(proxy string) error {
env := buildEnv(proxy)

getCmd := exec.Command("go", "get", "-d", a.patt)
getCmd := goCommand("get", "-d", a.patt)
getCmd.Dir = a.wd
getCmd.Env = env

if err := run(getCmd); err != nil {
if err := getCmd.run(); err != nil {
return err
}

Expand All @@ -452,12 +452,12 @@ func (a *arg) list(proxy string) error {

var stdout bytes.Buffer

listCmd := exec.Command("go", "list", "-json", a.pkgPatt)
listCmd := goCommand("list", "-json", a.pkgPatt)
listCmd.Dir = a.wd
listCmd.Stdout = &stdout
listCmd.Env = env

if err := run(listCmd); err != nil {
if err := listCmd.run(); err != nil {
return err
}

Expand Down Expand Up @@ -541,23 +541,23 @@ func (a *arg) list(proxy string) error {
}
}

gmeCmd := exec.Command("go", "mod", "edit", "-require="+pkg.Module.Path+"@"+pkg.Module.Version)
gmeCmd := goCommand("mod", "edit", "-require="+pkg.Module.Path+"@"+pkg.Module.Version)
gmeCmd.Dir = a.wd
gmeCmd.Env = buildEnv("")

if err := run(gmeCmd); err != nil {
if err := gmeCmd.run(); err != nil {
return err
}

// now that we effectively have a copy of everything relevant in the
// target module (including replace directives), list to ensure they
// have been resolved

listCmd := exec.Command("go", "list", "-json", pkg.ImportPath)
listCmd := goCommand("list", "-json", pkg.ImportPath)
listCmd.Dir = a.wd
listCmd.Env = buildEnv(proxy)

if err := run(listCmd); err != nil {
if err := listCmd.run(); err != nil {
return err
}
}
Expand All @@ -583,11 +583,34 @@ func buildEnv(proxy string) []string {
return append(env, "GOFLAGS="+goflags)
}

func run(cmd *exec.Cmd) error {
type goCmd struct {
*exec.Cmd
}

func goCommand(args ...string) *goCmd {
return &goCmd{
Cmd: exec.Command("go", args...),
}
}

func (cmd *goCmd) run() error {
var stderr bytes.Buffer
cmd.Stderr = &stderr
start := time.Now()

if cmd.Env == nil {
cmd.Env = os.Environ()
}
// in -run mode, it only makes sense to perform go commands in line with
// runtime.GOOS and runtime.GOARCH. In the edge case scenario where this is
// intended, use gobin -p with appropriate GOOS and GOARCH env vars set.
if *fRun {
cmd.Env = append(cmd.Env,
"GOOS="+runtime.GOOS,
"GOARCH="+runtime.GOARCH,
)
}

if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to run %v: %v\n%s", strings.Join(cmd.Args, " "), err, stderr.String())
}
Expand All @@ -598,12 +621,8 @@ func run(cmd *exec.Cmd) error {
return nil
}

env := cmd.Env
if env == nil {
env = os.Environ()
}
var goenv []string
for _, v := range env {
for _, v := range cmd.Env {
if strings.HasPrefix(v, "GO") {
goenv = append(goenv, v)
}
Expand Down
19 changes: 19 additions & 0 deletions testdata/run_alt_goos_goarch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
env GOOS=rubbish
env GOARCH=myarch
gobin -m -run mod.com/p
stdout '^GOOS is rubbish, GOARCH is myarch$'

-- go.mod --
module mod.com

-- p/main.go --
package main

import (
"fmt"
"os"
)

func main() {
fmt.Printf("GOOS is %v, GOARCH is %v\n", os.Getenv("GOOS"), os.Getenv("GOARCH"))
}