Skip to content

Instantly share code, notes, and snippets.

@tauoverpi
Created November 22, 2023 17:33
Show Gist options
  • Save tauoverpi/5ee4fd8245e790467345b77278a39d2e to your computer and use it in GitHub Desktop.
Save tauoverpi/5ee4fd8245e790467345b77278a39d2e to your computer and use it in GitHub Desktop.
When you don't want a Makefile
package main
import (
"os"
"os/exec"
"reflect"
"runtime"
)
func main() {
env := getenv()
wasm := env
wasm.GOOS = "js"
wasm.GOARCH = "wasm"
var command string
if len(os.Args) == 2 {
command = os.Args[1]
} else {
command = "build"
}
switch command {
case "build":
Go(env, "build", "github.com/tauoverpi/example/cmd/levy-server")
Go(wasm, "build", "github.com/tauoverpi/example/cmd/levy-wasm")
case "test":
Go(env, "test", "github.com/tauoverpi/example/cmd/levy-web")
Go(env, "build", "github.com/tauoverpi/example/cmd/levy-wasm")
default:
panic("u wot m8")
}
}
func Go(env Env, command ...string) {
cmd := exec.Command("go", command...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
val := reflect.ValueOf(env)
typ := val.Type()
for i := 0; i < typ.NumField(); i++ {
value := val.Field(i).String()
field := typ.Field(i).Name
cmd.Env = append(cmd.Env, field+"="+value)
}
}
type Env struct {
GOOS string
GOARCH string
GOCACHE string
XDG_CACHE_HOME string
HOME string
}
func getenv() Env {
env := Env{
GOOS: os.Getenv("GOOS"),
GOARCH: os.Getenv("GOARCH"),
GOCACHE: os.Getenv("GOCACHE"),
XDG_CACHE_HOME: os.Getenv("XDG_CACHE_HOME"),
HOME: os.Getenv("HOME"),
}
if len(env.GOOS) == 0 {
env.GOOS = runtime.GOOS
}
if len(env.GOARCH) == 0 {
env.GOARCH = runtime.GOARCH
}
if len(env.GOCACHE) == 0 {
env.GOCACHE = "/tmp/go-cache"
}
return env
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment