sn_testnet_deploy/
safe.rs

1// Copyright (c) 2023, MaidSafe.
2// All rights reserved.
3//
4// This SAFE Network Software is licensed under the BSD-3-Clause license.
5// Please see the LICENSE file for more details.
6
7use crate::{
8    error::{Error, Result},
9    run_external_command,
10};
11use regex::Regex;
12use std::{
13    net::SocketAddr,
14    path::{Path, PathBuf},
15};
16use tokio::{fs::File as TokioFile, io::AsyncWriteExt};
17
18pub struct SafeClient {
19    pub binary_path: PathBuf,
20    pub working_directory_path: PathBuf,
21}
22
23impl SafeClient {
24    pub fn new(binary_path: PathBuf, working_directory_path: PathBuf) -> SafeClient {
25        SafeClient {
26            binary_path,
27            working_directory_path,
28        }
29    }
30
31    pub fn download_files(&self, peer_multiaddr: &str) -> Result<()> {
32        run_external_command(
33            self.binary_path.clone(),
34            self.working_directory_path.clone(),
35            vec![
36                "--peer".to_string(),
37                peer_multiaddr.to_string(),
38                "files".to_string(),
39                "download".to_string(),
40            ],
41            false,
42            false,
43        )?;
44        Ok(())
45    }
46
47    pub fn upload_file(&self, peer_multiaddr: &str, path: &Path) -> Result<String> {
48        let output = run_external_command(
49            self.binary_path.clone(),
50            self.working_directory_path.clone(),
51            vec![
52                "--peer".to_string(),
53                peer_multiaddr.to_string(),
54                "files".to_string(),
55                "upload".to_string(),
56                path.to_string_lossy().to_string(),
57            ],
58            false,
59            false,
60        )?;
61
62        let re = Regex::new(r"Uploaded .+ to ([a-fA-F0-9]+)")?;
63        for line in &output {
64            if let Some(captures) = re.captures(line) {
65                return Ok(captures[1].to_string());
66            }
67        }
68
69        Err(Error::SafeCmdError(
70            "could not obtain hex address of uploaded file".to_string(),
71        ))
72    }
73
74    pub fn wallet_get_faucet(&self, peer_multiaddr: &str, faucet_addr: SocketAddr) -> Result<()> {
75        run_external_command(
76            self.binary_path.clone(),
77            self.working_directory_path.clone(),
78            vec![
79                "--peer".to_string(),
80                peer_multiaddr.to_string(),
81                "wallet".to_string(),
82                "get-faucet".to_string(),
83                faucet_addr.to_string(),
84            ],
85            false,
86            false,
87        )?;
88        Ok(())
89    }
90}
91
92pub struct SafeBinaryRepository;
93
94impl SafeBinaryRepository {
95    pub async fn download(&self, binary_archive_url: &str, dest_path: &Path) -> Result<()> {
96        let response = reqwest::get(binary_archive_url).await?;
97
98        if !response.status().is_success() {
99            return Err(Error::SafeBinaryDownloadError);
100        }
101
102        let mut dest = TokioFile::create(dest_path).await?;
103        let content = response.bytes().await?;
104        dest.write_all(&content).await?;
105
106        Ok(())
107    }
108}