2022-06-22 22:24:35 +00:00
|
|
|
package disk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Disk interface {
|
2023-12-06 19:37:33 +00:00
|
|
|
// Exists checks if the provided file or directory exists
|
2022-06-22 22:24:35 +00:00
|
|
|
Exists(path string) error
|
2023-12-06 19:37:33 +00:00
|
|
|
|
|
|
|
// Read returns the entire file as a byte buffer
|
|
|
|
//
|
|
|
|
// Returns error if provided path is not a file
|
2022-06-22 22:24:35 +00:00
|
|
|
Read(path string) ([]byte, error)
|
2023-12-06 19:37:33 +00:00
|
|
|
|
|
|
|
// Write writes provided byte buffer to the path
|
2022-06-22 22:24:35 +00:00
|
|
|
Write(path string, data []byte) error
|
2023-12-06 19:37:33 +00:00
|
|
|
|
|
|
|
// Remove deletes the provided file or directory recursively
|
2022-06-22 22:24:35 +00:00
|
|
|
Remove(path string) error
|
2023-12-06 19:37:33 +00:00
|
|
|
|
|
|
|
// MkDir creates the provided directory recursively
|
2022-06-22 22:24:35 +00:00
|
|
|
MkDir(path string) error
|
2023-12-06 19:37:33 +00:00
|
|
|
|
|
|
|
// ReadDir returns all entries within the directory
|
|
|
|
//
|
|
|
|
// Returns error if provided path is not a directory
|
2022-06-22 22:24:35 +00:00
|
|
|
ReadDir(path string) ([]Entry, error)
|
2023-12-06 19:37:33 +00:00
|
|
|
|
|
|
|
// IsNotExist returns true if provided error is a not-exist type error
|
2022-06-22 22:24:35 +00:00
|
|
|
IsNotExist(err error) bool
|
2023-12-06 19:37:33 +00:00
|
|
|
|
|
|
|
// IsExist returns true if provided error is a does-exist type error
|
2022-06-22 22:24:35 +00:00
|
|
|
IsExist(err error) bool
|
2023-12-06 19:37:33 +00:00
|
|
|
|
|
|
|
// Open opens provided path for writing
|
2022-06-22 22:24:35 +00:00
|
|
|
Open(path string, flag int) (io.WriteCloser, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Entry interface {
|
|
|
|
IsDir() bool
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromPath(path string) (Disk, error) {
|
|
|
|
parsed, err := url.Parse(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to parse path")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch parsed.Scheme {
|
|
|
|
case "ftp":
|
|
|
|
log.Info().Str("path", path).Msg("connecting to ftp")
|
|
|
|
return newFTP(path)
|
|
|
|
case "sftp":
|
|
|
|
log.Info().Str("path", path).Msg("connecting to sftp")
|
|
|
|
return newSFTP(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Str("path", path).Msg("using local disk")
|
|
|
|
return newLocal(path)
|
|
|
|
}
|