Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

term: add examples for ReadPassword #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions read_password_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package term_test

import (
"fmt"
"os"

"golang.org/x/term"
)

func ExampleReadPassword() {
fmt.Print("Enter your password: ")
p, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
fmt.Printf("\nYou entered '%s'\n", string(p))
}
30 changes: 30 additions & 0 deletions read_password_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !windows

package term_test

import (
"fmt"
"os"

"golang.org/x/term"
)

func ExampleReadPassword_unix() {
// If standard input is not bound to the terminal, a password can
// still be read from it.
tty, err := os.Open("/dev/tty")
if err != nil {
panic(err)
}
defer tty.Close()
fmt.Print("Enter your password: ")
p, err := term.ReadPassword(int(tty.Fd()))
if err != nil {
panic(err)
}
fmt.Printf("\nYou entered '%s'\n", string(p))
}
40 changes: 40 additions & 0 deletions read_password_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package term_test

import (
"fmt"
"unsafe"

"golang.org/x/sys/windows"
"golang.org/x/term"
)

func ExampleReadPassword_windows() {
// If standard input is not bound to the terminal, a password can
// still be read from it.

path, err := windows.UTF16PtrFromString("CONIN$")
if err != nil {
panic(err)
}
var sa windows.SecurityAttributes
sa.Length = uint32(unsafe.Sizeof(sa))
sa.InheritHandle = 1
tty, err := windows.CreateFile(path, windows.GENERIC_READ|windows.GENERIC_WRITE, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, &sa, windows.OPEN_EXISTING, 0, 0)
if err != nil {
panic(err)
}
defer windows.CloseHandle(tty)

fmt.Print("Enter your password: ")
p, err := term.ReadPassword(int(tty))
if err != nil {
panic(err)
}
fmt.Printf("\nYou entered '%s'\n", string(p))
}