Refactoring to use Client and Stack
This commit is contained in:
parent
95cd8f17b1
commit
8158edf88c
4 changed files with 24 additions and 14 deletions
|
@ -4,17 +4,21 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetFile sends the file to the client and returns true if it succeeds and false otherwise.
|
// GetFile sends the file to the client and returns true if it succeeds and false otherwise.
|
||||||
func GetFile(c net.Conn, path string) (int, error) {
|
func GetFile(c Client, path string) (int, error) {
|
||||||
fileName := sanitizeFilePath(path)
|
fileName := sanitizeFilePath(path)
|
||||||
|
|
||||||
file, err := os.Open(BasePath + fileName)
|
stack, ok := c.Stack().(*StringStack)
|
||||||
|
if !ok {
|
||||||
|
return 0, ErrStackCast
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(MakePathFromStringStack(stack) + fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return 0, err
|
return 0, err
|
||||||
|
@ -26,7 +30,7 @@ func GetFile(c net.Conn, path string) (int, error) {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err := c.Write(data)
|
n, err := c.Connection().Write(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return 0, err
|
return 0, err
|
||||||
|
@ -61,8 +65,13 @@ func sanitizeFilePath(path string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 Client) error {
|
||||||
files, err := ioutil.ReadDir(BasePath)
|
stack, ok := c.Stack().(*StringStack)
|
||||||
|
if !ok {
|
||||||
|
return ErrStackCast
|
||||||
|
}
|
||||||
|
|
||||||
|
files, err := ioutil.ReadDir(MakePathFromStringStack(stack))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -73,7 +82,7 @@ func ListFiles(c net.Conn) error {
|
||||||
strconv.FormatInt(f.Size(), 10) + " " + f.ModTime().String() + " " + string(f.Name()) + " " + "\n")
|
strconv.FormatInt(f.Size(), 10) + " " + f.ModTime().String() + " " + string(f.Name()) + " " + "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = c.Write(buffer.Bytes())
|
_, err = c.Connection().Write(buffer.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -81,7 +90,7 @@ func ListFiles(c net.Conn) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShowHelp(c net.Conn) error {
|
func ShowHelp(c Client) error {
|
||||||
var helpText = `
|
var helpText = `
|
||||||
The available commands are:
|
The available commands are:
|
||||||
get <filename> - Download the requested filename.
|
get <filename> - Download the requested filename.
|
||||||
|
@ -89,7 +98,7 @@ ls - List the files in the current directory.
|
||||||
clear - Clear the screen.
|
clear - Clear the screen.
|
||||||
exit - Close the connection with the server.
|
exit - Close the connection with the server.
|
||||||
`
|
`
|
||||||
_, err := c.Write([]byte(helpText))
|
_, err := c.Connection().Write([]byte(helpText))
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ type Client interface {
|
||||||
Connection() net.Conn // Connection returns the connection stream.
|
Connection() net.Conn // Connection returns the connection stream.
|
||||||
SetConnection(conn net.Conn) // SetConnection sets the connection for the client.
|
SetConnection(conn net.Conn) // SetConnection sets the connection for the client.
|
||||||
Disconnect() // Disconnect closes the Client's connections and clears up resources.
|
Disconnect() // Disconnect closes the Client's connections and clears up resources.
|
||||||
Stack() Stack // Returns the underlying Stack.
|
Stack() Stack // Returns the underlying String Stack.
|
||||||
}
|
}
|
||||||
|
|
||||||
// FTPClient represents a FTPClient connection, it holds a root cage and the underlying connection.
|
// FTPClient represents a FTPClient connection, it holds a root cage and the underlying connection.
|
||||||
|
|
|
@ -36,6 +36,7 @@ var (
|
||||||
StackInvalidTypeError = StackError{"InvalidTypeError", errors.New("invalid item type for the Stack")}
|
StackInvalidTypeError = StackError{"InvalidTypeError", errors.New("invalid item type for the Stack")}
|
||||||
StackOverflowError = StackError{"StackOverflowError", errors.New("stack capacity exceeded")}
|
StackOverflowError = StackError{"StackOverflowError", errors.New("stack capacity exceeded")}
|
||||||
StackUnderflowError = StackError{"StackUnderflowError", errors.New("stack is empty")}
|
StackUnderflowError = StackError{"StackUnderflowError", errors.New("stack is empty")}
|
||||||
|
ErrStackCast = StackError{"StackCastError", errors.New("stack can't be casted to selected type")}
|
||||||
)
|
)
|
||||||
|
|
||||||
// PathErrors
|
// PathErrors
|
||||||
|
|
|
@ -32,7 +32,7 @@ func ProcessInput(c Client, text string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the file
|
// Get the file
|
||||||
_, err = GetFile(c.Connection(), commands[1])
|
_, err = GetFile(c, commands[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &InputError{thisCommand, err}
|
return &InputError{thisCommand, err}
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ func ProcessInput(c Client, text string) error {
|
||||||
return &InputError{thisCommand, err}
|
return &InputError{thisCommand, err}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ListFiles(c.Connection())
|
err = ListFiles(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &InputError{thisCommand, err}
|
return &InputError{thisCommand, err}
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ func ProcessInput(c Client, text string) error {
|
||||||
return &InputError{thisCommand, err}
|
return &InputError{thisCommand, err}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ShowHelp(c.Connection())
|
err = ShowHelp(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &InputError{thisCommand, err}
|
return &InputError{thisCommand, err}
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ func ProcessInput(c Client, text string) error {
|
||||||
return &InputError{thisCommand, err}
|
return &InputError{thisCommand, err}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Connection().Close()
|
c.Disconnect()
|
||||||
default:
|
default:
|
||||||
return &InputError{thisCommand, InputInvalidCommand}
|
return &InputError{thisCommand, InputInvalidCommand}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue