Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an alternative way to specify build enviroment variables #31

Closed
oliverpool opened this issue Mar 24, 2022 · 2 comments
Closed

Add an alternative way to specify build enviroment variables #31

oliverpool opened this issue Mar 24, 2022 · 2 comments

Comments

@oliverpool
Copy link
Contributor

Currently build variables like GOARM, GOOS and GOARCH are simply forwarded from the current environment with some default values:

func goEnv() []string {
goarch := "arm64" // Raspberry Pi 3
if e := os.Getenv("GOARCH"); e != "" {
goarch = e
}
env := os.Environ()
for idx, e := range env {
if strings.HasPrefix(e, "CGO_ENABLED=") {
env[idx] = "CGO_ENABLED=0"
}
}
return append(env, fmt.Sprintf("GOARCH=%s", goarch))
}

However this does not play well with go run:

GOARCH=arm go run github.com/gokrazy/tools/cmd/gokr-packer
    github.com/gokrazy/hello

Since GOARCH will be used by go run, the resulting gokr-packer executable will not run on and amd64 machine for instance.


I would like to be able to use go run github.com/gokrazy/tools/cmd/gokr-packer, so that the gokr-packer version can be managed by go.mod using a tools.go file:

//go:build tools

package tools

import (
	_ "github.com/gokrazy/tools/cmd/gokr-packer"
)

Here are the possible solutions to this problem, that I thought of:

  • support prefixed environment variables: GOKR_GOARCH= go run ... for instance (which would override `GOARCH)
  • add a flag to support build environment file: go run .../gokr-packer -buildenv=env with env being a file like:
GOARCH=arm

What do you think would be the most elegant way to fix this issue ?
I can then try to craft a PR.

@stapelberg
Copy link
Contributor

Instead of using go run, why not just go install && gokr-packer?

If you absolutely want to use go run, can you use its -exec flag to specify a wrapper that applies environment variables?

I’d like to avoid adding a custom way to pass environment variables.

@oliverpool
Copy link
Contributor Author

Instead of using go run, why not just go install && gokr-packer?

Because I wasn't able to find gokr-packerafterwards 🙈 it is actually simply in $(go env GOPATH)/bin/gokr-packer...
(and it seems faster than go run)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants