github.com/thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/cf/terminal/ui_windows.go (about)

     1  // +build windows
     2  
     3  package terminal
     4  
     5  import (
     6  	"os"
     7  	"syscall"
     8  )
     9  
    10  // see SetConsoleMode documentation for bit flags
    11  // http://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx
    12  const ENABLE_ECHO_INPUT = 0x0004
    13  
    14  func (ui terminalUI) AskForPassword(prompt string) (passwd string) {
    15  	hStdin := syscall.Handle(os.Stdin.Fd())
    16  	var originalMode uint32
    17  
    18  	err := syscall.GetConsoleMode(hStdin, &originalMode)
    19  	if err != nil {
    20  		return
    21  	}
    22  
    23  	var newMode uint32 = (originalMode &^ ENABLE_ECHO_INPUT)
    24  
    25  	err = setConsoleMode(hStdin, newMode)
    26  	defer setConsoleMode(hStdin, originalMode)
    27  	defer ui.Say("")
    28  
    29  	if err != nil {
    30  		return
    31  	}
    32  
    33  	return ui.Ask(prompt)
    34  }
    35  
    36  func setConsoleMode(console syscall.Handle, mode uint32) (err error) {
    37  	dll := syscall.MustLoadDLL("kernel32")
    38  	proc := dll.MustFindProc("SetConsoleMode")
    39  	r, _, err := proc.Call(uintptr(console), uintptr(mode))
    40  
    41  	if r == 0 {
    42  		return err
    43  	}
    44  	return nil
    45  }