Adding the help command

This commit is contained in:
Denis-Cosmin Nutiu 2017-10-20 18:20:11 +03:00
parent 9757016561
commit 3e0e994e86
2 changed files with 26 additions and 2 deletions

View file

@ -22,7 +22,7 @@ func SendFile(c net.Conn, path string) (int, error) {
lastForwardSlash := strings.LastIndex(path, "/") lastForwardSlash := strings.LastIndex(path, "/")
if lastForwardSlash != -1 { if lastForwardSlash != -1 {
// Eliminate the last forward slash i.e ../../asdas will become asdas // Eliminate the last forward slash i.e ../../asdas will become asdas
fileName = path[lastForwardSlash + 1:] fileName = path[lastForwardSlash+1:]
} else { } else {
fileName = path fileName = path
} }
@ -50,7 +50,6 @@ func SendFile(c net.Conn, path string) (int, error) {
return n, nil return n, nil
} }
// ListFiles list the files from path and sends them to the connection // ListFiles list the files from path and sends them to the connection
func ListFiles(c net.Conn) error { func ListFiles(c net.Conn) error {
files, err := ioutil.ReadDir(PATH) files, err := ioutil.ReadDir(PATH)
@ -71,3 +70,16 @@ func ListFiles(c net.Conn) error {
return nil return nil
} }
func ShowHelp(c net.Conn) error {
var helpText string = `
The available commands are:
get <filename> - Download the requested filename.
ls - List the files in the current directory.
clear - Clear the screen.
exit - Close the connection with the server.
`
_, err := c.Write([]byte(helpText))
return err
}

View file

@ -58,7 +58,19 @@ func ProcessInput(c net.Conn, text string) error {
// Ansi clear: 1b 5b 48 1b 5b 4a // Ansi clear: 1b 5b 48 1b 5b 4a
// clear | hexdump -C // clear | hexdump -C
var b []byte = []byte{0x1b, 0x5b, 0x48, 0x1b, 0x5b, 0x4a} var b []byte = []byte{0x1b, 0x5b, 0x48, 0x1b, 0x5b, 0x4a}
c.Write(b) c.Write(b)
case "help":
// Check arguments
err := checkArgumentsLength(commandsLen, 1)
if err != nil {
return &InputError{commands[0], err}
}
err = ShowHelp(c)
if err != nil {
return &InputError{commands[0], err}
}
case "exit": case "exit":
err := checkArgumentsLength(commandsLen, 1) err := checkArgumentsLength(commandsLen, 1)
if err != nil { if err != nil {