github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/cmd/getgo/system.go (about)

     1  // Copyright 2017 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  //go:build !plan9
     6  // +build !plan9
     7  
     8  package main
     9  
    10  import (
    11  	"bytes"
    12  	"context"
    13  	exec "golang.org/x/sys/execabs"
    14  	"runtime"
    15  	"strings"
    16  )
    17  
    18  // arch contains either amd64 or 386.
    19  var arch = func() string {
    20  	cmd := exec.Command("uname", "-m") // "x86_64"
    21  	if runtime.GOOS == "windows" {
    22  		cmd = exec.Command("powershell", "-command", "(Get-WmiObject -Class Win32_ComputerSystem).SystemType") // "x64-based PC"
    23  	}
    24  
    25  	out, err := cmd.Output()
    26  	if err != nil {
    27  		// a sensible default?
    28  		return "amd64"
    29  	}
    30  	if bytes.Contains(out, []byte("64")) {
    31  		return "amd64"
    32  	}
    33  	return "386"
    34  }()
    35  
    36  func findGo(ctx context.Context, cmd string) (string, error) {
    37  	out, err := exec.CommandContext(ctx, cmd, "go").CombinedOutput()
    38  	return strings.TrimSpace(string(out)), err
    39  }