-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
60 lines (58 loc) · 1.44 KB
/
cli.go
File metadata and controls
60 lines (58 loc) · 1.44 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
package main
import (
"bufio"
"fmt"
"github.com/abiosoft/ishell"
"os"
)
func startCli(peerManager *PeerManager) {
shell := ishell.New()
shell.Println("Started goChat")
shell.AddCmd(&ishell.Cmd{
Name: "chat",
Help: "Start chatting",
Func: func(c *ishell.Context) {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
msg := scanner.Text()
if msg == "stop_chat" {
break
}
peerManager.sendMessage(msg)
}
},
})
shell.AddCmd(&ishell.Cmd{
Name: "send_file",
Help: "Send a file to a peer",
Func: func(c *ishell.Context) {
var filePath string
fmt.Print("Enter the file path you wish to send: ")
fmt.Scanln(&filePath)
fmt.Println("sending file ", filePath)
connectedPeers := peerManager.getAllPeers()
fmt.Println("Connected peers are: ")
for i := range connectedPeers {
fmt.Println(i, ". ", connectedPeers[i].username)
}
var peerIndex int
fmt.Print("Enter the index of the peer to whom you wish to send the file: ")
fmt.Scan(&peerIndex)
if peerIndex > len(connectedPeers) || peerIndex < 0 {
fmt.Println("Enter a valid index")
return
}
//Add support for multiple target peers later
targeUsernames := []string{connectedPeers[peerIndex].getIPWithoutPort()}
peerManager.sendFiles(targeUsernames, filePath)
},
})
shell.AddCmd(&ishell.Cmd{
Name: "print",
Help: "Print peers",
Func: func(c *ishell.Context) {
peerManager.printAll()
},
})
shell.Start()
}