Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExtraConfig ¶
type ExtraConfig struct { //Username is the username used to connect to the sftp server Username string //Password is the password used to connect to the sftp server Password string //LocalDir is the local directory to sync with the remote directory LocalDir string //RemoteDir is the remote directory to sync with the local directory RemoteDir string //Retries is the number of retries to connect to the sftp server Retries int //MaxRetries is the maximum number of retries to connect to the sftp server MaxRetries int }
ExtraConfig is the struct that holds the extra configuration for the sftp client
type SFTP ¶
type SFTP struct { //Direction is the direction of the sync operation Direction SyncDirection //Watcher is the fsnotify watcher used to watch for file changes Watcher *fsnotify.Watcher //Client is the sftp client Client *sftp.Client //Pool is the worker pool Pool *worker.Pool // contains filtered or unexported fields }
SFtp is the struct that holds the sftp client and the sync direction
func Connect ¶
func Connect(address string, port int, direction SyncDirection, config *ExtraConfig) (*SFTP, error)
Connect establishes an SFTP connection to the remote server at the specified address and port. The function returns an *SFTP object that represents the connection, allowing you to perform file synchronization and other SFTP operations between the local and remote directories.
Parameters:
- address: The IP address or hostname of the remote SFTP server.
- port: The port number to connect to on the remote server.
- direction: The direction of the sync operation, either LocalToRemote or RemoteToLocal.
- config: An optional *ExtraConfig object that holds additional configuration for the SFTP client. If nil, anonymous authentication will be used. If provided, it may contain the username, password, local directory, remote directory, retries, and max retries for connecting to the SFTP server.
Return Values:
- *SFTP: A pointer to the SFTP object representing the connection to the remote server.
- error: If an error occurs during the connection process, it will be returned. Otherwise, it will be nil.
Example Usage:
// Connect to the remote SFTP server using password-based authentication config := &ExtraConfig{ Username: "your_username", Password: "your_password", LocalDir: "/path/to/local/directory", RemoteDir: "/path/to/remote/directory", MaxRetries: 3, } sftpConn, err := Connect("example.com", 22, LocalToRemote, config) if err != nil { log.Fatal("Failed to connect to the SFTP server:", err) } defer sftpConn.Close() // Perform SFTP operations, such as initial sync and directory watching sftpConn.WatchDirectory()
func ConnectSSHPair ¶
func ConnectSSHPair(address string, port int, direction SyncDirection, config *ExtraConfig) (*SFTP, error)
ConnectSSHPair establishes an SFTP connection to the remote server at the specified address and port using SSH key pair authentication. It reads the private key from the current user's home directory (typically the `~/.ssh/id_rsa` file) to use for authentication.
The function returns an *SFTP object that represents the connection, allowing you to perform file synchronization and other SFTP operations between the local and remote directories.
Parameters:
- address: The IP address or hostname of the remote SFTP server.
- port: The port number to connect to on the remote server.
- direction: The direction of the sync operation, either LocalToRemote or RemoteToLocal.
- config: An optional *ExtraConfig object that holds additional configuration for the SFTP client. If nil, default settings will be used. If provided, it may contain the username, local directory, remote directory, retries, and max retries for connecting to the SFTP server.
Return Values:
- *SFTP: A pointer to the SFTP object representing the connection to the remote server.
- error: If an error occurs during the connection process, it will be returned. Otherwise, it will be nil.
Example Usage:
// Connect to the remote SFTP server using SSH key pair authentication config := &ExtraConfig{ Username: "your_username", LocalDir: "/path/to/local/directory", RemoteDir: "/path/to/remote/directory", MaxRetries: 3, } sftpConn, err := ConnectSSHPair("example.com", 22, LocalToRemote, config) if err != nil { log.Fatal("Failed to connect to the SFTP server:", err) } defer sftpConn.Close() // Perform SFTP operations, such as initial sync and directory watching sftpConn.WatchDirectory()
func (*SFTP) AddDirectoriesToWatcher ¶
AddDirectoriesToWatcher adds the specified directory and its subdirectories to the fsnotify watcher based on the SyncDirection of the SFTP connection. For a LocalToRemote connection, it adds the local directory and its subdirectories to the watcher. For a RemoteToLocal connection, it dynamically monitors the remote directory and its subdirectories by continuously comparing the file modifications between successive calls and triggering the corresponding worker to handle the events.
Parameters:
- watcher: The fsnotify.Watcher to which the directories should be added.
- rootDir: The root directory to start watching.
Note: The function will continuously monitor the directories for changes until the SFTP context is canceled.
func (*SFTP) Mkdir ¶
Mkdir creates a directory in the remote server based on the config Parameters:
- dir: The path of the directory to create.
Returns:
- error: If an error occurs during the upload process.
Note: This function is meant to be used within the SFTP struct and should not be called directly.
func (*SFTP) RemoveLocalFile ¶
RemoveLocalFile removes a file from the local server based on the config and the relative path Parameters:
- localPath: The path of the file to remove.
Returns:
- error: If an error occurs during the upload process.
Note: This function is meant to be used within the SFTP struct and should not be called directly.
func (*SFTP) RemoveRemoteFile ¶
RemoveRemoteFile removes a file from the remote server based on the config and the relative path Parameters:
- remotePath: The path of the file to remove.
Returns:
- error: If an error occurs during the upload process.
Note: This function is meant to be used within the SFTP struct and should not be called directly.
func (*SFTP) WatchDirectory ¶
func (s *SFTP) WatchDirectory()
WatchDirectory sets up a file system watcher to monitor changes in the local or remote directory, depending on the SyncDirection of the SFTP connection. When a file or directory event is detected, it triggers the corresponding worker to handle the event.
The function first starts the worker pool, performs an initial synchronization of the local and remote directories using the initialSync method, and then sets up the file system watcher to watch for changes. The watcher is added to the specified local or remote directory, and when a file or directory is created, modified, or removed, the corresponding worker is launched to handle the event.
Note: The worker pool must be running before calling this function.
Usage:
// Assume sftpConn is an established SFTP connection with a worker pool. sftpConn.WatchDirectory()
Example:
// Create an SFTP connection with a worker pool. config := &ExtraConfig{ Username: "your_username", Password: "your_password", LocalDir: "/path/to/local/directory", RemoteDir: "/path/to/remote/directory", Retries: 3, MaxRetries: 5, } sftpConn, err := Connect("your_server_address", 22, LocalToRemote, config) if err != nil { log.Fatal("Failed to connect:", err) }
defer sftpConn.Close()
// Watch for changes in the directory. go sftpConn.WatchDirectory()
func (*SFTP) Worker ¶
func (s *SFTP) Worker()
Worker starts a new worker goroutine that processes tasks received from the worker pool's task channel. The tasks can include file events such as creation, write, and removal events received from the fsnotify watcher.
Note: This function is meant to be used within the SFTP struct and should not be called directly.
type SyncDirection ¶
type SyncDirection int
SyncDirection is the direction of the sync operation
const ( //LocalToRemote is the direction of the sync operation from local to remote LocalToRemote SyncDirection = iota //RemoteToLocal is the direction of the sync operation from remote to local RemoteToLocal )