Use thisCommand instead of commands[0] & Change get command's error reporting

This commit is contained in:
Denis-Cosmin Nutiu 2017-10-20 18:24:07 +03:00
parent 3e0e994e86
commit c9725bd926

View file

@ -24,35 +24,37 @@ func ProcessInput(c net.Conn, text string) error {
return nil return nil
} }
switch commands[0] { thisCommand := commands[0]
switch thisCommand {
case "get": case "get":
// Check arguments // Check arguments
err := checkArgumentsLength(commandsLen, 2) err := checkArgumentsLength(commandsLen, 2)
if err != nil { if err != nil {
return &InputError{commands[0], err} return &InputError{thisCommand, err}
} }
// Get the file // Get the file
_, err = SendFile(c, commands[1]) _, err = SendFile(c, commands[1])
if err != nil { if err != nil {
return &InputError{"SendFile", err} return &InputError{thisCommand, err}
} }
case "ls": case "ls":
// Check arguments // Check arguments
err := checkArgumentsLength(commandsLen, 1) err := checkArgumentsLength(commandsLen, 1)
if err != nil { if err != nil {
return &InputError{commands[0], err} return &InputError{thisCommand, err}
} }
err = ListFiles(c) err = ListFiles(c)
if err != nil { if err != nil {
return &InputError{commands[0], err} return &InputError{thisCommand, err}
} }
case "clear": case "clear":
// Check arguments // Check arguments
err := checkArgumentsLength(commandsLen, 1) err := checkArgumentsLength(commandsLen, 1)
if err != nil { if err != nil {
return &InputError{commands[0], err} return &InputError{thisCommand, err}
} }
// Ansi clear: 1b 5b 48 1b 5b 4a // Ansi clear: 1b 5b 48 1b 5b 4a
@ -64,22 +66,22 @@ func ProcessInput(c net.Conn, text string) error {
// Check arguments // Check arguments
err := checkArgumentsLength(commandsLen, 1) err := checkArgumentsLength(commandsLen, 1)
if err != nil { if err != nil {
return &InputError{commands[0], err} return &InputError{thisCommand, err}
} }
err = ShowHelp(c) err = ShowHelp(c)
if err != nil { if err != nil {
return &InputError{commands[0], err} return &InputError{thisCommand, err}
} }
case "exit": case "exit":
err := checkArgumentsLength(commandsLen, 1) err := checkArgumentsLength(commandsLen, 1)
if err != nil { if err != nil {
return &InputError{commands[0], err} return &InputError{thisCommand, err}
} }
c.Close() c.Close()
default: default:
return &InputError{commands[0], InvalidCommand} return &InputError{thisCommand, InvalidCommand}
} }
return nil return nil