/
helpers.go
84 lines (62 loc) · 1.51 KB
/
helpers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package commands
import "C"
import (
"errors"
"fmt"
"golang.org/x/crypto/ssh/terminal"
"os"
"github.com/codecare/gokeeper/internal/shell"
"strings"
"syscall"
)
func ExecuteConfirm() error {
fmt.Println("please confirm (y/n)")
confirmation := shell.ReadInput()
confirmation = strings.TrimSpace(confirmation)
if strings.EqualFold("y", confirmation) {
return nil
}
return errors.New("not confiremd by user")
}
func readValue(parameterName, defaultValue, newValue string) string {
// use from command
if len(newValue) > 0 {
fmt.Printf("%-20s ->[%s]\n", parameterName + ":", newValue)
return newValue
}
fmt.Printf("%-20s [%s]\n", parameterName + ":", defaultValue)
inputValue := shell.ReadInput()
if len(inputValue) < 1 {
return defaultValue
}
return inputValue
}
func readValueNoDefault(parameterName string) string {
fmt.Printf("%-20s \n", parameterName + ":")
return shell.ReadInput()
}
func readPassword() string {
if (terminal.IsTerminal(int(syscall.Stdin))) {
fmt.Print("Enter Password (terminal): ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return ""
}
password := string(bytePassword)
return strings.TrimSpace(password)
} else {
fmt.Print("Enter Password (no terminal): ")
inputValue := shell.ReadInput()
return strings.TrimSpace(inputValue)
}
}
func fileDoesExist(file string) (bool, error) {
_, err := os.Stat(file)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
return true, nil
}