github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/exp/ssh/utils_unix.go (about)

     1  // Copyright 2022 the u-root 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 || !windows
     6  // +build !plan9 !windows
     7  
     8  package main
     9  
    10  import (
    11  	"fmt"
    12  	"io"
    13  	"os"
    14  	"path/filepath"
    15  
    16  	"golang.org/x/term"
    17  )
    18  
    19  var (
    20  	defaultKeyFile    = filepath.Join(os.Getenv("HOME"), ".ssh/id_rsa")
    21  	defaultConfigFile = filepath.Join(os.Getenv("HOME"), ".ssh/config")
    22  
    23  	oldState *term.State
    24  )
    25  
    26  // cleanup returns the terminal to its original state
    27  func cleanup(in *os.File) {
    28  	if oldState != nil {
    29  		term.Restore(int(in.Fd()), oldState)
    30  	}
    31  }
    32  
    33  // raw puts the terminal into raw mode
    34  func raw(in *os.File) (err error) {
    35  	oldState, err = term.MakeRaw(int(in.Fd()))
    36  	return
    37  }
    38  
    39  // readPassword prompts the user for a password.
    40  func readPassword(in *os.File, out io.Writer) (string, error) {
    41  	fmt.Fprintf(out, "Password: ")
    42  	b, err := term.ReadPassword(int(in.Fd()))
    43  	return string(b), err
    44  }
    45  
    46  // getSize reads the size of the terminal.
    47  func getSize(in *os.File) (width, height int, err error) {
    48  	return term.GetSize(int(in.Fd()))
    49  }