github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/cmd/getgo/system_windows.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 windows
     6  // +build windows
     7  
     8  package main
     9  
    10  import (
    11  	"context"
    12  	"log"
    13  	"os"
    14  	"syscall"
    15  	"unsafe"
    16  )
    17  
    18  const (
    19  	envSeparator = ";"
    20  	homeKey      = "USERPROFILE"
    21  	lineEnding   = "/r/n"
    22  	pathVar      = "$env:Path"
    23  )
    24  
    25  var installPath = `c:\go`
    26  
    27  func isWindowsXP() bool {
    28  	v, err := syscall.GetVersion()
    29  	if err != nil {
    30  		log.Fatalf("GetVersion failed: %v", err)
    31  	}
    32  	major := byte(v)
    33  	return major < 6
    34  }
    35  
    36  func whichGo(ctx context.Context) (string, error) {
    37  	return findGo(ctx, "where")
    38  }
    39  
    40  // currentShell reports the current shell.
    41  // It might be "powershell.exe", "cmd.exe" or any of the *nix shells.
    42  //
    43  // Returns empty string if the shell is unknown.
    44  func currentShell() string {
    45  	shell := os.Getenv("SHELL")
    46  	if shell != "" {
    47  		return shell
    48  	}
    49  
    50  	pid := os.Getppid()
    51  	pe, err := getProcessEntry(pid)
    52  	if err != nil {
    53  		verbosef("getting shell from process entry failed: %v", err)
    54  		return ""
    55  	}
    56  
    57  	return syscall.UTF16ToString(pe.ExeFile[:])
    58  }
    59  
    60  func getProcessEntry(pid int) (*syscall.ProcessEntry32, error) {
    61  	// From https://go.googlesource.com/go/+/go1.8.3/src/syscall/syscall_windows.go#941
    62  	snapshot, err := syscall.CreateToolhelp32Snapshot(syscall.TH32CS_SNAPPROCESS, 0)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	defer syscall.CloseHandle(snapshot)
    67  
    68  	var procEntry syscall.ProcessEntry32
    69  	procEntry.Size = uint32(unsafe.Sizeof(procEntry))
    70  	if err = syscall.Process32First(snapshot, &procEntry); err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	for {
    75  		if procEntry.ProcessID == uint32(pid) {
    76  			return &procEntry, nil
    77  		}
    78  
    79  		if err := syscall.Process32Next(snapshot, &procEntry); err != nil {
    80  			return nil, err
    81  		}
    82  	}
    83  }
    84  
    85  func persistEnvChangesForSession() error {
    86  	return nil
    87  }