-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcommon.go
51 lines (41 loc) · 1.03 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) 2021 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package sshlib
import (
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"strings"
terminal "golang.org/x/term"
)
// getAbsPath return absolute path convert.
// Replace `~` with your home directory.
func getAbsPath(path string) string {
// Replace home directory
usr, _ := user.Current()
path = strings.Replace(path, "~", usr.HomeDir, 1)
path, _ = filepath.Abs(path)
return path
}
// getPassphrase gets the passphrase from virtual terminal input and returns the result. Works only on UNIX-based OS.
func getPassphrase(msg string) (input string, err error) {
fmt.Fprintf(os.Stderr, msg)
// Open /dev/tty
tty, err := os.Open("/dev/tty")
if err != nil {
log.Fatal(err)
}
defer tty.Close()
// get input
result, err := terminal.ReadPassword(int(tty.Fd()))
if len(result) == 0 {
err = fmt.Errorf("err: input is empty")
return
}
input = string(result)
fmt.Println()
return
}