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