This repository has been archived by the owner on Mar 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setupmanager.go
97 lines (83 loc) · 2.43 KB
/
setupmanager.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// This file is part of ezBastion.
// ezBastion is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// ezBastion is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with ezBastion. If not, see <https://www.gnu.org/licenses/>.
package setupmanager
import (
"bufio"
"fmt"
"log"
"os"
"path"
"regexp"
"strings"
)
//CheckFolder
func CheckFolder(exPath string) error {
if _, err := os.Stat(path.Join(exPath, "cert")); os.IsNotExist(err) {
err = os.MkdirAll(path.Join(exPath, "cert"), 0600)
if err != nil {
return err
}
log.Println("Make cert folder.")
}
if _, err := os.Stat(path.Join(exPath, "log")); os.IsNotExist(err) {
err = os.MkdirAll(path.Join(exPath, "log"), 0600)
if err != nil {
return err
}
log.Println("Make log folder.")
}
if _, err := os.Stat(path.Join(exPath, "conf")); os.IsNotExist(err) {
err = os.MkdirAll(path.Join(exPath, "conf"), 0600)
if err != nil {
return err
}
log.Println("Make conf folder.")
}
return nil
}
//AskForConfirmation waiting for user yes or no
func AskForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("\n%s [y/n]: ", s)
response, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err)
}
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
}
}
func AskForValue(s, def string, pattern string) string {
reader := bufio.NewReader(os.Stdin)
re := regexp.MustCompile(pattern)
for {
fmt.Printf("%s [%s]: ", s, def)
response, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err)
}
response = strings.TrimSpace(response)
if response == "" {
return def
} else if re.MatchString(response) {
return response
} else {
fmt.Printf("[%s] wrong format, must match (%s)\n", response, pattern)
}
fmt.Printf("[%s] wrong format, must match (%s)\n", response, pattern)
}
}