Skip to content

Commit

Permalink
Merge pull request #59 from hwrdtm/upload-file
Browse files Browse the repository at this point in the history
Implement upload file
  • Loading branch information
Miyoshi-Ryota authored May 5, 2024
2 parents 3472fc3 + 93200d2 commit 49aacfd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/super_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
# Full git history is needed to get a proper list of changed files within `super-linter`
fetch-depth: 0
################################
# Run Linter against code base #
################################
- name: Lint Code Base
uses: github/super-linter@v4
uses: github/super-linter@v6
env:
VALIDATE_JSCPD: false
VALIDATE_RUST_2015: false
VALIDATE_RUST_2018: false
VALIDATE_SHELL_SHFMT: false
VALIDATE_CHECKOV: false
DEFAULT_BRANCH: main
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ openssl = ["russh/openssl"]
[dependencies]
russh = "0.40.2"
russh-keys = "0.40.1"
russh-sftp = "2.0.0-beta.4"
thiserror = "1.0"
async-trait = "0.1.61"
tokio = { version = "1.14.0", features = ["fs"] }

[dev-dependencies]
tokio = "1.14.0"
41 changes: 38 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use russh::{
client::{Config, Handle, Handler, Msg},
Channel,
};
use russh_sftp::{client::SftpSession, protocol::OpenFlags};
use std::fmt::Debug;
use std::io::{self, Write};
use std::io;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::io::AsyncWriteExt;

use crate::ToSocketAddrsWithHostname;

Expand Down Expand Up @@ -259,6 +261,37 @@ impl Client {
.map_err(crate::Error::SshError)
}

pub async fn upload_file(
&self,
src_file_path: &str,
dest_file_path: &str,
) -> Result<(), crate::Error> {
// start sftp session
let channel = self.get_channel().await?;
channel.request_subsystem(true, "sftp").await?;
let sftp = SftpSession::new(channel.into_stream()).await?;

// read file contents locally
let file_contents = tokio::fs::read(src_file_path)
.await
.map_err(crate::Error::IoError)?;

// interaction with i/o
let mut file = sftp
.open_with_flags(
dest_file_path,
OpenFlags::CREATE | OpenFlags::TRUNCATE | OpenFlags::WRITE | OpenFlags::READ,
)
.await?;
file.write_all(&file_contents)
.await
.map_err(crate::Error::IoError)?;
file.flush().await.map_err(crate::Error::IoError)?;
file.shutdown().await.map_err(crate::Error::IoError)?;

Ok(())
}

/// Execute a remote command via the ssh connection.
///
/// Returns stdout, stderr and the exit code of the command,
Expand All @@ -284,10 +317,12 @@ impl Client {
//dbg!(&msg);
match msg {
// If we get data, add it to the buffer
russh::ChannelMsg::Data { ref data } => stdout_buffer.write_all(data).unwrap(),
russh::ChannelMsg::Data { ref data } => {
stdout_buffer.write_all(data).await.unwrap()
}
russh::ChannelMsg::ExtendedData { ref data, ext } => {
if ext == 1 {
stderr_buffer.write_all(data).unwrap()
stderr_buffer.write_all(data).await.unwrap()
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ pub enum Error {
SendError(#[from] russh::SendError),
#[error("Agent auth error")]
AgentAuthError(#[from] russh::AgentAuthError),
#[error("SFTP error occured: {0}")]
SftpError(#[from] russh_sftp::client::error::Error),
#[error("I/O error")]
IoError(#[from] io::Error),
}

0 comments on commit 49aacfd

Please sign in to comment.