-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
88 lines (78 loc) · 2.02 KB
/
cli.go
File metadata and controls
88 lines (78 loc) · 2.02 KB
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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"sync"
)
type CLIController struct {
userIO sync.Mutex
ioWait bool
inputChan chan string
}
func (cliController *CLIController) getInput(prompt string) string {
cliController.lock()
cliController.ioWait = true
fmt.Println(prompt)
text := <-cliController.inputChan
cliController.ioWait = false
cliController.unlock()
return text
}
func (cliController *CLIController) getCommandInput(prompt string) string {
fmt.Println(prompt)
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
trimmedText := strings.Trim(text, "\n")
return trimmedText
}
func (cliController *CLIController) print(msg string) {
cliController.lock()
fmt.Println(msg)
cliController.unlock()
}
func (cliController *CLIController) lock() {
cliController.userIO.Lock()
}
func (cliController *CLIController) unlock() {
cliController.userIO.Unlock()
}
func (cliController *CLIController) getInputUnsafe(prompt string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Println(prompt)
text, _ := reader.ReadString('\n')
return text[0 : len(text)-1]
}
func (cliController *CLIController) printUnsafe(msg string) {
fmt.Println(msg)
}
func (cliController *CLIController) startCli(folder FolderManager, peerManager PeerManager) {
reader := bufio.NewReader(os.Stdin)
for {
text, _ := reader.ReadString('\n')
trimmedText := strings.Trim(text, "\n")
if cliController.ioWait {
cliController.inputChan <- trimmedText
continue
}
switch trimmedText {
case "add":
fmt.Println("Asking for folder path")
folderPath := cliController.getCommandInput("Enter the folder path to be added:")
folder.add(folderPath)
case "sync":
folderPath := cliController.getCommandInput("Enter the folder path to be synced")
folder.sync(folderPath)
cliController.print("Syncing " + folderPath)
case "print":
peerManager.printFileTransferStatus()
default:
if cliController.ioWait {
fmt.Println("Ignoring ", text)
} else {
fmt.Println("Invalid command ", text)
}
}
}
}