Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove gofr's dependency from ftp package #1320

Open
wants to merge 8 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions pkg/gofr/datasource/file/ftp/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"time"

"github.com/jlaffaye/ftp"

file_interface "gofr.dev/pkg/gofr/datasource/file"
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
)

var (
Expand Down Expand Up @@ -50,7 +48,7 @@ type jsonReader struct {
}

// ReadAll reads either JSON or text files based on file extension and returns a corresponding RowReader.
func (f *File) ReadAll() (file_interface.RowReader, error) {
func (f *File) ReadAll() (RowReader, error) {
defer f.sendOperationStats(&FileLog{Operation: "ReadAll", Location: f.path}, time.Now())

if strings.HasSuffix(f.Name(), ".json") {
Expand All @@ -61,7 +59,7 @@ func (f *File) ReadAll() (file_interface.RowReader, error) {
}

// createJSONReader creates a JSON reader for JSON files.
func (f *File) createJSONReader() (file_interface.RowReader, error) {
func (f *File) createJSONReader() (RowReader, error) {
status := statusError

defer f.sendOperationStats(&FileLog{Operation: "JSON Reader", Location: f.path, Status: &status}, time.Now())
Expand Down Expand Up @@ -106,7 +104,7 @@ func (f *File) createJSONReader() (file_interface.RowReader, error) {
}

// createTextCSVReader creates a text reader for reading text files.
func (f *File) createTextCSVReader() (file_interface.RowReader, error) {
func (f *File) createTextCSVReader() (RowReader, error) {
status := statusError

defer f.sendOperationStats(&FileLog{Operation: "Text/CSV Reader", Location: f.path, Status: &status}, time.Now())
Expand Down
10 changes: 4 additions & 6 deletions pkg/gofr/datasource/file/ftp/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"time"

"github.com/jlaffaye/ftp"

file_interface "gofr.dev/pkg/gofr/datasource/file"
)

const (
Expand Down Expand Up @@ -60,7 +58,7 @@ type Config struct {
}

// New initializes a new instance of FTP fileSystem with provided configuration.
func New(config *Config) file_interface.FileSystemProvider {
func New(config *Config) FileSystemProvider {
return &FileSystem{config: config}
}

Expand Down Expand Up @@ -125,7 +123,7 @@ func (f *FileSystem) Connect() {
}

// Create creates an empty file on the FTP server.
func (f *FileSystem) Create(name string) (file_interface.File, error) {
func (f *FileSystem) Create(name string) (FTPFile, error) {
filePath := path.Join(f.config.RemoteDir, name)

var msg string
Expand Down Expand Up @@ -188,7 +186,7 @@ func (f *FileSystem) Create(name string) (file_interface.File, error) {
// Open retrieves a file from the FTP server and returns a file handle.
// Note: Here Open and OpenFile both methods have been implemented so that the
// FTP FileSystem comply with the gofr FileSystem interface.
func (f *FileSystem) Open(name string) (file_interface.File, error) {
func (f *FileSystem) Open(name string) (FTPFile, error) {
var msg string

status := statusError
Expand Down Expand Up @@ -242,7 +240,7 @@ func (f *FileSystem) Open(name string) (file_interface.File, error) {
// Permissions are not clear for Ftp as file commands do not accept an argument and don't store their file permissions.
// currently, this function just calls the Open function.
// Here, os.FileMode is unused, but is added to comply with FileSystem interface.
func (f *FileSystem) OpenFile(name string, _ int, _ os.FileMode) (file_interface.File, error) {
func (f *FileSystem) OpenFile(name string, _ int, _ os.FileMode) (FTPFile, error) {
return f.Open(name)
}

Expand Down
8 changes: 3 additions & 5 deletions pkg/gofr/datasource/file/ftp/fs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"time"

"github.com/jlaffaye/ftp"

file_interface "gofr.dev/pkg/gofr/datasource/file"
)

// Mkdir creates a directory on the FTP server.
Expand Down Expand Up @@ -156,7 +154,7 @@ func (f *FileSystem) RemoveAll(name string) error {
}

// Stat returns information of the files/directories in the specified directory.
func (f *FileSystem) Stat(name string) (file_interface.FileInfo, error) {
func (f *FileSystem) Stat(name string) (FileInfo, error) {
status := statusError

defer f.sendOperationStats(&FileLog{
Expand Down Expand Up @@ -248,7 +246,7 @@ func (f *FileSystem) ChDir(dir string) error {
// ReadDir reads the named directory, returning all its directory entries sorted by filename.
// If an error occurs reading the directory, ReadDir returns the entries it was able to read before the error, along with the error.
// It returns the list of files/directories present in the current directory when "." is passed.
func (f *FileSystem) ReadDir(dir string) ([]file_interface.FileInfo, error) {
func (f *FileSystem) ReadDir(dir string) ([]FileInfo, error) {
var msg string

status := statusError
Expand All @@ -271,7 +269,7 @@ func (f *FileSystem) ReadDir(dir string) ([]file_interface.FileInfo, error) {
return nil, err
}

fileInfo := make([]file_interface.FileInfo, 0)
fileInfo := make([]FileInfo, 0)

for _, entry := range entries {
entryPath := path.Join(filepath, entry.Name)
Expand Down
7 changes: 1 addition & 6 deletions pkg/gofr/datasource/file/ftp/go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
module gofr.dev/pkg/gofr/datasource/file/ftp

go 1.22.3

replace gofr.dev => ../../../../../../gofr
go 1.22.7

require (
github.com/jlaffaye/ftp v0.2.0
github.com/stretchr/testify v1.10.0
go.uber.org/mock v0.5.0
gofr.dev v1.15.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
8 changes: 0 additions & 8 deletions pkg/gofr/datasource/file/ftp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/jlaffaye/ftp v0.2.0 h1:lXNvW7cBu7R/68bknOX3MrRIIqZ61zELs1P2RAiA3lg=
github.com/jlaffaye/ftp v0.2.0/go.mod h1:is2Ds5qkhceAPy2xD6RLI6hmp/qysSoymZ+Z2uTnspI=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand All @@ -17,8 +15,6 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
Expand All @@ -28,10 +24,6 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU=
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
84 changes: 84 additions & 0 deletions pkg/gofr/datasource/file/ftp/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,82 @@ package ftp
import (
"context"
"io"
"os"
"time"

"github.com/jlaffaye/ftp"
)

// FTPFile represents a file in the filesystem.
type FTPFile interface {
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
io.Closer
io.Reader
io.ReaderAt
io.Seeker
io.Writer
io.WriterAt

ReadAll() (RowReader, error)
}

type FileInfo interface {
Name() string // base name of the file
Size() int64 // length in bytes for regular files; system-dependent for others
ModTime() time.Time // modification time
Mode() os.FileMode // file mode bits
IsDir() bool // abbreviation for Mode().IsDir()
}

type RowReader interface {
Next() bool
Scan(interface{}) error
}

// FTPFileSystem : Any simulated or real filesystem should implement this interface.
type FTPFileSystem interface {
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
// Create creates a file in the filesystem, returning the file and an
// error, if any happens.
Create(name string) (FTPFile, error)

// TODO - Lets make bucket constant for MkdirAll as well, we might create buckets from migrations
// Mkdir creates a directory in the filesystem, return an error if any
// happens.
Mkdir(name string, perm os.FileMode) error

// MkdirAll creates a directory path and all parents that does not exist
// yet.
MkdirAll(path string, perm os.FileMode) error

// Open opens a file, returning it or an error, if any happens.
Open(name string) (FTPFile, error)

// OpenFile opens a file using the given flags and the given mode.
OpenFile(name string, flag int, perm os.FileMode) (FTPFile, error)

// Remove removes a file identified by name, returning an error, if any
// happens.
Remove(name string) error

// RemoveAll removes a directory path and any children it contains. It
// does not fail if the path does not exist (return nil).
RemoveAll(path string) error

// Rename renames a file.
Rename(oldname, newname string) error

// ReadDir returns a list of files/directories present in the directory.
ReadDir(dir string) ([]FileInfo, error)

// Stat returns the file/directory information in the directory.
Stat(name string) (FileInfo, error)

// ChDir changes the current directory.
ChDir(dirname string) error

// Getwd returns the path of the current directory.
Getwd() (string, error)
}

// Logger interface is used by ftp package to log information about query execution.
type Logger interface {
Debug(args ...interface{})
Expand Down Expand Up @@ -47,3 +118,16 @@ type ftpResponse interface {
Close() error
SetDeadline(t time.Time) error
}

type FileSystemProvider interface {
FTPFileSystem

// UseLogger sets the logger for the FileSystem client.
UseLogger(logger interface{})

// UseMetrics sets the metrics for the FileSystem client.
UseMetrics(metrics interface{})

// Connect establishes a connection to FileSystem and registers metrics using the provided configuration when the client was Created.
Connect()
}
Loading