-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
67 lines (59 loc) · 1.82 KB
/
helper.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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
)
// Split the string
func spiltString(str string) []string {
return strings.Split(str, ",")
}
// Handle error
func handleError(err string, exitOnError bool) {
fmt.Println("\n" + err)
if exitOnError {
os.Exit(1)
}
}
// Prompt for confirmation
func yesOrNoConfirmation(appName string) string {
var YesOrNo = map[string]string{"y": "y", "ye": "y", "yes": "y", "n": "n", "no": "n"}
// Start the new scanner to get the user input
fmt.Printf("Are you sure you want to delete these apps (%s), do you wish to continue (Yy/Nn)?: ", appName)
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
// The choice entered
choiceEntered := input.Text()
// If its a valid value move on
if YesOrNo[strings.ToLower(choiceEntered)] == "y" { // Is it Yes
return choiceEntered
} else if YesOrNo[strings.ToLower(choiceEntered)] == "n" { // Is it No
handleError("Canceling as per user request", true)
} else { // Invalid choice, ask to re-enter
fmt.Println("Invalid Choice: Please enter Yy/Nn, try again.")
return yesOrNoConfirmation(appName)
}
}
return ""
}
// Check if manifest file exists in the current working directory
func readManifest() []byte {
manifestFileName := "manifest.yml"
currentDirectory, err := os.Getwd()
fullPath := fmt.Sprintf("%s/%s", currentDirectory, manifestFileName)
if err != nil {
handleError(fmt.Sprintf("Unable to get the current working directory, err: %v", err), true)
}
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
handleError(fmt.Sprintf("Unable to file the manifest file \"%s\"", fullPath), true)
} else {
b, err := ioutil.ReadFile(fullPath) // b has type []byte
if err != nil {
handleError(fmt.Sprintf("Error when reading the manifest \"%s\", err: %v",fullPath, err), true)
}
return b
}
return []byte{}
}