/
userInteraction.go
109 lines (87 loc) · 2.66 KB
/
userInteraction.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
98
99
100
101
102
103
104
105
106
107
108
109
package lib
/*==================================================================================*
* Interaction with user, including printing menus, and getting, processing input *
*===================================================================================*
* MenuMain represents entry-point into menu routine. Options printed represent *
* global CONSTANT variables stored in slices. *
* User input selections are represented by the same CONSTANT variables presented *
* in menu printing. *
*===================================================================================*/
import (
"bufio"
"fmt"
"os"
"strings"
)
// GreetUser on application start
func GreetUser() {
fmt.Println(messageGreeting)
}
// Menu functions
// MenuMain represents top-level menu for user application options
func MenuMain() {
fmt.Println()
userSelection := userMenuInput(InputOptionsMapMain)
startActionMain(userSelection)
}
func menuSite() {
userSelection := userMenuInput(InputOptionsMapSite)
startActionSite(userSelection)
}
func menuPage() {
userSelection := userMenuInput(InputOptionsMapPage)
startActionPage(userSelection)
}
func printMenuOptions(menuMap []string) {
for counter := range menuMap {
fmt.Printf("%v. %v\n", counter+1, menuMap[counter])
}
fmt.Println()
}
// User input functions
func userMenuInput(menuMap []string) string {
userSelection := ""
userInput := ""
userInputInt := -1
for userSelection == "" {
for userInputInt < 0 || userInputInt > len(menuMap)-1 {
printMenuOptions(menuMap)
fmt.Print(messageChoosePageAction)
userInput = getUserInputOption()
userInputInt = stringToInt(userInput) - 1
}
userSelection = menuMap[userInputInt]
}
return userSelection
}
func getUserInputOption() string {
reader := bufio.NewReader(os.Stdin)
userInputString, _ := reader.ReadString('\n')
userInputStringCleaned := removeCharacters(userInputString, "\n")
return userInputStringCleaned
}
func getPageSelection() string {
fmt.Print(messageChoosePageURL)
pageURL := getUserInputOption()
return pageURL
}
func getScopeSelection(scopes []string) string {
fmt.Printf("\n%v\n", messageSelectScope)
userInput := userMenuInput(scopes)
return userInput
}
func getDomainScopes(url string) []string {
scopesParts := []string{}
scopes := []string{}
scopes = append(scopes, pageDomainURL(url))
urlSplit := strings.Split(url, "/")
for counter := 1; counter < len(urlSplit); counter++ {
if len(urlSplit[counter]) > 0 {
scopesParts = append(scopesParts, urlSplit[counter])
}
}
for counter := 1; counter < len(scopesParts); counter++ {
scopes = append(scopes, scopes[counter-1]+slash+scopesParts[counter])
}
return scopes
}