52 lines
1.9 KiB
Go
52 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/forgekey"
|
|
)
|
|
|
|
// ProbeTimeout bounds `go version`: it reads the installed go and fetches nothing (ADR 0477 §1).
|
|
const ProbeTimeout = 15 * time.Second
|
|
|
|
// ProbeGo is the probe of the guard (novox/hq ADR 0477 §1, §2; issue 1572): it runs `go version` with
|
|
// GOTOOLCHAIN=local, so it reads the version of the go the node's package manager installed, which is what the
|
|
// module builds with, and the third word of its answer is that version. It downloads nothing and needs no network.
|
|
// The environment is built from scratch like a step's (StepRunner), so the module's bus credentials are not in it.
|
|
func ProbeGo(home, cache string) func(ctx context.Context) (string, error) {
|
|
run := forgekey.ExecRunnerClean(0)
|
|
return func(ctx context.Context) (string, error) { return probeGo(ctx, run, home, cache) }
|
|
}
|
|
|
|
// probeEnv is what the probe gives `go version` beyond the base environment: the installed go alone, and the
|
|
// module's caches, so nothing is written to the account's home.
|
|
func probeEnv(cache string) []string {
|
|
return []string{
|
|
"GOTOOLCHAIN=local",
|
|
"GOMODCACHE=" + filepath.Join(cache, "gomod"),
|
|
"GOCACHE=" + filepath.Join(cache, "gobuild"),
|
|
}
|
|
}
|
|
|
|
func probeGo(ctx context.Context, run forgekey.Runner, home, cache string) (string, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, ProbeTimeout)
|
|
defer cancel()
|
|
r := run(ctx, append(baseEnv(home), probeEnv(cache)...), "go", "version")
|
|
if r.Status != 0 || r.Err != "" {
|
|
return "", fmt.Errorf("`go version` with GOTOOLCHAIN=local failed: %s", causeLine(r.Err+"\n"+r.Stderr))
|
|
}
|
|
return parseGoVersion(r.Stdout)
|
|
}
|
|
|
|
func parseGoVersion(out string) (string, error) {
|
|
f := strings.Fields(out)
|
|
if len(f) < 3 || f[0] != "go" || f[1] != "version" || !strings.HasPrefix(f[2], "go") {
|
|
return "", fmt.Errorf("`go version` answered %q", strings.TrimSpace(out))
|
|
}
|
|
return f[2], nil
|
|
}
|