From 3e0e994e86445e2e85c068234a15ddce5b51e01d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Fri, 20 Oct 2017 18:20:11 +0300 Subject: [PATCH] Adding the help command --- server/server/commands.go | 16 ++++++++++++++-- server/server/parser.go | 12 ++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/server/server/commands.go b/server/server/commands.go index 889177d..adcd341 100644 --- a/server/server/commands.go +++ b/server/server/commands.go @@ -22,7 +22,7 @@ func SendFile(c net.Conn, path string) (int, error) { lastForwardSlash := strings.LastIndex(path, "/") if lastForwardSlash != -1 { // Eliminate the last forward slash i.e ../../asdas will become asdas - fileName = path[lastForwardSlash + 1:] + fileName = path[lastForwardSlash+1:] } else { fileName = path } @@ -50,7 +50,6 @@ func SendFile(c net.Conn, path string) (int, error) { return n, nil } - // ListFiles list the files from path and sends them to the connection func ListFiles(c net.Conn) error { files, err := ioutil.ReadDir(PATH) @@ -71,3 +70,16 @@ func ListFiles(c net.Conn) error { return nil } + +func ShowHelp(c net.Conn) error { + var helpText string = ` +The available commands are: +get - 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 +} diff --git a/server/server/parser.go b/server/server/parser.go index e6e3438..4454977 100644 --- a/server/server/parser.go +++ b/server/server/parser.go @@ -58,7 +58,19 @@ func ProcessInput(c net.Conn, text string) error { // Ansi clear: 1b 5b 48 1b 5b 4a // clear | hexdump -C var b []byte = []byte{0x1b, 0x5b, 0x48, 0x1b, 0x5b, 0x4a} + 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": err := checkArgumentsLength(commandsLen, 1) if err != nil {