simplFT/server/parser.go

104 lines
2 KiB
Go
Raw Normal View History

2017-10-20 15:08:25 +00:00
package server
import (
"strings"
)
// checkArgumentsLength returns an error if length is not equal to expected.
func checkArgumentsLength(length int, expected int) error {
if length > expected {
2017-10-28 16:02:26 +00:00
return InputTooManyArguments
2017-10-20 15:08:25 +00:00
} else if length < expected {
2017-10-28 16:02:26 +00:00
return InputTooFewArguments
2017-10-20 15:08:25 +00:00
}
return nil
}
func ProcessInput(c Client, text string) error {
2017-10-20 15:08:25 +00:00
commands := strings.Fields(text)
commandsLen := len(commands)
if commandsLen == 0 {
return nil
}
thisCommand := commands[0]
switch thisCommand {
2017-11-09 20:39:58 +00:00
case "cd":
err := checkArgumentsLength(commandsLen, 2)
if err != nil {
return &InputError{thisCommand, err}
}
err = ChangeDirectoryCommand(c, commands[1])
if err != nil {
return err
}
2017-10-20 15:08:25 +00:00
case "get":
err := checkArgumentsLength(commandsLen, 2)
if err != nil {
return &InputError{thisCommand, err}
2017-10-20 15:08:25 +00:00
}
// Get the file
2017-11-08 20:18:23 +00:00
_, err = GetFile(c, commands[1])
2017-10-20 15:08:25 +00:00
if err != nil {
return &InputError{thisCommand, err}
2017-10-20 15:08:25 +00:00
}
2017-11-24 20:16:06 +00:00
case "pic":
err := checkArgumentsLength(commandsLen, 2)
if err != nil {
return &InputError{thisCommand, err}
}
// Get the file
2017-12-16 20:46:55 +00:00
err = SendASCIIPic(c, commands[1])
2017-11-24 20:16:06 +00:00
if err != nil {
return &InputError{thisCommand, err}
}
2017-10-20 15:08:25 +00:00
case "ls":
err := checkArgumentsLength(commandsLen, 1)
if err != nil {
return &InputError{thisCommand, err}
2017-10-20 15:08:25 +00:00
}
2017-11-08 20:18:23 +00:00
err = ListFiles(c)
2017-10-20 15:08:25 +00:00
if err != nil {
return &InputError{thisCommand, err}
2017-10-20 15:08:25 +00:00
}
case "clear":
err := checkArgumentsLength(commandsLen, 1)
if err != nil {
return &InputError{thisCommand, err}
2017-10-20 15:08:25 +00:00
}
2017-11-09 20:39:58 +00:00
err = ClearScreen(c)
if err != nil {
return err
}
2017-10-20 15:20:11 +00:00
case "help":
// Check arguments
err := checkArgumentsLength(commandsLen, 1)
if err != nil {
return &InputError{thisCommand, err}
2017-10-20 15:20:11 +00:00
}
2017-11-08 20:18:23 +00:00
err = ShowHelp(c)
2017-10-20 15:20:11 +00:00
if err != nil {
return &InputError{thisCommand, err}
2017-10-20 15:20:11 +00:00
}
2017-10-20 15:08:25 +00:00
case "exit":
err := checkArgumentsLength(commandsLen, 1)
if err != nil {
return &InputError{thisCommand, err}
2017-10-20 15:08:25 +00:00
}
2017-11-08 20:18:23 +00:00
c.Disconnect()
2017-10-20 15:08:25 +00:00
default:
2017-10-28 16:02:26 +00:00
return &InputError{thisCommand, InputInvalidCommand}
2017-10-20 15:08:25 +00:00
}
return nil
2017-10-28 20:40:24 +00:00
}